[英]Making an image black and white in C#
我正在嘗試在C#中制作黑白圖像。 這是我的代碼:
public static void SetGrayscale(Bitmap b)
{
Bitmap temp = (Bitmap) b;
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));
}
}
b = (Bitmap)bmap.Clone();
}
static void Main(string[] args)
{
Bitmap bm = new Bitmap(Image.FromFile("D:\\users\\visual studio 2010\\Projects\\aaa\\20130924_144411.tif"));
byte[] pixels = ImageToByte(bm);
SetGrayscale(bm);
}
問題是,它不會變成黑白,它仍然相同。 我不保存更改的圖像嗎? 這可能是什么問題?
謝謝
您是指磁盤上的文件沒有更改嗎? 您將必須保存灰度位圖: bm.Save("D:\\\\bitmap.tif");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.