2011-01-26 38 views
4

我想將iTextSharp.text.Image類型的對象轉換回System.Drawing.Image。 下面是一段代碼,不工作:將iTextSharp.text.Image轉換回System.Drawing.Image

System.Drawing.Image img = System.Drawing.Image.FromStream(new MemoryStream(itextImg.RawData)); 

我可能會對此都錯了,但我不會知道,除非我請教有關專家,經過兩小時無果而終的在線搜索,我我終於自己發表了一個問題。

回答

1

我敢肯定,偶爾會工作,但在一般情況下會失敗......這取決於圖像使用的壓縮過濾器。

我相信JPEG圖像流正是您在.jpeg文件中看到的......但對於大多數(所有)其他壓縮類型,圖像信息(高度,寬度,每個組件的位數,組件的數量等)是至關重要的。

所以它可能,但不是那樣。

PS:至少有一種iText無法解壓縮的圖像格式,CITTFAXDecode(JBIG2,可能是其他的)。在這些情況下,您需要一些能夠獲取原始像素數據的「其他軟件」,以便將其包裝在Drawing.Image中。

1

是的,我發現通過重寫BarcodeQRCode類iTextSharp的和GetImageMethod()的溶液如下所述:

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using iTextSharp.text; 
using iTextSharp.text.pdf.qrcode; 
using iTextSharp.text.pdf.codec; 
/* 
    Class rewritted to Convert ByteMatrix to BMP image by rewritting GetImage method 
from ITextSharp 
author: Luis Claudio Souza 
*/ 

namespace iTextSharp.text.pdf{ 

/** 
* A QRCode implementation based on the zxing code. 
* @author Paulo Soares 
* @since 5.0.2 
*/ 
public class BarcodeQRCode { 
    ByteMatrix bm; 

    /** 
    * Creates the QR barcode. The barcode is always created with the smallest possible  size and is then stretched 
    * to the width and height given. Set the width and height to 1 to get an unscaled barcode. 
    * @param content the text to be encoded 
    * @param width the barcode width 
    * @param height the barcode height 
    * @param hints modifiers to change the way the barcode is create. They can be EncodeHintType.ERROR_CORRECTION 
    * and EncodeHintType.CHARACTER_SET. For EncodeHintType.ERROR_CORRECTION the values can be ErrorCorrectionLevel.L, M, Q, H. 
    * For EncodeHintType.CHARACTER_SET the values are strings and can be Cp437, Shift_JIS and ISO-8859-1 to ISO-8859-16. The default value is 
    * ISO-8859-1. 
    * @throws WriterException 
    */ 
    public BarcodeQRCode(String content, int width, int height, IDictionary<EncodeHintType, Object> hints) { 
     QRCodeWriter qc = new QRCodeWriter(); 
     bm = qc.Encode(content, width, height, hints); 
    } 

    private byte[] GetBitMatrix() { 
     int width = bm.GetWidth(); 
     int height = bm.GetHeight(); 
     int stride = (width + 7)/8; 
     byte[] b = new byte[stride * height]; 
     sbyte[][] mt = bm.GetArray(); 
     for (int y = 0; y < height; ++y) { 
      sbyte[] line = mt[y]; 
      for (int x = 0; x < width; ++x) { 
       if (line[x] != 0) { 
        int offset = stride * y + x/8; 
        b[offset] |= (byte)(0x80 >> (x % 8)); 
       } 
      } 
     } 
     return b; 
    } 

    /** Gets an <CODE>Image</CODE> with the barcode. 
    * @return the barcode <CODE>Image</CODE> 
    * @throws BadElementException on error 
    */ 

    public void GetImage() 
    { 

     sbyte[][] imgNew = bm.GetArray(); 
     Bitmap bmp1 = new Bitmap(bm.GetWidth(), bm.GetHeight()); 


     Graphics g1 = Graphics.FromImage(bmp1); 
     g1.Clear(Color.White); 
     for (int i = 0; i <= imgNew.Length - 1; i++) 
     { 
      for (int j = 0; j <= imgNew[i].Length - 1; j++) 
      { 
       if (imgNew[j][i] == 0) 
       { 
        g1.FillRectangle(Brushes.Black, i, j, 1, 1); 
       } 
       else 
       { 
        g1.FillRectangle(Brushes.White, i, j, 1, 1); 
       } 
      } 
     } 
     bmp1.Save("D:\\QREncode.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 


    } 

    } 
} 

之後,我使用該代碼來調用方法:

  var hints = new Dictionary<EncodeHintType, object>(); 
     hints.Add(EncodeHintType.ERROR_CORRECTION, iTextSharp.text.pdf.qrcode.ErrorCorrectionLevel.H); 


     BarcodeQRCode code = new BarcodeQRCode("98134979213479523874952873", 100, 100, hints); 

     code.GetImage(); 

在新的GetImage方法中,您可以選擇如何處理bmp類。在這種情況下,它會保存一個JPEG圖像文件,但該方法可以返回供調用者使用的內存流。

相關問題