MagicNET 将 PDF 转换为黑白 (1bit) PNG

问题描述

我们的一位客户需要将 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) + 300 DPI)运行此代码时,PNG 图像为 1169x2303 和(4Bit 深度)。

当使用以下设置(移除 BitDepth(1) + 300 DPI)运行此代码时,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,"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);
                }
            }

解决方法

为我的问题找到了解决方案,请参阅下面的代码。必须结合使用多种不同的设置才能获得良好的最终结果。

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);
                }
            }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...