Yes, I found a solution by rewriting the BarcodeQRCode class ITextSharp and GetImageMethod (), as described below:
using System; using System.Collections.Generic; using System.Drawing; using iTextSharp.text; using iTextSharp.text.pdf.qrcode; using iTextSharp.text.pdf.codec; namespace iTextSharp.text.pdf{ public class BarcodeQRCode { ByteMatrix bm; 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; } 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 the new GetImage method, you can choose what to do with the bmp class. In this case, it saves the JPEG image file, but the method can either return the storage stream that will be used by the caller.
source share