将8位彩色bmp图像转换为8位灰度bmp

这是我的位图对象

Bitmap b = new Bitmap(columns, rows, PixelFormat.Format8bppIndexed); BitmapData bmd = b.LockBits(new Rectangle(0, 0, columns, rows), ImageLockMode.ReadWrite, b.PixelFormat); 

如何将其转换为8位灰度位图?

您好,您可以将调色板更改为灰度调色板

虽然以下代码在Vb.net中。 您可以轻松将其转换为C#

 Private Function GetGrayScalePalette() As ColorPalette Dim bmp As Bitmap = New Bitmap(1, 1, Imaging.PixelFormat.Format8bppIndexed) Dim monoPalette As ColorPalette = bmp.Palette Dim entries() As Color = monoPalette.Entries Dim i As Integer For i = 0 To 256 - 1 Step i + 1 entries(i) = Color.FromArgb(i, i, i) Next Return monoPalette End Function 

原始资料 – > http://social.msdn.microsoft.com/Forums/en-us/vblanguage/thread/500f7827-06cf-4646-a4a1-e075c16bbb38

是的,无需更改像素,只需调色板就可以了。 ColorPalette是一个片状类型,这个示例代码运行良好:

  var bmp = Image.FromFile("c:/temp/8bpp.bmp"); if (bmp.PixelFormat != System.Drawing.Imaging.PixelFormat.Format8bppIndexed) throw new InvalidOperationException(); var newPalette = bmp.Palette; for (int index = 0; index < bmp.Palette.Entries.Length; ++index) { var entry = bmp.Palette.Entries[index]; var gray = (int)(0.30 * entry.R + 0.59 * entry.G + 0.11 * entry.B); newPalette.Entries[index] = Color.FromArgb(gray, gray, gray); } bmp.Palette = newPalette; // Yes, assignment to self is intended if (pictureBox1.Image != null) pictureBox1.Image.Dispose(); pictureBox1.Image = bmp; 

我实际上并不建议你使用这个代码,索引像素格式是一个皮塔饼来处理。 您将在此答案中找到快速且更通用的颜色到灰度转换。

就像是:

 Bitmap b = new Bitmap(columns, rows, PixelFormat.Format8bppIndexed); for (int i = 0; i < columns; i++) { for (int x = 0; x < rows; x++) { Color oc = b.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); b.SetPixel(i, x, nc); } } BitmapData bmd = b.LockBits(new Rectangle(0, 0, columns, rows), ImageLockMode.ReadWrite, b.PixelFormat); 

请注意,如果您想要与现代高清电视进行相同的转换,则需要使用Rec。 709个转换系数。 上面提供的那些(.3,.59,.11)(几乎)是Rec。 601(标准def)系数。 Rec。 709个系数是灰色= 0.2126 R’+ 0.7152 G’+ 0.0722 B’,其中R’,G’和B’是伽马调整的红色,绿色和蓝色分量。

检查此链接 。 我们在大学做过这件事并且有效。

输入和输出就是您所需要的。