【问题标题】:Why can't I open a PDF file with ImageMagickNet?为什么我无法使用 ImageMagickNet 打开 PDF 文件?
【发布时间】:2012-07-06 19:33:46
【问题描述】:

这里是有问题的代码:

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");

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

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

【问题讨论】:

  • 任何内部异常?堆栈跟踪?
  • @Oded 对此感到抱歉。将两者都添加到原始帖子中。
  • 您为什么要尝试将 PDF 文件加载到图像处理库中?该文件是否确实存在于该位置?
  • @Oded 我正在尝试将 pdf 转换为 jpg。这段代码取自一个声称可以完成这项工作的示例。该文件确实存在于该位置。

标签: c# .net pdf imagemagick imagemagick.net


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 非常感谢您的评论。当我周一重返工作岗位时,我会将其应用于我的问题。你似乎对这个问题非常了解,我想知道你是否可以给我一些快速的建议。我要做的就是将几个 1 页 PDFS 转换为图像。我需要处理图像,但甚至不关心保存它们。我是否以正确的方式解决这个问题,还是您会提出不同的解决方案?再次感谢!
  • 另外,正在运行的代码没有编辑 c:\ 中的文件。我刚刚编辑了这个问题的路径。
  • 那么什么如果这些图像没有保存,你想对它们做什么?什么样的进一步加工?您是否考虑过在管道中这样做?
  • 我正在扫描他们的二维码。我应该使用的 QR 解码库只接受图像。我只需要阅读 QR 码,然后根据其中编码的信息命名 PDF。
  • 所以您可以使用管道或 FIFO 从(而不是文件句柄)获取图像?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 2022-08-16
  • 1970-01-01
  • 2021-05-01
  • 1970-01-01
相关资源
最近更新 更多