/*Insert this as a function anywere, I used it in a seperate DLL to keep my form code clean. Just pass the Bitmap and the picture box to show it in to the function*/
public void greyscale
(Bitmap bmp, PictureBox picBox
)
{
BitmapData data = bmp.
LockBits(new Rectangle
(0,
0, bmp.
Width, bmp.
Height),
ImageLockMode.
ReadWrite, PixelFormat.
Format24bppRgb);
unsafe
{
byte* imgPtr =
(byte*
)(data.
Scan0);
byte red, green, blue;
for (int i =
0; i < data.
Height; i++
)
{
for (int j =
0; j < data.
Width; j++
)
{
blue = imgPtr
[0];
green = imgPtr
[1];
red = imgPtr
[2];
imgPtr
[0] = imgPtr
[1] = imgPtr
[2] =
(byte)(.
299 * red
+ .
587 * green
+ .
114 * blue
);
imgPtr +=
3;
}
imgPtr += data.
Stride - data.
Width *
3;
}
}
bmp.
UnlockBits(data
);
picBox.
Image = bmp;
}