private static Image CreateQrCodeImage(string content) { var qrCodeWriter = new QRCodeWriter(); var byteMatrix = qrCodeWriter.Encode(content, 1, 1, null); var width = byteMatrix.GetWidth(); var height = byteMatrix.GetHeight(); var stride = (width + 7) / 8; var bitMatrix = new byte[stride * height]; var byteMatrixArray = byteMatrix.GetArray(); for (var y = 0; y < height; ++y) { var line = byteMatrixArray[y]; for (var x = 0; x < width; ++x) { if (line[x] != 0) { var offset = stride * y + x / 8; bitMatrix[offset] |= (byte)(0x80 >> (x % 8)); } } } var encodedImage = Ccittg4Encoder.Compress(bitMatrix, byteMatrix.GetWidth(), byteMatrix.GetHeight()); var qrcodeImage = Image.GetInstance(byteMatrix.GetWidth(), byteMatrix.GetHeight(), false, Image.CCITTG4, Image.CCITT_BLACKIS1, encodedImage, null); return(qrcodeImage); }
/// <summary>Regenerates barcode after changes in hints or code.</summary> public virtual void Regenerate() { if (code != null) { try { QRCodeWriter qc = new QRCodeWriter(); bm = qc.Encode(code, 1, 1, hints); } catch (WriterException ex) { throw new ArgumentException(ex.Message, ex.InnerException); } } }
private string GenerateQrCode(string assettagid) { string base64; //iTextSharp.text.Document doc = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(4.5f, 5.5f), 0.5f, 0.5f, 0, 0); //string filepathimg = Path.Combine(Server.MapPath("~/Views/QrCode/"), assettagid + ".jpg"); ByteMatrix btm; MemoryStream ms = null; Bitmap bmp = null; try { BarcodeQRCode qrcode = new BarcodeQRCode(assettagid, 200, 200, null); QRCodeWriter qrwriter = new QRCodeWriter(); btm = qrwriter.Encode(assettagid, 200, 200, null); qrcode.GetImage(); sbyte[][] imgQr = btm.GetArray(); bmp = new Bitmap(200, 200); Graphics gpr = Graphics.FromImage(bmp); gpr.Clear(Color.White); for (int i = 0; i < imgQr.Length; i++) { for (int j = 0; j <= imgQr[i].Length - 1; j++) { if (imgQr[j][i] == 0) { gpr.FillRectangle(Brushes.Black, i, j, 1, 1); } else { gpr.FillRectangle(Brushes.White, i, j, 1, 1); } } } Response.ContentType = "image/jpeg"; //fs = new FileStream(filepathimg, FileMode.Create); ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] byteImg = ms.ToArray(); base64 = Convert.ToBase64String(byteImg); } catch (Exception ex) { base64 = ""; string msg = ex.Message; } bmp.Dispose(); ms.Close(); return(base64); }
public System.Drawing.Image Generate(string text, int px_w, int px_h, int labelspace_right, string label_text) { var c = new QRCodeWriter(); var matrix = c.Encode(text, px_w, px_h); var pic = new Bitmap(px_w + 2, px_h + 2 + labelspace_right); for (var x = 0; x < matrix.GetWidth(); ++x) { for (var y = 0; y < matrix.GetHeight(); ++y) { pic.SetPixel(x + 1, y + 1, matrix.Get(x, y) == 0 ? Color.Black : Color.White); } } /* draw the frame marks */ using (var dwg = Graphics.FromImage(pic)) { var quarter = px_w / 4; #if DEBUG dwg.DrawRectangle(Pens.BlanchedAlmond, 0, 0, px_w, px_h); #endif for (var i = 0; i < Math.Max(1, px_w / (200 / 3)); ++i) { dwg.DrawLines(Pens.DarkGray, new Point[] { new Point(i, quarter - i), new Point(i, i), new Point(quarter - i, i) }); dwg.DrawLines(Pens.DarkGray, new Point[] { new Point(px_w - quarter + i, px_h + labelspace_right - i), new Point(px_w - i, px_h + labelspace_right - i), new Point(px_w - i, px_h + labelspace_right - quarter + i) }); } /* draw the label */ if (labelspace_right > 0) { #if DEBUG dwg.DrawRectangle(Pens.Maroon, 0, px_h, px_w, labelspace_right); #endif dwg.Clip = new Region(new RectangleF(0, px_h, px_w, labelspace_right)); dwg.DrawString(label_text, SystemFonts.SmallCaptionFont, Brushes.Black, new RectangleF(0, px_h, px_w, labelspace_right), StringFormat.GenericTypographic); } } pic.RotateFlip(RotateFlipType.Rotate270FlipNone); return(pic); }
/** * 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. * You can also use UTF-8, but correct behaviour is not guaranteed as Unicode is not supported in QRCodes. * 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); }