imagemagickpdf-conversionghostscript.netbit-depthmagicknet

MagicNET convert PDF to black and white (1bit) PNG


One of our customers need to convert PDF shipping labels to PNG images. The PDF images need to be 300 DPI and have a bit depth of 1 (pure black and white without grayscale).

I have got this working but with some issues that i can not find any solution for.

My Code

MagickNET.SetGhostscriptDirectory(_ghostscriptPath);
            var settings = new MagickReadSettings();
            settings.Density = new Density(_dpi);
            //settings.ColorType = ColorType.Bilevel;                 // Not working
            //settings.Depth = 1;                                     // Not working
            //settings.SetDefine(MagickFormat.Png, "Bit-depth", "1"); // Not working

            using (var images = new MagickImageCollection())
            {
                images.Read(sourceFilePath, settings);
                using (var horizontal = images.AppendHorizontally())
                {
                    //horizontal.Density = new Density(_dpi);         // Not working
                    horizontal.BitDepth(1);                           // Testing (Sometimes commented out)
                    horizontal.Write(targetPath_working);
                }
            }

Result

When this code is run with the following setup (BitDepth(1) + 300 DPI) the PNG image is 1169x2303 and (4Bit depth).

When this code is run with the following setup (Removed BitDepth(1) + 300 DPI) the PNG image is 1169x2303 and (32Bit depth).

This gives me 2 primary issues, when BitDepth is set to 1 why is the PNG image still 4bit? Second the quality of that 4bit image is horrible and unreadable by a barcode scanner. It feels like the image is somehow being resized during the writing process. I would need to have the "sharpness" of the 32 bit image but as 1 bit.

Could someone point me in the right direction here, feels like i am lacking the knowhow of image conversion.

Thank you!

PS: I'm using Magick.NET-Q8-AnyCPU (7.23.3)

Test suggestion 1

Results in image with black lines as borders for everything, none of the text is filled with black however the image is now 1bit as expected.

MagickNET.SetGhostscriptDirectory(_ghostscriptPath);
            var settings = new MagickReadSettings();
            settings.Density = new Density(_dpi);
            settings.SetDefine(MagickFormat.Png, "Bit-depth", "1");

            using (var images = new MagickImageCollection())
            {
                images.Read(sourceFilePath, settings);
                using (var horizontal = images.AppendHorizontally())
                {
                    horizontal.AutoLevel();
                    horizontal.Threshold(new Percentage(50));
                    horizontal.Write(targetPath_working);
                }
            }

Solution

  • Found solution for my issue see code below. Had to use a number of different settings in combination for a good end result.

    MagickNET.SetGhostscriptDirectory(_ghostscriptPath);
                var settings = new MagickReadSettings();
                settings.Density = new Density(_dpi);
                settings.SetDefine(MagickFormat.Png, "Bit-depth", "1");
    
                using (var images = new MagickImageCollection())
                {
                    images.Read(sourceFilePath, settings);
                    using (var horizontal = images.AppendHorizontally())
                    {
                        horizontal.AutoLevel();
                        horizontal.Threshold(new Percentage(50));
                        horizontal.ColorType = ColorType.Bilevel;
                        horizontal.Format = MagickFormat.Png8;
                        horizontal.Write(targetPath_working);
                    }
                }