2015-04-30 53 views
2

我試圖使用以下代碼從PDF中提取圖像。它適用於像DCTDecode這樣的過濾器,但不適用於JPXDEcode。在參數image.GetDrawingImage()被調用時出現「參數無效」錯誤。使用iTextSharp從包含JPXDecode過濾器的PDF中提取圖像時出現「參數無效」錯誤

using System.Drawing.Imaging; 
    using iTextSharp.text.pdf; 
    using iTextSharp.text.pdf.parser; 
    ... 
    PdfReader pdf = new PdfReader(currfilename); 
    PdfReaderContentParser parser = new PdfReaderContentParser(pdf); 
    ImageRender listener = new ImageRender(); 
    for (int i = 1; i <= pdf.NumberOfPages; i++) 
    { 
      try 
      { 
       parser.ProcessContent(i, listener);//calls RenderImage() at this point 

      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e); 
      } 

     } 


    public void RenderImage(ImageRenderInfo renderInfo) 
      { 
       PdfImageObject image = renderInfo.GetImage(); 
       PdfName filter = image.Get(PdfName.FILTER) as PdfName; 


       if (renderInfo.GetRef() != null && image != null) 
       { 
        using (System.Drawing.Image dotnetImg = image.GetDrawingImage())//exception occurs at this point 
        { 
         if (dotnetImg != null) 
         { 
          ImageNames.Add(string.Format("{0}.tiff", renderInfo.GetRef().Number)); 
          using (MemoryStream ms = new MemoryStream()) 
          { 
           dotnetImg.Save(ms, ImageFormat.Tiff); 
           Images.Add(ms.ToArray()); 
          } 
         } 
        } 
       } 



      } 

我試圖這些鏈接的解決方案

Extract images using iTextSharp

Extract Image from a particular page in PDF

,並能提取使用PdfReader.GetStreamBytesRaw()函數的原始圖像字節,但是 「參數無效」異常總是發生在System.Drawing.Image.FromStream(內存流)被調用的地方。

我也檢查過這個鏈接"Parameter is not valid" exception from System.Drawing.Image.FromStream() method,但找不到任何有用的東西。

請幫

回答

2

使用FreeImage.dll解決了這個問題。代碼如下所示

using FreeImageAPI; 
using System.Drawing.Imaging; 
using iTextSharp.text.pdf; 
using iTextSharp.text.pdf.parser; 
... 
     imagecount = 0; 
     PdfReader pdf = new PdfReader(currfilename); 
     PdfReaderContentParser parser = new PdfReaderContentParser(pdf); 
     ImageRender listener = new ImageRender(); 
     for (int i = 1; i <= pdf.NumberOfPages; i++) 
     { 
     try 
      { 
        parser.ProcessContent(i, listener);//calls RenderImage() at this point 
      } 
     catch (Exception e) 
     { 
      Console.WriteLine(e); 
     } 

     } 
     if (listener.Images.Count > 0) 
     { 
      for (int j = 0; (j < listener.Images.Count); ++j) 
      { 
       string imgpath = Environment.CurrentDirectory.ToString() + "\\Image" + imagecount + ".bmp"; 

       // create a memory stream 
       MemoryStream imageStream = new MemoryStream(listener.Images[j]); 
       // create a FIBITMAP from that stream 
       FIBITMAP dib = FreeImage.LoadFromStream(imageStream); 

       if (dib.IsNull) continue; 
      //turn it into a normal Bitmap 
      Bitmap bitmap = FreeImage.GetBitmap(dib); 
      bitmap.Save(imgpath); 
      //unload the FIBITMAP 
      FreeImage.UnloadEx(ref dib); 
      bitmap.Dispose(); 

      System.Drawing.Image img = System.Drawing.Image.FromFile(imgpath); 



      } 





    public void RenderImage(ImageRenderInfo renderInfo) 
{ 
    PdfImageObject image = renderInfo.GetImage();  
    if (renderInfo.GetRef() != null && image != null) 
    { 

     byte[] tempImage = image.GetImageAsBytes();         
     ImageNames.Add(string.Format("0}.bmp",renderInfo.GetRef().Number)); 
     Images.Add(tempImage); 

    }    


} 

我跟着給here到FreeImage的淨增加解決方案

相關問題