3

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

我看过其他帖子,其中命令行版本的 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;
4

1 回答 1

8

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

image.Alpha(AlphaOption.Remove);
于 2018-11-01T00:57:07.573 回答