【问题标题】:Removing Transparency using Imagemagick.NET使用 Imagemagick.NET 去除透明度
【发布时间】:2019-04-05 05:43:07
【问题描述】:

我编写的软件包的一部分接收二进制编码的 PDF,将其转换为位图,并将其保存到数据库中的 blob。我发现虽然它在 PDF 形式的原始文件中不会立即可见,但在转换后的位图中完全可见文档中的透明度。

我看到other posts 的命令行版本的 imagemagick 可以“拼合”图像以移除透明层。这可以通过 .NET 包以某种方式完成吗?如何在保存位图之前删除透明度?我需要将其保留为位图,因为它是标签打印机普遍认可的格式。

    /// <summary>
    /// Write image data from a pdf file to a bitmap file
    /// </summary>
    /// <param name="imgData"></param>
    private static void convertPdfToBmp(ImageData imgData)
    {
        MagickReadSettings settings = new MagickReadSettings();
        // Settings the density to 600 dpi will create an image with a better quality
        settings.Density = new Density(600);

        using (MagickImageCollection images = new MagickImageCollection())
        {
            // Add all the pages of the pdf file to the collection
            images.Read(imgData.pdfFilePath, settings);

            // Create new image that appends all the pages horizontally
            using (IMagickImage image = images.AppendVertically())
            {
                // Convert the image to a bitmap
                image.Format = MagickFormat.Bmp;

                // Delete any old file 
                if (File.Exists(imgData.bmpFilePath))
                {
                    File.Delete(imgData.bmpFilePath);
                }
                // Save result as a bmp
                image.Write(imgData.bmpFilePath);
            }
        }
    }

我尝试使用不同的方法来调整透明度图层或背景着色,但均未成功。

            //image.BackgroundColor = new ColorMono(false);
            //image.TransparentChroma(new MagickColor(0, 0, 0), new MagickColor(0, 0, 0));
            //image.TransparentChroma(new ColorRGB(0, 0, 0), new ColorRGB(65535, 65535, 65535));
            //image.BackgroundColor = new MagickColor("#fff");
            //image.BackgroundColor = new MagickColor("#ffffff");
            //image.BackgroundColor = new MagickColor("#ffffffffffff");
            //image.Settings.BackgroundColor.A = 0;

【问题讨论】:

    标签: c# .net imagemagick.net


    【解决方案1】:

    尝试移除 Alpha 组件,并根据输出设置背景颜色:

    image.Alpha(AlphaOption.Remove);
    

    【讨论】:

    • 似乎已经做到了。但是更改背景颜色不起作用。我在那里错过了什么?
    • 试试 image.Settings.BackgroundColor
    • 我做到了,这是 OP 中列出的内容之一。只是再次尝试了很好的措施。 image.Settings.BackgroundColor.R = 65535 我仍然看到白色。
    • 详细说明,我只是想始终强制使用白色背景。
    • image.Settings.BackgroundColor = Color.White
    猜你喜欢
    • 2013-04-14
    • 2014-09-08
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 2020-10-04
    相关资源
    最近更新 更多