2012-07-06 39 views
0

這裏是有問題的代碼:爲什麼我無法用ImageMagickNet打開PDF文件?

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:空

堆棧跟蹤:

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

任何人都有任何想法,我可能會做錯嗎?

+1

任何內部異常?堆棧跟蹤? – Oded 2012-07-06 19:35:28

+0

@Oded對不起。添加到原始帖子。 – PFranchise 2012-07-06 19:38:10

+0

你爲什麼要將PDF文件加載到圖像處理庫中?該文件是否確實存在於該位置? – Oded 2012-07-06 19:39:37

回答

2

您似乎在第二個引用行中聲明瞭image = new作爲* .pdf文件。最好用c:\tmp.jpg來嘗試。或者更好c:\temp\tmp.jpg ...

ImageMagick應用其'我想將該文件解析爲PDF' -mode如果它看到後綴* .pdf。 (僅在文件名沒有後綴的情況下才應用其魔術文件類型發現例程。)

此外,運行代碼的用戶可能無法寫入文件c:\tmp.jpg。可能有兩個原因:

  1. c:\作爲目錄不可寫入此用戶。
  2. 該文件已經存在,並且不能被該用戶覆蓋(它可能屬於另一個用戶)。

最後,注意ImageMagick的能力來處理PDF文件作爲輸入依賴於外部「代理」:它不能勝任這項工作本身,它需要在同一臺主機上的Ghostscript安裝調用它,讓它去做...

+0

非常感謝您的評論。當我在星期一回到工作崗位時,我會將其應用於我的問題。你似乎對這個問題非常瞭解,我想知道你是否可以借給我一些快速建議。我所要做的就是將幾頁1頁PDF轉換爲圖像。我需要處理圖像,但不關心保存圖像。我是否會以正確的方式進行討論,或者您會提出一個不同的解決方案?再次感謝! – PFranchise 2012-07-07 15:13:03

+0

此外,正在運行的代碼未在編輯c:\中的文件。我剛剛編輯了這個問題的路徑。 – PFranchise 2012-07-07 15:17:08

+0

所以**如果你沒有保存這些圖片,你想要做什麼**?什麼樣的進一步處理?你有沒有考慮過這樣做呢? – 2012-07-07 15:18:30

相關問題