This project has moved. For the latest updates, please go here.

Converting a PDF to JPG and then scaling turns the image black

May 14, 2014 at 9:11 PM
My code to convert a PDF file to JPG works great, but if I try to use images[0].Scale(50.0,50.0); where images is an ImageCollection and images[0] is an Image that appears fine when written; it will scale my image down, but it turns the majority of the image solid black. Is there a setting I can set or better way for resizing images based on percentage that are in an ImageCollection?
Coordinator
May 15, 2014 at 5:14 AM
Can you post a link to your pdf file so I can reproduce your issue?
May 15, 2014 at 1:36 PM
This happens with every PDF I try. Here's an example:

http://msiatlanta.com/kb/index.php?View=file&EntryID=3
        MagickReadSettings settings = new MagickReadSettings();
        settings.Density = new MagickGeometry(144, 144);
        foreach(string PDF in PDFs)
        {
            using(MagickImageCollection images = new MagickImageCollection())
            {
                images.Read(PDF, settings);
                int page = 1;
                foreach (MagickImage image in images)
                {
                    image.Write(PDF.Substring(0, PDF.Length - 4)+" Page "+page+".jpg");
                    page++;
                }
                images[0].Scale(95.0, 95.0);
                images[0].Write(PDF.Substring(0, PDF.Length - 4) + " High.jpg");
            }
        }
This code creates images of each PDF page and creates a scaled down image of the first page. However, the scaled down image turns black like so:

http://i.imgur.com/GuoWM0g.png
Coordinator
May 15, 2014 at 5:55 PM
This is happening because the background of your PDF is transparent. When you save it as JPEG this will turn black for your PDF's. You should change the transparent pixels to a color with the ColorAlpha method:
foreach (MagickImage image in images)
{
  image.ColorAlpha(new MagickColor("#fff"));
  image.Write(fileName.Substring(0, fileName.Length - 4) + " Page " + page + ".jpg");
  page++;
}