1

In continuation from a previous SO post here I've gone from being able to remove the transparency from a PDF conversion, to not being able to adjust the background color of the conversion. I've tried everything that I can find on the Imagemagick.NET github docs. I need to ensure that ANY image that is passed through this software package has a non transparent white background.

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

In the code above, if I set any of the 4 channels image.Settings.BackgroundColor to a different color, it has no affect on the image. If I use image.BackgroundColor it has no affect on the image. What am I missing?

Note: in the above code I am setting the colors to black to verify the code is working. I've tried other colors as well for giggles.

addohm
  • 2,248
  • 3
  • 14
  • 40

2 Answers2

0

Change your settings from:

settings.Density = new Density(600);

to,

settings.Density = new Density(600);
settings.ColorType = ColorType.TrueColor;
settings.BackgroundColor = new MagickColor(Color.White);

I have already tried this and works perfectly.

MRizwan
  • 1
  • 2
0

I had a similar issue when converting a PDF file to a set of images. For me, what resolved was not to use the settings object (except for density) and apply the desired properties (Format, BackgroundColor, ColorType) to the images, instead.

magickReadSettings = new MagickReadSettings();
magickReadSettings.Density = new Density(300);

using (var images = new MagickImageCollection())
{
    // Add all the pages of the pdf file to the collection
    await images.ReadAsync(pdf, magickReadSettings);

    foreach (IMagickImage<ushort> image in images)
    {
        image.Format = MagickFormat.Png;
        image.BackgroundColor = new MagickColor(255, 255, 255, 255);
        image.ColorType = ColorType.Grayscale;
    }
}
Starnuto di topo
  • 3,215
  • 5
  • 32
  • 66