Bitmap c = new Bitmap("filename");
我想要一个名为d的Bitmap,它是c的灰度版本。我看到有一个构造函数包括System.Drawing.Imaging.PixelFormat,但我不知道如何使用它。我对图像处理和相关的C#库是新手,但对C#本身有一定的经验。任何帮助、在线资源的参考、提示或建议都将不胜感激。
编辑:d是c的灰度版本。
Bitmap c = new Bitmap("filename");
我想要一个名为d的Bitmap,它是c的灰度版本。我看到有一个构造函数包括System.Drawing.Imaging.PixelFormat,但我不知道如何使用它。我对图像处理和相关的C#库是新手,但对C#本身有一定的经验。我想要一个灰度的 Bitmap d。 我看到了一个包括 System.Drawing.Imaging.PixelFormat 的构造函数, 但我不知道如何使用它。
以下是实现方法:
Bitmap grayScaleBP = new
System.Drawing.Bitmap(2, 2, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
编辑: 转换为灰度
Bitmap c = new Bitmap("fromFile");
Bitmap d;
int x, y;
// Loop through the images pixels to reset color.
for (x = 0; x < c.Width; x++)
{
for (y = 0; y < c.Height; y++)
{
Color pixelColor = c.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
c.SetPixel(x, y, newColor); // Now greyscale
}
}
d = c; // d is grayscale version of c
更快的版本来自switchonthecode,请点击链接获取完整分析:
public static Bitmap MakeGrayscale3(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
using(Graphics g = Graphics.FromImage(newBitmap)){
//create the grayscale ColorMatrix
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}
});
//create some image attributes
using(ImageAttributes attributes = new ImageAttributes()){
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
}
}
return newBitmap;
}
Bitmap d = new Bitmap(c.Width, c.Height);
for (int i = 0; i < c.Width; i++)
{
for (int x = 0; x < c.Height; x++)
{
Color oc = c.GetPixel(i, x);
int grayScale = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11));
Color nc = Color.FromArgb(oc.A, grayScale, grayScale, grayScale);
d.SetPixel(i, x, nc);
}
}
这种方法还可以保留 alpha 通道。
享受吧。
ToolStripRenderer
类中有一个静态方法,名为CreateDisabledImage
。
它的使用非常简单:
Bitmap c = new Bitmap("filename");
Image d = ToolStripRenderer.CreateDisabledImage(c);
它使用的矩阵与被接受的答案略有不同,并且另外将其乘以透明度值为0.7,因此效果略有不同于只是灰度化,但如果您想使您的图像变成灰色,这是最简单和最好的解决方案。
Bitmap bt = new Bitmap("imageFilePath");
for (int y = 0; y < bt.Height; y++)
{
for (int x = 0; x < bt.Width; x++)
{
Color c = bt.GetPixel(x, y);
int r = c.R;
int g = c.G;
int b = c.B;
int avg = (r + g + b) / 3;
bt.SetPixel(x, y, Color.FromArgb(avg,avg,avg));
}
}
bt.Save("d:\\out.bmp");
以上示例均未创建8位(8bpp)位图图像。一些软件,如图像处理,仅支持8bpp。不幸的是,MS .NET库没有解决方案。PixelFormat.Format8bppIndexed格式看起来很有前途,但经过多次尝试,我无法使其工作。
要创建真正的8位位图文件,您需要创建正确的标头。最终,我找到了Grayscale库用于创建8位位图(BMP)文件的解决方案。代码非常简单:
Image image = Image.FromFile("c:/path/to/image.jpg");
GrayBMP_File.CreateGrayBitmapFile(image, "c:/path/to/8bpp/image.bmp");
这个项目的代码并不漂亮,但是它可以工作,只有一个小问题需要简单修复。作者在代码中将图像分辨率硬编码为10x10。图像处理程序不喜欢这个设置。解决方法是打开GrayBMP_File.cs文件(是的,名字很奇怪),并用下面的代码替换第50和51行。该示例将分辨率设置为200x200,但您应该将其更改为正确的数字。
int resX = 200;
int resY = 200;
// horizontal resolution
Copy_to_Index(DIB_header, BitConverter.GetBytes(resX * 100), 24);
// vertical resolution
Copy_to_Index(DIB_header, BitConverter.GetBytes(resY * 100), 28);
image.LockBits()
来获取它,然后使用Marshal.Copy
复制数据。请谷歌搜索以获取完整的过程。请注意,图像的步幅(从数据中读取的行)通常比实际宽度长,因为它会四舍五入到最接近4个字节的倍数。 - Nyerguds总结一下这里的几点:
有一些逐像素的选项,虽然简单但不够快。
@Luis的评论链接:(存档) https://web.archive.org/web/20110827032809/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale 非常好。
他介绍了三种不同的选项,并包括每个选项的时间。
我认为在这种情况下这是一个好方法:
/// <summary>
/// Change the RGB color to the Grayscale version
/// </summary>
/// <param name="color">The source color</param>
/// <param name="volume">Gray scale volume between -255 - 255</param>
/// <returns></returns>
public virtual Color Grayscale(Color color, short volume = 0)
{
if (volume == 0) return color;
var r = color.R;
var g = color.G;
var b = color.B;
var mean = (r + g + b) / 3F;
var n = volume / 255F;
var o = 1 - n;
return Color.FromArgb(color.A, Convert.ToInt32(r * o + mean * n), Convert.ToInt32(g * o + mean * n), Convert.ToInt32(b * o + mean * n));
}
public virtual Image Grayscale(Image source, short volume = 0)
{
if (volume == 0) return source;
Bitmap bmp = new Bitmap(source);
for (int x = 0; x < bmp.Width; x++)
for (int y = 0; y < bmp.Height; y++)
{
Color c = bmp.GetPixel(x, y);
if (c.A > 0)
bmp.SetPixel(x, y, Grayscale(c,volume));
}
return bmp;
}
享受...