首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MagicNET将PDF转换为黑白(1位) PNG

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

Stack Overflow用户
提问于 2021-06-24 07:28:02
回答 1查看 283关注 0票数 0

我们的客户之一需要将PDF运输标签转换为PNG图像。PDF图像需要300 DPI,有一点深度为1(纯黑白无灰度)。

我已经开始工作了,但是有些问题我找不到解决的办法。

我的代码

代码语言:javascript
复制
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位。

代码语言:javascript
复制
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);
                }
            }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-29 11:16:34

为我的问题找到解决方案,请参阅下面的代码。为了取得好的结果,必须结合使用许多不同的设置。

代码语言:javascript
复制
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);
                }
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68111319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档