我需要将黑色图像转换为灰度图像。我的代码将彩色图像转换为灰度图像。但不会转换为黑色图像 到灰度图像。甚至尝试从stackoverflow中的一个样本 灰度转换。也不适用于黑色到灰度 图像转换。该链接为https://stackoverflow.com/a/2265990/8569792
public void GreyScaleConversion()
{
//read image
Bitmap bmp = global::Ribbon.Properties.Resources.Device;
//load original image in picturebox1
pictureBox1.Image = global::Ribbon.Properties.Resources.Device;
//get image dimension
int width = bmp.Width;
int height = bmp.Height;
//color of pixel
Color p;
//grayscale
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
//get pixel value
p = bmp.GetPixel(x, y);
//extract pixel component ARGB
int a = p.A;
int r = p.R;
int g = p.G;
int b = p.B;
//find average
int avg = (r + g + b) / 3;
//set new pixel value
bmp.SetPixel(x, y, Color.FromArgb(a, avg, avg, avg));
}
}
//load grayscale image in picturebox2
pictureBox2.Image = bmp;
}
答案 0 :(得分:0)
public void GreyScaleConversion()
{
//read image
Bitmap bmp = global::Ribbon.Properties.Resources.Device;
//load original image in picturebox1
pictureBox1.Image = global::Ribbon.Properties.Resources.Device;
//get image dimension
int width = bmp.Width;
int height = bmp.Height;
//color of pixel
Color p;
//grayscale
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
//get pixel value
p = bmp.GetPixel(x, y);
//extract pixel component A
int a = p.A;
//set new pixel value
bmp.SetPixel(x, y, Color.FromArgb(a, 155, 155, 155));
}
}
//load grayscale image in picturebox2
pictureBox2.Image = bmp;
}