ما در این مقاله قصد داریم با استفاده از سی شارپ عکسی را سیاه و سفید کنیم
یک پروژه ویندوز فرم ایجاد کنید و ظاهر پروژه را به صورت زیر بسازید

در رویداد کلیک باتن open کد زیر را قرار دهید
private void btnOpen_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Images|*.jpg" })
{
if (ofd.ShowDialog() == DialogResult.OK)
picOriginal.Image = Image.FromFile(ofd.FileName);
}
}
یک متد به شکل زیر بسازید
public Bitmap MakeGrayscale(Bitmap original)
{
Bitmap newBmp = new Bitmap(original.Width, original.Height);
Graphics g = Graphics.FromImage(newBmp);
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
ImageAttributes img = new ImageAttributes();
img.SetColorMatrix(colorMatrix);
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, img);
g.Dispose();
return newBmp;
}
کد زیر را به باتن convert اضافه کنید
private void btnConvert_Click(object sender, EventArgs e)
{
picConvert.Image = MakeGrayscale((Bitmap)picOriginal.Image);
}
تمام.