views:

470

answers:

1

Hi, I'm trying to convert pdf files to images. ImageMagick is a great tool, and using the command line tool gets me desired result.

but i need to do this in my code, So added a reference to http://imagemagick.codeplex.com/ And the following code sample renders each page of the pdf as an image:

MagickNet.InitializeMagick();
using (ImageList im = new ImageList())
{
    im.ReadImages(@"E:\Test\" + fileName + ".pdf");
    int count = 0;
    foreach (Image image in im)
    {
        image.Quality = 100;
        image.CompressType = mageMagickNET.CompressionType.LosslessJPEGCompression;
        image.Write(@"E:\Test\" + fileName + "-" + count.ToString() + ".jpg");
        ++count;
    }
}

The problem: IT LOOKS LIKE CRAP the rendered image is hardly readable. the problem i realized is it uses the default 72 DPI of ImageMagick. and i can't find a way to set it(96dpi or 120dpi gives good results) via the .Net wrapper.

Am I missing something , or there is really no way to set it via this wrapper?

Thanks

+1  A: 

I had a brief look into this.

The Image.Resolution property can be used to set the PDF rendering resolution but that property is not exposed by the ImageMagick.NET wrapper.

Adding the missing property to the Image class is simple enough.

Index: ImageMagickNET/Image.h
===================================================================
--- ImageMagickNET/Image.h  (revision 59374)
+++ ImageMagickNET/Image.h  (working copy)
@@ -532,6 +532,13 @@
        }


+       // Vertical and horizontal resolution in pixels of the image.
+       property Geometry^  Density
+       {
+           void set(Geometry^);
+       }
+
+
        //----------------------------------------------------------------
        // IO
        //----------------------------------------------------------------
Index: ImageMagickNET/Image.cpp
===================================================================
--- ImageMagickNET/Image.cpp    (revision 59374)
+++ ImageMagickNET/Image.cpp    (working copy)
@@ -1099,5 +1099,9 @@
        return bitmap;
    }

+   void Image::Density::set(Geometry^ density_)
+   {
+       image->density(*(density_->geometry));
+   }
 }

Unfortunately it seems that a bug prevents us from setting the rendering quality while iterating through the PDF pages as you're attempting to do.

Another option would be to open each page separately:

Image image = new Image();
image.Density = new Geometry("1000");  // 1000 dpi
image.Read(@"C:\u\test.pdf[2]");       // Open page 2

If the page number is out of range you get a raw C++ exception. While you can catch it in C# the wrapper should probably include a .NET exception class for representing ImageMagick errors.

Alexandre Jasmin
Thanks for looking into this,tried to compile the sources as published at the http://www.codeproject.com/KB/dotnet/ImageMagick_in_VBNET.aspx post(couldn't find the sources at http://imagemagick.codeplex.com/ ... probably missing something)I am working with framework 4.0, and the original wrapper is written for 2.0, and it seems i did something wrong, since it compiled but there was a runtime error ouch..so looked for another solution(don't mind paying for a good solution)found Two1. Aspose.pdf.kit very very slow and expensive2. O2solutions pdfview4net - fast and afordable :-)
Avi Pinto
Couldn't find a source tarball of ImageMagick.NET on Codeplex. I had to checkout the code from source control... In case you're still looking for alternatives I suggest you consider using either the [Ghostscript](http://ghostscript.com/) or [Poppler](http://poppler.freedesktop.org/) library directly. These are the two prevalent opensource PDF rendering libraries. Not sure if good .NET wrappers are available though.
Alexandre Jasmin