c# - 调整图像的亮度对比度和 Gamma

标签 c# .net image image-processing brightness

在 .NET 中调整图像的亮度对比度和 Gamma 值的简单方法是什么

我会自己发布答案以供稍后查找。

最佳答案

c# and gdi+ have a simple way to control the colors that are drawn. It’s basically a ColorMatrix. It’s a 5×5 matrix that is applied to each color if it is set. Adjusting brightness is just performing a translate on the color data, and contrast is performing a scale on the color. Gamma is a whole different form of transform, but it’s included in ImageAttributes which accepts the ColorMatrix.

Bitmap originalImage;
Bitmap adjustedImage;
float brightness = 1.0f; // no change in brightness
float contrast = 2.0f; // twice the contrast
float gamma = 1.0f; // no change in gamma

float adjustedBrightness = brightness - 1.0f;
// create matrix that will brighten and contrast the image
float[][] ptsArray ={
        new float[] {contrast, 0, 0, 0, 0}, // scale red
        new float[] {0, contrast, 0, 0, 0}, // scale green
        new float[] {0, 0, contrast, 0, 0}, // scale blue
        new float[] {0, 0, 0, 1.0f, 0}, // don't scale alpha
        new float[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1}};

ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.ClearColorMatrix();
imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
Graphics g = Graphics.FromImage(adjustedImage);
g.DrawImage(originalImage, new Rectangle(0,0,adjustedImage.Width,adjustedImage.Height)
    ,0,0,originalImage.Width,originalImage.Height,
    GraphicsUnit.Pixel, imageAttributes);

关于c# - 调整图像的亮度对比度和 Gamma ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15408607/

相关文章:

c# - 为什么我的图像大小调整代码总是产生全黑的输出图像?

c# - 如何在 ItemCommand 事件中获取 Repeater Item 数据对象

c# - 我可以用 c# 解密在 php 中用 PASSWORD_BCRYPT 加密的密码吗?

c# - C# 中静态成员的别名?

html - 如何使用 HTML 和 CSS 将 14 张图片放在两个垂直列中,每张图片下方都有一个标题,并且图片也排在每一行上?

cocoa - 如何从 NSBitmapImageRep 创建具有透明度的 8 位 PNG?

c# - 最小起订量错误 "An expression tree may not contain a call or invocation that uses optional arguments"

.net - 在 VB .NET 中查找并启动 VPN 连接

c# - 计算 DataGrid 中可见行的数量

c# - 获取 Type 中使用的程序集的路径