C# 为什么可以';我不能用ImageMagickNet打开PDF文件吗?

C# 为什么可以';我不能用ImageMagickNet打开PDF文件吗?,c#,.net,pdf,imagemagick,imagemagick.net,C#,.net,Pdf,Imagemagick,Imagemagick.net,以下是相关代码: MagickNet.InitializeMagick(); ImageMagickNET.Image image = new ImageMagickNET.Image(@"C:\temp.pdf"); image.Quality = 100; image.CompressType = ImageMagickNET.CompressionType.LosslessJPEGCompression; image.Write(@"C:\temp.jpg"); 我很确定这段代码应该可以

以下是相关代码:

MagickNet.InitializeMagick();
ImageMagickNET.Image image = new ImageMagickNET.Image(@"C:\temp.pdf");
image.Quality = 100;
image.CompressType = ImageMagickNET.CompressionType.LosslessJPEGCompression;
image.Write(@"C:\temp.jpg");
我很确定这段代码应该可以工作,但我得到了一个异常的信息异常:
外部组件抛出了一个异常。

此异常在以下行中引发:
ImageMagickNET.Image Image=new-ImageMagickNET.Image(@“C:\temp.pdf”)

InnerException:null

堆栈跟踪:

   at Magick.Image.{ctor}(Image* , basic_string<char\,std::char_traits<char>\,std::allocator<char> >* )
   at ImageMagickNET.Image..ctor(String imageSpec)
   at WindowsFormsApplication1.Form1.ReadQRCode(String doc) in C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs:line 126
   at WindowsFormsApplication1.Form1.seperatePDFsInOrder(String fileName) in C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs:line 109
   at WindowsFormsApplication1.Form1.InitializeComponent() in C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs:line 44
   at WindowsFormsApplication1.Form1..ctor() in C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 16
   at WindowsFormsApplication1.Program.Main() in C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 20
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
在Magick.Image.{ctor}(Image*,basic_string*)
在ImageMagickNET.Image..ctor(字符串imageSpec)
在C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.ReadQRCode(字符串文档)中的WindowsFormsApplication1.Form1.ReadQRCode(字符串文档):第126行
在C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.SeparatePDFSinorder(字符串文件名)中
在C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.InitializeComponent()中的WindowsFormsApplication1.Form1.InitializeComponent()处。cs:第44行
在C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs中的WindowsFormsApplication1..ctor()处:第16行
在C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs中的WindowsFormsApplication1.Program.Main()中:第20行
位于System.AppDomain.\u nExecuteAssembly(RuntimeAssembly程序集,字符串[]args)
位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上
位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
位于System.Threading.ThreadHelper.ThreadStart()处

有人知道我可能做错了什么吗

在第二行引号中,您似乎将自己的
image=new
声明为*.pdf文件。最好用
c:\tmp.jpg
试试。或者更好的
c:\temp\tmp.jpg

如果看到后缀*.PDF,ImageMagick将应用其“我想将此文件解析为PDF”模式。(仅当文件名没有后缀时,它才会应用其神奇的文件类型发现例程。)

此外,运行代码的用户可能无法写入文件
c:\tmp.jpg
。可能有两个原因:

  • c:\as目录对此用户不可写
  • 文件已存在,此用户无法覆盖该文件(它可能属于其他用户)

  • 最后,请注意,ImageMagick将PDF作为输入进行处理的能力依赖于外部“委托”:它本身无法完成该工作,它需要在同一主机上安装Ghostscript来调用它并让它完成该工作……

    有内部异常吗?堆栈跟踪?@Oded对此表示抱歉。将两者都添加到原始帖子中。为什么要尝试将PDF文件加载到图像处理库中?该文件是否确实存在于该位置?@Oded我正在尝试将pdf转换为jpg。这段代码取自一个声称做这项工作的示例。该文件确实存在于该位置。非常感谢您的评论。当我周一返回工作岗位时,我会将其应用于我的问题。你似乎对这个问题非常了解,我想知道你是否能给我一些快速的建议。我所要做的就是将几个1页的PDF文件转换成图像。我需要处理图像,但根本不关心保存它们。我这样做是正确的,还是你会提出不同的解决方案?再次感谢!此外,正在运行的代码没有在c:\中编辑文件。我刚刚编辑了这个问题的路径。那么,如果这些图像没有保存,你打算怎么处理它们呢?什么样的进一步加工?你有没有考虑过在管道中这样做?我正在扫描二维码。我应该使用的QR解码库只接受图像。我只需要读取二维码,然后根据其中编码的信息命名PDF。那么你可以使用管道或FIFO从中获取图像(而不是文件句柄)?