3

A part of a software package I've written takes in a binary encoded PDF, converts it to a Bitmap, and saves it to a blob in a db. I'm discovering that although it's not immediately visible in the original in it's PDF form, there is transparency in the document that is completely visible in the converted bitmap.

I've seen other posts where the command line version of imagemagick can "flatten" an image to remove the transparency layers. Can this somehow be done with the .NET package? How do I remove the transparency before saving the bitmap? I need to keep it as a bitmap as it's a universally recognized format for their label printers.

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

I have tried to use different methods of adjusting the transparency layers, or coloring of the background without success.

            //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;
addohm
  • 1,593
  • 2
  • 9
  • 28

1 Answers1

8

Try removing the Alpha component, and set the background color depending on the output:

image.Alpha(AlphaOption.Remove);
Russell
  • 16,427
  • 22
  • 77
  • 121
  • That seems to have done it. Changing the background color isn't working though. What am I missing there? – addohm Nov 01 '18 at 01:02
  • try image.Settings.BackgroundColor – Russell Nov 01 '18 at 01:04
  • I did, that's one of the things listed in the OP. Just tried it again for good measure. `image.Settings.BackgroundColor.R = 65535` and I still see white. – addohm Nov 01 '18 at 01:05
  • To elaborate, I am just trying to always force a white background. – addohm Nov 01 '18 at 01:06
  • image.Settings.BackgroundColor = Color.White – Russell Nov 01 '18 at 01:08
  • `image.Settings.BackgroundColor = Color.Black;` has no affect on the output image. (I'm using `Color.Black` to check) – addohm Nov 01 '18 at 01:12
  • The removing of alpha behind the scenes is using the background color to determine "transparency" which is probably what you are seeing - try setting ALL background color values at the same time (including the ones above) to see what you need to do – Russell Nov 01 '18 at 01:14
  • I notice that `Color.Black` colors the A channel of ARGB. I'll play around with it some more. – addohm Nov 01 '18 at 01:15