Skip to content

Instantly share code, notes, and snippets.

@cemerson
Created December 26, 2018 17:39
Show Gist options
  • Save cemerson/4f55bd017b69fe54785e58cf8e35b7b3 to your computer and use it in GitHub Desktop.
Save cemerson/4f55bd017b69fe54785e58cf8e35b7b3 to your computer and use it in GitHub Desktop.
C# Convert Image Grayscale
// source: https://stackoverflow.com/a/3054357/826308
public static Image ConvertToGrayscale(Image image)
{
Image grayscaleImage = new Bitmap(image.Width, image.Height, image.PixelFormat);
// Create the ImageAttributes object and apply the ColorMatrix
ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes();
ColorMatrix grayscaleMatrix = new ColorMatrix(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}
});
attributes.SetColorMatrix(grayscaleMatrix);
// Use a new Graphics object from the new image.
using (Graphics g = Graphics.FromImage(grayscaleImage))
{
// Draw the original image using the ImageAttributes created above.
g.DrawImage(image,
new Rectangle(0, 0, grayscaleImage.Width, grayscaleImage.Height),
0, 0, grayscaleImage.Width, grayscaleImage.Height,
GraphicsUnit.Pixel,
attributes);
}
return grayscaleImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment