如何设置DPI - 使用ImageMagick.NET PDF转换为图像 [英] Converting PDF to images using ImageMagick.NET - how to set the 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/ $参照b $ b和下面的代码示例呈现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;
}
}
问题:IT看起来像废话
渲染的图像几乎没有可读性。
的问题,我意识到它是使用默认的DPI 72 ImageMagick的的。
,我无法找到一种方法,通过.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.
添加缺少的属性,以图像类是很简单的。
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\test.pdf[2]"); // Open the 3rd page, index 0 is the first
如果页码超出范围你得到的原始的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.
这篇关于如何设置DPI - 使用ImageMagick.NET PDF转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!