【发布时间】:2015-11-04 19:13:31
【问题描述】:
我正在尝试创建一个真正的灰度图像,但是当我的设计师检查文件时,他说图像仍然是 RGB。
目前我已经尝试了两种方法:
1) 使用 MakeGrayScale3 方法:
public static Image MakeGrayscale3(Image original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
newBitmap.SetResolution(original.HorizontalResolution, original.VerticalResolution);
//get a graphics object from the new image
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
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);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
2) 通过以下代码使用 ImageMagick.Net 库:
using (MagickImage image = new MagickImage(file))
{
image.ColorSpace = ColorSpace.GRAY;
image.Write(originalFile);
}
如果有人之前遇到过这个问题,或者知道如何改变它..
谢谢!
原图:
结果图像(ImageMagick):
结果图像(MakeGrayscale3):
【问题讨论】:
-
你使用的是什么文件格式,他是如何检查图像的(我看不到你的截图)? Photoshop 有一些图像配置文件,对于 jpeg 文件,这些配置文件始终是 RGB,即使它只包含灰色阴影。如果是 Photoshop,那么我认为您需要将文件另存为 .PSD 文件并以某种方式设置配置文件以使其显示为灰度图像。
-
你能发布结果图像吗?我想尝试用 Matlab 或其他软件打开它,以确保它是 RGB 的图像,而不是告诉你是 RGB 的 PS。
-
他在 Photoshop(我已经添加了屏幕截图)和 FastStone Photo Resizer(光度选项)上检查图像。
-
我刚刚用最新版本尝试了你的 Magick.NET 示例,我得到了一张只有一个通道并且是灰色的图像。您使用的是最新版本的 Magick.NET 吗?
标签: c# image image-processing