首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将颜色应用于ImageMagick.NET中的PDF转换

将颜色应用于ImageMagick.NET中的PDF转换
EN

Stack Overflow用户
提问于 2018-11-01 10:23:28
回答 1查看 341关注 0票数 0

在之前的一篇文章中,我从能够删除here转换中的透明度,到不能调整转换的背景色。我在Imagemagick.NET github docs上找过的都试过了。我需要确保通过此软件包的任何图像都具有不透明的白色背景。

代码语言:javascript
复制
    /// <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())
            {
                // Remove the transparency layers and color the background white
                image.Alpha(AlphaOption.Remove);

                int aval = image.Settings.BackgroundColor.A = 0;
                int rval = image.Settings.BackgroundColor.R = 0;
                int bval = image.Settings.BackgroundColor.G = 0;
                int gval = image.Settings.BackgroundColor.B = 0;

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

在上面的代码中,如果我将4个通道image.Settings.BackgroundColor中的任何一个设置为不同的颜色,它对图像没有任何影响。如果我使用image.BackgroundColor,它对图像没有影响。我遗漏了什么?

注意:在上面的代码中,我将颜色设置为黑色,以验证代码是否正常工作。我也试过其他颜色的傻笑。

EN

回答 1

Stack Overflow用户

发布于 2019-06-14 20:11:54

从以下位置更改您的设置:

代码语言:javascript
复制
settings.Density = new Density(600);

致,

代码语言:javascript
复制
settings.Density = new Density(600);
settings.ColorType = ColorType.TrueColor;
settings.BackgroundColor = new MagickColor(Color.White);

我已经试过了,效果很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53094420

复制
相关文章

相似问题

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