【使い方】
/// <summary>
/// 指定した BitMap イメージをモノクロ変換して返します。
/// </summary>
/// <param name="bmpBase">変換元となる BitMap イメージ</param>
/// <returns>モノクロ変換した BitMap イメージ</returns>
private Bitmap TransformToMono( Bitmap bmpBase )
{
// 変換作業用 BitMap イメージを生成
Bitmap bmp = new Bitmap(bmpBase.Width, bmpBase.Height);
// Graphics を生成
Graphics g = Graphics.FromImage(bmp);
// モノクロ変換カラーマトリックス用値配列
float[][] colorMatrixElements = new float[][]
{
new float[]{0.299F, 0.299F, 0.299F, 0, 0},
new float[]{0.587F, 0.587F, 0.587F, 0, 0},
new float[]{0.114F, 0.114F, 0.114F, 0, 0},
new float[]{0, 0, 0, 1, 0},
new float[]{0, 0, 0, 0, 1}
};
// モノクロ変換カラーマトリックス生成
System.Drawing.Imaging.ColorMatrix cm =
new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
//ImageAttributesオブジェクトの作成
System.Drawing.Imaging.ImageAttributes ia =
new System.Drawing.Imaging.ImageAttributes();
//ColorMatrixを設定する
ia.SetColorMatrix(cm);
//ImageAttributesを使用してモノクロ画像を描画
g.DrawImage(bmpBase, new Rectangle(0, 0, bmpBase.Width, bmpBase.Height),
0, 0, bmpBase.Width, bmpBase.Height, GraphicsUnit.Pixel, ia);
//リソースを開放する
g.Dispose();
//変換画像を返す
return bmp;
}
|