//using System.Drawing.Imaging;private void button1_Click(object sender, EventArgs e) { Bitmap bmp = new Bitmap(@"c:\temp\Oryx Antelope.jpg"); // a color matrix to produce grayscel image ColorMatrix cm = new ColorMatrix(new float[][] { new float[] {.30f, .30f, .30f, 0, 0}, new float[] {.59f, .59f, .59f, 0, 0}, // human eyes are more sensitive to green color new float[] {.11f, .11f, .11f, 0, 0}, // the least sensitive to blue. new float[] {.00f, .00f, .00f, 1, 0}, new float[] {.00f, .00f, .00f, 0, 1} }); Bitmap result = new Bitmap(bmp.Width, bmp.Height); using (Graphics g = Graphics.FromImage(result)) { // setting up color matrix and also Threshold value for binarization. ImageAttributes ia = new ImageAttributes(); ia.SetColorMatrix(cm); ia.SetThreshold(0.5f); //<--- g.DrawImage(bmp, new Rectangle(0, 0, result.Width, result.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia); } result.Save(@"c:\temp\black&white.bmp"); result.Dispose(); bmp.Dispose(); } Hope this might help.
//using System.Drawing.Imaging;private void button1_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(@"c:\temp\Oryx Antelope.jpg"); // a color matrix to produce grayscel image
ColorMatrix cm = new ColorMatrix(new float[][]
{
new float[] {.30f, .30f, .30f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0}, // human eyes are more sensitive to green color
new float[] {.11f, .11f, .11f, 0, 0}, // the least sensitive to blue.
new float[] {.00f, .00f, .00f, 1, 0},
new float[] {.00f, .00f, .00f, 0, 1}
}); Bitmap result = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(result))
{
// setting up color matrix and also Threshold value for binarization.
ImageAttributes ia = new ImageAttributes();
ia.SetColorMatrix(cm);
ia.SetThreshold(0.5f); //<--- g.DrawImage(bmp, new Rectangle(0, 0, result.Width, result.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia);
}
result.Save(@"c:\temp\black&white.bmp"); result.Dispose();
bmp.Dispose();
}
Hope this might help.