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

Convert a pdf page to image

May 20, 2013 at 8:10 AM
Good morning,

I'm new with Magick.net. I'm developing an application and I have a pdf page which I want to convert to image (jpg,jpeg,bmp,tiff,png,etc) and I don't know how. Is it possible to do it with Magick.Net ?

Waiting for your help

Thanks a lot
Coordinator
May 20, 2013 at 4:12 PM
That is possible. To read pdf's you need to install GhostScript (http://www.ghostscript.com/download/). I already posted an example here: https://magick.codeplex.com/discussions/443658
May 20, 2013 at 7:18 PM
Thank you for your answer dlemstra.

I have already Ghostscript installed.

I'll try the code in the discussion and hope it'll work.

Thanks
May 20, 2013 at 7:55 PM
I tried the code , and each time I run , I've got an error.

I've created this function :

region PDF to image conversion

     private void pdfConvert(string file, string destFolder)
    {
        using (var list = new MagickImageCollection())
        {
            list.Read(file);
            for (int id = 0; id < list.Count; id++)
            {
                using (MagickImage image = new MagickImage())
                {
                    image.Density = new MagickGeometry(144, 144);
                    image.Read(file + "[" + id + "]");
                    image.Quality = 80;
                    image.Write(destFolder + "img" + id + ".jpg");
                }
            }
        }
    }
    #endregion
After that I called the function as follow :

if (openFileDialog1.ShowDialog(this) != DialogResult.OK) return;
            ProjectName = openFileDialog1.FileName;
            string ProjectType = Path.GetExtension(ProjectName);                    
        if (ProjectName.EndsWith(".pdf"))
            {
                pdfConvert(ProjectName, "C:\\");
            }
and this is the error : File Not found Exception handlel.Could not load file or assembly 'Magick.NET.dll' or one of its dependencies.

I really don't know what to do . Please help and sorry for bothering you
Coordinator
May 20, 2013 at 8:22 PM
Did you call MagickNET.Initialize() at the start of your program as explained here? The new release has been publish so you could use this code:
using (MagickImageCollection collection = new MagickImageCollection())
{
  MagickReadSettings settings = new MagickReadSettings();
  settings.Density = new MagickGeometry(144, 144);

  collection.Read(file, settings);

  int id = 0;
  foreach (MagickImage image in collection)
  {
    image.Write(destFolder + "img" + id + ".png");
    id++;
  }
}
May 22, 2013 at 10:37 AM
Hi delmstra

I followed the tutorial and some error occurs and until now I have the same mistake.

Could not load file or assembly 'Magick.NET.dll' or one of its dependencies
May 22, 2013 at 10:41 AM
I don't know if I'm missing something or not.

These are the steps that I followed.
  1. download Magick.Net package "Magick.NET-6.8.5.401-Q16-x86-net40-client"
  2. un-zip the folder
  3. add a reference to Magick.dll to myproject
  4. call the initialize method, I called it in my Program.cs or my Form() (costructor) and always getting the same error.
Coordinator
May 22, 2013 at 11:17 AM
Did you install 'Visual C++ Redistributable for Visual Studio 2012 x86'?
May 22, 2013 at 1:10 PM
Now I got it.

I'm working on Visual Studio 2010 Ultimate, x86 . So I think I can't make it work, is that right ?!
Coordinator
May 22, 2013 at 1:18 PM
It won't work unless you install Visual C++ Redistributable for Visual Studio 2012 x86 (http://www.microsoft.com/en-us/download/details.aspx?id=30679).
May 22, 2013 at 1:29 PM
Thank you so much dlemstra , you're so kind , thank you so much for your help.

So good, I installed it and now when I build the project there's no error.

I have another question : after converting the pdf page into an image , how can I call the image and use it ?! Do I have to call "collection" or what ?
Coordinator
May 22, 2013 at 2:54 PM
What do you mean by calling and using it? When you write the image you have an object that is the image. You can use the variable 'image' to perform other actions.
May 22, 2013 at 3:07 PM
The conversion works fine now and this is the function I used to do the conversion action and at the same time it returns to me the new name of the converted image

private string convertPdf(string file)
     {
         string newPathImage = "";

         MagickImageCollection collection = new MagickImageCollection();

         MagickReadSettings settings = new MagickReadSettings();

         settings.Density = new MagickGeometry(144, 144);

         collection.Read(file, settings);

         int id = 0;

         foreach (MagickImage image in collection)
          {
            image.Write(file+ "img" + id + ".png");

            newPathImage = image.ToString();

             id++;
          }

          return newPathImage;
     }
the problem when I want to open newPathImage in other action , an error occur "can not open the file "imagepdf.pdfimg0.png"
Coordinator
May 22, 2013 at 4:24 PM
image.ToString() will not return the filename. 'file+ "img" + id + ".png"' is the name of the file.

The problems you are running into are not really issues related to Magick.NET or ImageMagick. I think you should look for more help somewhere else.
May 22, 2013 at 4:28 PM
I found the solution for the problem and I want to thank you for your help.

Also I want to apologyze if I bothered you.
May 25, 2013 at 11:20 AM
Edited May 25, 2013 at 11:21 AM
Hi Dlemstra,

thanks a lot.... your code is working perfectly...but some of the images are generated with light gray shade in the background of the color. In my pdf file the corresponding page background is pure while...

Not all the page are got that prolem. only some of the images are came up with the light gray shadow on the background....

Could you please tell what is the problem on that?
Coordinator
May 25, 2013 at 3:03 PM
Is this a random problem or can you reproduce it? Setting the background property to white might solve your problem. Can you send me a pdf that will produce images with a grey background?
May 28, 2013 at 6:15 AM
whenever i am setting density of the image into minimal one then, some of the images are coming like that... for example like that...

MagickReadSettings settings = new MagickReadSettings();
settings.Density = new MagickGeometry(15, 15); // Reducing image size



(2) And one more problem i encounted while testing with Windows 7 machine...

The Error Message is : An Unhandled exception of type System.StackOverflowException occurred in Magick.NET.DLL


Could you please tell me what is the problem here....

I am using the same copy of the dll files and folders that are used in the windows xp machine...
Coordinator
May 28, 2013 at 6:46 AM
Can you provide me with a code example that causes the StackOverflowException? And are you able to send me a pdf that i can use to test the grey background?
May 28, 2013 at 6:58 AM
am very sorry... i think it is random problem... i checked it now... but it is working fine...

Stackoverflow exception :
 string inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "agreement.pdf");
 string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "img-output\\");
            
            using (MagickImageCollection collection = new MagickImageCollection())
            {
                 collection.Read(inputFile);  // here the problem occrured...

                int id = 1;
                foreach (MagickImage image in collection)
                {
                    image.Write(outputFile + "img" + id + ".png");
                    id++;
                }
            }
Coordinator
May 28, 2013 at 7:07 AM
Edited May 28, 2013 at 7:07 AM
Can you provide me with a link to the agreement.pdf document?
May 28, 2013 at 8:09 AM
@dlemstra : I am very sorry... It is one of our client document and more secure... So, i can't share with you... Please dont mistake me...

Anyway if i encounter the same with some other document means i will share with you...

Can you tell me reason for that stackoverflow exception...
Coordinator
May 28, 2013 at 10:21 AM
I am unable to reproduce the problem without your pdf. I understand that you cannot share it but without the document I cannot reproduce the stackoverflow. Can you supply me with a stacktrace? What version of Magick.NET are you using? You can use MagickNET.Version to determine the version.
May 28, 2013 at 11:21 AM
version is : Magick.NET-6.8.5.401-Q16-x86-net40-client and Magick.NET.Web-6.8.5.401-x86-net40
Coordinator
May 28, 2013 at 1:45 PM
Edited May 28, 2013 at 1:45 PM
Can you also post the stacktrace? I don't have enough information to investigate the issue.