BitMap イメージをセピア変換する方法

  BitMap イメージをセピア変換するには、ColorMatrixImageAttributes を利用して行います。
【使い方】
// <summary>
/// 指定した BitMap イメージをセピア変換して返します。
/// </summary>
/// <param name="bmpBase">変換元となる BitMap イメージ</param>
/// <returns>セピア変換した BitMap イメージ</returns>
private Bitmap TransformToSepia( 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.25714F, 0.2093F, 0, 0},
        new float[]{0.587F, 0.50482F, 0.4109F, 0, 0},
        new float[]{0.114F, 0.09804F, 0.0798F, 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;
}
【注意】
戻る