尝试打开图像,设置其分辨率并保存它,但我遇到了一个问题,即在我没有任何输入的情况下修改
ColorSpace
。
输入图像是一张空白的sRGB/8图像,创建后检查
img.ColorSpace
显示sRGB
,但是在设置密度,将图像写入磁盘并打开输出后,ColorSpace
设置为Gray
.
using (var img = new MagickImage(inputImagePath))
{
img.Density = new Density(parameters.HorizontalResolution, parameters.VerticalResolution);
img.Write(outputImagePath);
// img.ColorSpace is sRGB
}
using (var imgCheck = new MagickImage(outputImagePath))
{
// imgCheck.ColorSpace is gray
}
下游代码要求输出图像的色彩空间与输入图像的色彩空间相同。是否有
MagickImage.Write()
的参数可以保留颜色空间?
在回顾这个相关问题后:ImageMagick: convert keeps changing the colorspace to Gray.如何保留 sRGB 色彩空间? 如果图像的格式设置为
MagickFormat.Png
,并且其内容为灰度,则 ImageMagick 会将色彩空间设置为灰度。
using (var img = new MagickImage(inputImagePath))
{
img.Density = new Density(parameters.HorizontalResolution, parameters.VerticalResolution);
img.Write(outputImagePath, MagickFormat.Png00);
// img.ColorSpace is sRGB
}
using (var imgCheck = new MagickImage(outputImagePath))
{
// imgCheck.ColorSpace is gray
}
在
Write()
上手动设置 MagickFormat 到 Png00
保留输入的颜色空间和每像素位数设置。