简体   繁体   English

将iTextSharp.text.Image转换回System.Drawing.Image

[英]Converting iTextSharp.text.Image back to System.Drawing.Image

I am trying to convert object of type iTextSharp.text.Image back to System.Drawing.Image. 我正在尝试将iTextSharp.text.Image类型的对象转换回System.Drawing.Image。 Here is a chunk of code that is not working: 这是一段不起作用的代码:

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

I could be going about this all wrong, but I won't know unless I consult the experts, and after two hours of fruitless searching online, I am finally posting it myself as a question. 我可能会犯错,但除非咨询专家,否则我将不知道。经过两个小时的网上搜索无效后,我终于将自己发布为一个问题。

Yes, I found the solution by rewriting BarcodeQRCode class of ITextSharp and GetImageMethod() as described below: 是的,我通过重写ITextSharp和GetImageMethod()的BarcodeQRCode类来找到解决方案,如下所述:

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


    }

    }
}

After that, I used this code to call the method: 之后,我使用以下代码来调用方法:

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

In new GetImage method, you can choose what to do with the bmp class. 在新的GetImage方法中,您可以选择对bmp类执行的操作。 In this case, it saves a JPEG image file but the method can either return a memorystream to be used by the caller. 在这种情况下,它会保存JPEG图像文件,但是该方法可以返回要由调用方使用的内存流。

I'm pretty sure that will work occasionally, but will fail in the general case... it depends on what compression filters the image is using. 我很确定这偶尔会起作用,但是在一般情况下会失败...这取决于图像使用的压缩滤镜。

I believe JPEG image streams are exactly what you'd see in a .jpeg file... but for most (all?) other compression types, the image information (height, width, bits per component, number of components, etc) is vital. 我相信JPEG图像流正是您在.jpeg文件中看到的...但是对于大多数(全部?)其他压缩类型,图像信息(高度,宽度,每个组件的位数,组件的数量等)是重要。

So it'll be possible, but Not Like That. 因此有可能,但并非如此。

PS: There's at least one image format that iText cannot decompress, CITTFAXDecode (JBIG2, probably others). PS:至少有一种iText无法解压缩的图像格式CITTFAXDecode(JBIG2,可能还有其他格式)。 In those cases, you'll need some Other Software that will get the raw pixel data out so you can wrap it in a Drawing.Image . 在这种情况下,您将需要一些其他软件来获取原始像素数据,以便将其包装在Drawing.Image

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM