0

I have created a small .net core 3.1 console application to convert pdf to png using Magick.Net (https://www.nuget.org/packages/Magick.NET-Q16-AnyCPU/). I have also installed ghostscript public license. It's doing alright but the output image is totally black and I can't read a single line. If I open the file using Paint then it's fine. Here is my code:

        public static int ConvertFileToImages(string filePath, string destinationPath)
        {
            int noofPages = 0;
            MagickReadSettings magickReadSettings = new MagickReadSettings();
            magickReadSettings.Density = new Density(300, 300);
            using (MagickImageCollection magickImageCollection = new MagickImageCollection())
            {
                magickImageCollection.Read(filePath, magickReadSettings);
                int page = 1;
                noofPages = magickImageCollection.Count;
                foreach (MagickImage magickImage in magickImageCollection)
                {
                    magickImage.Format = MagickFormat.Png;
                    magickImage.BackgroundColor = MagickColors.White;
                    string imageFilePath = string.Concat(destinationPath, "file-", page, ".png");
                    magickImage.Write(imageFilePath);
                    page++;
                }
            }
            return noofPages;
        }

Am I doing anything wrong?

Ask
  • 3,076
  • 6
  • 30
  • 63
  • It would be useful to put the PDF file somewhere. Which version of Ghostscript have you installed ? Do you get anything passed back on stderr/stdout ? – KenS Jul 09 '20 at 12:29
  • I used Ghostscript 9.52 for Windows (64 bit) (https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs952/gs952w64.exe) – Ask Jul 09 '20 at 12:35
  • Well, without having the PDF file I can't try any of this, so there's not a lot I can say. – KenS Jul 09 '20 at 13:11
  • @KenS, you can see this file https://drive.google.com/file/d/1RfP4dApPQYu7Ak-7QNZ_afoxn4xjwmLR/view?usp=sharing. When I execute the above mentioned code on this file. It generates a png but with complete black background – Ask Jul 09 '20 at 13:22
  • Tried it with Ghostscript and this command line: "c:\program files\gs\gs9.52\bin\gswin64c.exe" -r300 -sDEVICE=png16m -o out.png test_pdf.pdf and the resulting PNG file isn't black for me. So I don't think this is a Ghostscript problem. You should be able to find out what ImageMagick is using for a Ghostscript command line, but I'm afraid I can't help you with that. I'm puzzled by your comment 'if I open the file using Paint its fine'. Open which file ? The PNG ? If so then where is it all black ? – KenS Jul 09 '20 at 14:21
  • Yes, the png file. It's black when I tried opening it with default windows photo viewer – Ask Jul 10 '20 at 10:11
  • Well if Paint can open it without problems, then it sounds to me like the windows default photo viewer is where the bug lies. I don't think this shows any problems with the output from Ghostscript. – KenS Jul 10 '20 at 10:36

0 Answers0