How to Grayscale a bitmap

Will be posting article on how to create simple application, with full working code in C#
And welcome to anyone who want to post their own article
Post Reply
User avatar

Topic author
Site Admin
Site Admin
Man of action
Man of action
Posts: 1330
Joined: Sat Apr 16, 2011 7:49 am
Location: Montreal, Canada

How to Grayscale a bitmap


Post by Superl »

Grayscale a bitmap Introduction:
Change a Color Image into a Grayscale(Black & White) Image.

Code: Select all

/// <summary>
/// Convert an Image to a Grayscale Image.
/// </summary>
/// <param name="Bitmap">The Bitmap to Convert to Grayscale.</param>
/// <returns>A Grayscale Image.</returns>
public static Bitmap Grayscale(Bitmap bitmap)
//Declare myBitmap as a new Bitmap with the same Width & Height
Bitmap myBitmap = new Bitmap(bitmap.Width, bitmap.Height);

for (int i = 0; i < bitmap.Width; i++)
for (int x = 0; x < bitmap.Height; x++)
//Get the Pixel
Color BitmapColor = bitmap.GetPixel(i, x);

//Declare grayScale as the Grayscale Pixel
int grayScale = (int)((BitmapColor.R * 0.3) + (BitmapColor.G * 0.59) + (BitmapColor.B * 0.11));

//Declare myColor as a Grayscale Color
Color myColor = Color.FromArgb(grayScale, grayScale, grayScale);

//Set the Grayscale Pixel
myBitmap.SetPixel(i, x, myColor);
return myBitmap;

Come and say hello in here
Any donation will help click here please.

Have a nice day :103:
Post Reply

Return to “Coding Forum”