Converting PDF to images using ImageMagick.NET - how to set the DPI

后端 未结 3 1161
悲哀的现实
悲哀的现实 2020-12-08 05:51

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

相关标签:
3条回答
  • 2020-12-08 06:23

    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 the 3rd page, index 0 is the first
    

    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.

    0 讨论(0)
  • 2020-12-08 06:36

    Set density in MagickReadSettings before you read.

                MagickImage image = new MagickImage();
                MagickReadSettings settings = new MagickReadSettings();
                settings.Density = new Density(1000);
                image.Read(file,settings);    
    
    0 讨论(0)
  • 2020-12-08 06:41

    Updating reference, I founded an .NET wrapper on official ImageMagick website.

    Source: https://github.com/dlemstra/Magick.NET

    0 讨论(0)
提交回复
热议问题