C#で画像(Bitmap)の輝度とコントラストに変更を加えるメソッドを作成しています。
下記のソースコードで問題ないでしょうか?
分かる方いらっしゃいましたら、回答お願い致します。
/// <summary>輝度コントラスト変更メソッド</summary>
/// <param name="img">基になる画像</param>
/// <param name="brightness">明るさ(-255~255)</param>
/// <param name="contrast">コントラストの値(-100~100)</param>
/// <returns>変更した画像</returns>
private System.Drawing.Bitmap AdjustBrightness(System.Drawing.Bitmap img, double brightness, double contrast)
{
// 明るさを変更した画像の描画先となるImageオブジェクトを作成
System.Drawing.Bitmap newImg = new System.Drawing.Bitmap(img.Width, img.Height);
// newImgのGraphicsオブジェクトを取得
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newImg);
// ColorMatrixオブジェクトの作成 float plusVal = (float)brightness / 255f; float scale = (float)((100f + contrast) / 100f); scale *= scale; float append = 0.5f * (1f - scale); System.Drawing.Imaging.ColorMatrix cm = new System.Drawing.Imaging.ColorMatrix(new float[][] { new float[] { scale, 0, 0, 0, 0 }, new float[] { 0, scale, 0, 0, 0 }, new float[] { 0, 0, scale, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { (plusVal + append), (plusVal + append), (plusVal + append), 0, 1 } }); // ImageAttributesオブジェクトの作成 System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes(); // ColorMatrixを設定する ia.SetColorMatrix(cm); // ImageAttributesを使用して描画 g.DrawImage(img, new System.Drawing.Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, System.Drawing.GraphicsUnit.Pixel, ia); // リソースを解放する g.Dispose(); return newImg; }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。