调整图像的亮度对比度和伽玛值 [英] Adjust brightness contrast and gamma of an image

查看:42
本文介绍了调整图像的亮度对比度和伽玛值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is an easy way to adjust brightness contrast and gamma of an Image in .NET

将自己发布答案以供稍后查找.

Will post the answer myself to find it later.

推荐答案

c# 和 gdi+ 有一个简单的方法来控制绘制的颜色.它基本上是一个 ColorMatrix.这是一个 5×5 矩阵,应用于如果设置了每种颜色.调整亮度只是执行一个转换颜色数据,对比度正在对颜色数据进行缩放颜色.Gamma 是一种完全不同的变换形式,但它包括在内在接受 ColorMatrix 的 ImageAttributes 中.

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);

这篇关于调整图像的亮度对比度和伽玛值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆