使用 ImageMagick.NET 将 PDF 转换为图像 - 如何设置 DPI [英] Converting PDF to images using ImageMagick.NET - how to set the DPI

查看:33
本文介绍了使用 ImageMagick.NET 将 PDF 转换为图像 - 如何设置 DPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 pdf 文件转换为图像.ImageMagick 是一个很棒的工具,使用命令行工具可以得到我想要的结果.

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

但我需要在我的代码中做到这一点,所以添加了对 http://imagemagick.codeplex.com/ 的引用以下代码示例将 pdf 的每一页呈现为图像:

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;
    }
}

问题:看起来像废话渲染的图像几乎不可读.我意识到的问题是它使用 ImageMagick 的默认 72 DPI.而且我找不到通过 .Net 包装器设置它的方法(96dpi 或 120dpi 给出了很好的结果).

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?

谢谢

推荐答案

我对此进行了简要研究.

I had a brief look into this.

Image.Resolution 属性可用于设置 PDF 渲染分辨率,但 ImageMagick.NET 包装器不会公开该属性.

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

将缺失的属性添加到 Image 类很简单.

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));
+   }
 }

很遗憾,一个错误 阻止我们在您尝试执行的过程中迭代 PDF 页面时设置渲染质量.

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	est.pdf[2]");       // Open the 3rd page, index 0 is the first

如果页码超出范围,您会收到 raw C++ 异常.虽然 您可以在 C# 中捕获它,但包装器应该可能包含一个 .NET 异常类来表示 ImageMagick 错误.

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.

这篇关于使用 ImageMagick.NET 将 PDF 转换为图像 - 如何设置 DPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆