0

这是有问题的代码:

MagickNet.InitializeMagick();
ImageMagickNET.Image image = new ImageMagickNET.Image(@"C:\temp.pdf");
image.Quality = 100;
image.CompressType = ImageMagickNET.CompressionType.LosslessJPEGCompression;
image.Write(@"C:\temp.jpg");

我相当确定这段代码应该可以工作,但我得到了异常信息丰富的异常:External component has thrown an exception.

上线抛出这个异常:ImageMagickNET.Image image = new ImageMagickNET.Image(@"C:\temp.pdf");

内部异常:空

堆栈跟踪:

   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()

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

4

1 回答 1

2

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

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

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

  1. c:\ 作为该用户不可写的目录。
  2. 该文件已经存在,并且不能被这个用户覆盖(它可能属于另一个用户)。

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

于 2012-07-07T09:03:15.937 回答