我们的客户之一需要将PDF运输标签转换为PNG图像。PDF图像需要300 DPI,有一点深度为1(纯黑白无灰度)。
我已经开始工作了,但是有些问题我找不到解决的办法。
我的代码
MagickNET.SetGhostscriptDirectory(_ghostscriptPath);
var settings = new MagickReadSettings();
settings.Density = new Density(_dpi);
//settings.ColorType = ColorType.Bilevel; // Not working
//settings.Depth = 1; // Not working
//settings.SetDefine(MagickFormat.Png, "Bit-depth", "1"); // Not working
using (var images = new MagickImageCollection())
{
images.Read(sourceFilePath, settings);
using (var horizontal = images.AppendHorizontally())
{
//horizontal.Density = new Density(_dpi); // Not working
horizontal.BitDepth(1); // Testing (Sometimes commented out)
horizontal.Write(targetPath_working);
}
}结果
当使用以下设置(BitDepth(1) +300DPI)运行此代码时,PNG映像为1169x2303和(4Bit深度)。
当使用以下设置(删除BitDepth(1) +300DPI)运行此代码时,PNG映像为1169x2303和(32位深度)。
这给了我两个主要问题,当BitDepth设置为1时,为什么PNG映像仍然是4位?其次,该4位图像的质量是可怕的和无法读的条形码扫描仪。感觉就像在写作过程中,图像在某种程度上被调整。我需要有32位图像的“锐度”,但作为1位。
有人能指点我在这里的正确方向,感觉我缺乏图像转换的诀窍。
谢谢!
PS:我正在使用Magick.NET-Q8-AnyCPU (7.23.3)
测试建议1
结果在以黑线为边框的图像中,没有一个文本被填充为黑色,但是图像现在与预期的一样是1位。
MagickNET.SetGhostscriptDirectory(_ghostscriptPath);
var settings = new MagickReadSettings();
settings.Density = new Density(_dpi);
settings.SetDefine(MagickFormat.Png, "Bit-depth", "1");
using (var images = new MagickImageCollection())
{
images.Read(sourceFilePath, settings);
using (var horizontal = images.AppendHorizontally())
{
horizontal.AutoLevel();
horizontal.Threshold(new Percentage(50));
horizontal.Write(targetPath_working);
}
}发布于 2021-06-29 11:16:34
为我的问题找到解决方案,请参阅下面的代码。为了取得好的结果,必须结合使用许多不同的设置。
MagickNET.SetGhostscriptDirectory(_ghostscriptPath);
var settings = new MagickReadSettings();
settings.Density = new Density(_dpi);
settings.SetDefine(MagickFormat.Png, "Bit-depth", "1");
using (var images = new MagickImageCollection())
{
images.Read(sourceFilePath, settings);
using (var horizontal = images.AppendHorizontally())
{
horizontal.AutoLevel();
horizontal.Threshold(new Percentage(50));
horizontal.ColorType = ColorType.Bilevel;
horizontal.Format = MagickFormat.Png8;
horizontal.Write(targetPath_working);
}
}https://stackoverflow.com/questions/68111319
复制相似问题