C#中級 コーディング例
G8)画像明度補正
画像を明度補正して、ビットマップで返します。暗く・元・明るく
//source:対象イメージ画像
public static Bitmap AdjustBrightness(Image source, float brightness)
{
// brightness: -1.0f 〜 +1.0f(0 で変化なし)
//
float b = brightness;
//ColorMatrix はこれをそのまま行列化したものです
float[][] ptsArray = {
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {b, b, b, 0, 1}
};
var colorMatrix = new ColorMatrix(ptsArray);
var attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
//ImageAttributes オブジェクトを、Graphics オブジェクトの DrawImage メソッドに渡します
var outputBmp = new Bitmap(source.Width, source.Height);
using (var g = Graphics.FromImage(outputBmp))
{
g.DrawImage(source,
new Rectangle(0, 0, outputBmp.Width, outputBmp.Height),
0, 0, source.Width, source.Height,
GraphicsUnit.Pixel,
attributes);
}
return outputBmp;
}
元画像
暗い 明るい