Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public void SetGrayscale()
{
Bitmap temp = (Bitmap)_currentBitmap;
Bitmap bmap = (Bitmap)temp.Clone();
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
byte gray = (byte)(.299 * c.R + .587 * c.G + .114 * c.B);

bmap.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
}
}
_currentBitmap = (Bitmap)bmap.Clone();
}
Posted
Comments
BillWoodruff 15-Oct-14 2:40am    
You have a wonderful opportunity to learn here: I suggest you start by looking up in the MS documentation each operator in each line of this code, and studying how it is used, and what it does.

1 solution

The commented code is as follows:

public void SetGrayscale()
{
// Get the bitmap image
Bitmap temp = (Bitmap)_currentBitmap;

// Create clone of bitmap image
Bitmap bmap = (Bitmap)temp.Clone();

// Color class to change the
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
// Get the pixel position of the image
c = bmap.GetPixel(i, j);

// Change the color of pixel by latering the RGB values
byte gray = (byte)(.299 * c.R + .587 * c.G + .114 * c.B);

// Modify the altered values in the cloned image
bmap.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
}
}
// Replace the current image by the cloned image
_currentBitmap = (Bitmap)bmap.Clone();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900