如何使用C#和ImageMagick将PDF转换为图像?

如何使用C#和ImageMagick将PDF转换为图像?,c#,.net,imagemagick,magicknet,C#,.net,Imagemagick,Magicknet,我想使用C#和magicknet.dll将PDF文件转换为.GIF。我已将对MagickNet Dll的引用添加到我的项目中 MagickNet.Magick.Init(); MagickNet.Image img = new MagickNet.Image("d:/aa.pdf"); img.Write("d:/bb.gif"); MagickNet.Magick.Term(); img.Dispose(); 用户代码未处理System.Runtime.InteropServices.Seh

我想使用C#和magicknet.dll将PDF文件转换为.GIF。我已将对MagickNet Dll的引用添加到我的项目中

MagickNet.Magick.Init();
MagickNet.Image img = new MagickNet.Image("d:/aa.pdf");
img.Write("d:/bb.gif");
MagickNet.Magick.Term();
img.Dispose();
用户代码未处理System.Runtime.InteropServices.SehexException Message=“外部组件已引发异常。” Source=“ImageMagickNET” 错误代码=-2147467259 堆栈跟踪: 在Magick.Image.{ctor}(Image*,basic_string\,std::allocator>*) 在ImageMagickNET.Image..ctor(字符串imageSpec) 在测试1中,单击C:\Users\PANKAJ\Documents\Visual Studio 2008\Projects\Test1\Test1\Default.aspx.cs中的(对象发送方,事件参数e):第31行 在System.Web.UI.WebControls.Button.OnClick(EventArgs e)中 位于System.Web.UI.WebControl.Button.RaisePostBackEvent(String eventArgument) 位于System.Web.UI.WebControl.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串事件参数) 位于System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument) 位于System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint) 内部异常:
ImageMagick需要GhostScript来解释PDF文件。如果您愿意,可以直接调用GhostScript dll(通过我的个人资料与我联系,我会给您发送一个c#包装器)

或者,您可以使用GhostScript命令行或商业第三方组件,例如来自Tall Components的PDF库。

Magic.Net是流行的库ImageMagick的C#端口。使用url中的Nuget软件包安装Magick.net。这样你就可以使用C。请参阅下面的代码

注意,它将垂直附加图像。类似地,您可以水平追加,即替换图像。水平追加

using ImageMagick;

string inputPdf= @"C:\my docs\input.pdf";
string outputPng= @"C:\my docs\output.png";

using (MagickImageCollection images = new MagickImageCollection())
{
    images.Read(inputPdf);
    using (IMagickImage vertical = images.AppendVertically())
        {
            vertical.Format = MagickFormat.Png;
            vertical.Density = new Density(300);  
            vertical.Write(outputPng);
        }
}

有没有一种方法可以确定页数,或者更好,有没有一种方法可以在不传递第一页或最后一页的情况下拆分pdf?hii标记。。谢谢你。。。事实上,我有鬼脚本dll。。。但是当我在我的程序中添加这个dll时,出现了一个错误。。。dat确保此dll是可访问的,并且是有效的com组件。GhostScript dll不是com组件,而是非托管dll,它是必需的平台调用。因此,如何在项目中添加GhostScript dll?我正在WindowsVista上使用VisualStudio2008。我想在我的web应用程序中添加GhostScript dll。正如我在回答中提到的,给我发电子邮件(在配置文件页面上发电子邮件),我会给你发送一个C#包装器,使你能够将PDF转换为图像。除此之外,您还需要查看GhostScript文档以了解接口是什么,并使用pInvoke调用连接到GhostScript dll.hiii标记。。我用了你的密码。。但这是一个错误。。。无法加载dll。。。。。请帮我锁定(typeof(Pdf_2_image.GhostScript)){GC.Collect();Pdf_2_image.GhostScript.OutputDevice=Pdf_2_image.GhostScript.OutputDevice.jpeg;Pdf_2_image.GhostScript.DeviceOption[]deviceOptions=Pdf_2_image.GhostScript.deviceOptions.jpg(100)使用(Pdf_2_image.GhostScript GhostScript=新的Pdf_2_image.GhostScript(@“C:\Program Files\gs\gs8.70\bin”){
using ImageMagick;

string inputPdf= @"C:\my docs\input.pdf";
string outputPng= @"C:\my docs\output.png";

using (MagickImageCollection images = new MagickImageCollection())
{
    images.Read(inputPdf);
    using (IMagickImage vertical = images.AppendVertically())
        {
            vertical.Format = MagickFormat.Png;
            vertical.Density = new Density(300);  
            vertical.Write(outputPng);
        }
}