Source/NFX.WinForms/Utils.cs
/// <summary>
/// Converts a bitmap into grayscale mode
/// </summary>
Converts a bitmap into grayscale mode
Converts a bitmap into grayscale mode
public static Bitmap CreateGrayscaleBitmap(Bitmap fromBitmap)
    {
      Bitmap newBitmap = new Bitmap(fromBitmap.Width, fromBitmap.Height);
      using (Graphics gr = Graphics.FromImage(newBitmap))
      {
        ColorMatrix colorMatrix = new ColorMatrix(
           new float[][]
        {
           new float[] {.3f, .3f, .3f, 0, 0},
           new float[] {.59f, .59f, .59f, 0, 0},
           new float[] {.11f, .11f, .11f, 0, 0},
           new float[] {0, 0, 0, 1, 0},
           new float[] {0, 0, 0, 0, 1}
        });
        using (ImageAttributes attr = new ImageAttributes())
        {
            attr.SetColorMatrix(colorMatrix);
            gr.DrawImage(fromBitmap, new Rectangle(0, 0, fromBitmap.Width, fromBitmap.Height),
               0, 0, fromBitmap.Width, fromBitmap.Height, GraphicsUnit.Pixel, attr);
        }
      }
      return newBitmap;
    }
public static Bitmap CreateGrayscaleBitmap ( Bitmap fromBitmap ) { Bitmap newBitmap = new Bitmap ( fromBitmap . Width , fromBitmap . Height ) ; using ( Graphics gr = Graphics . FromImage ( newBitmap ) ) { ColorMatrix colorMatrix = new ColorMatrix ( new float [ ] [ ] { new float [ ] { .3f , .3f , .3f , 0 , 0 } , new float [ ] { .59f , .59f , .59f , 0 , 0 } , new float [ ] { .11f , .11f , .11f , 0 , 0 } , new float [ ] { 0 , 0 , 0 , 1 , 0 } , new float [ ] { 0 , 0 , 0 , 0 , 1 } } ) ; using ( ImageAttributes attr = new ImageAttributes ( ) ) { attr . SetColorMatrix ( colorMatrix ) ; gr . DrawImage ( fromBitmap , new Rectangle ( 0 , 0 , fromBitmap . Width , fromBitmap . Height ) , 0 , 0 , fromBitmap . Width , fromBitmap . Height , GraphicsUnit . Pixel , attr ) ; } } return newBitmap ; }
Converts a bitmap into grayscale mode
Converts a bitmap into grayscale mode
//create the grayscale ColorMatrix covert ramp
fromBitmap
Bitmap