public static System.Drawing.Bitmap CreateImage(string content, string format, int width, int height, bool pureBarcode, string fontName, float fontSize) { var writer = new ZXing.BarcodeWriter { Format = (BarcodeFormat)(Enum.Parse(typeof(BarcodeFormat), format)), Options = new ZXing.Common.EncodingOptions { Height = height, Width = width, Margin = 0, PureBarcode = pureBarcode, } }; if (writer.Renderer is ZXing.Rendering.BitmapRenderer) { ((ZXing.Rendering.BitmapRenderer)writer.Renderer).TextFont = new System.Drawing.Font(fontName, fontSize <= 0 ? 12 : fontSize); } else if (writer.Renderer is ZXing.Rendering.WriteableBitmapRenderer) { ((ZXing.Rendering.WriteableBitmapRenderer)writer.Renderer).FontFamily = new System.Windows.Media.FontFamily(fontName); ((ZXing.Rendering.WriteableBitmapRenderer)writer.Renderer).FontSize = fontSize <= 0 ? 12 : fontSize; } var data = writer.Encode(content); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(data.Width, data.Height); for (int x = 0; x < data.Width; x++) { for (int y = 0; y < data.Height; y++) { bitmap.SetPixel(x, y, data[x, y] ? System.Drawing.Color.Black : System.Drawing.Color.White); } } return(bitmap); }
/// <summary> /// Generates the barcode. /// </summary> /// <param name="barcodeType">Type of the barcode.</param> /// <param name="value">The value.</param> /// <returns></returns> public BarcodeObject GenerateBarcode(string barcodeType, string value) { // Determine the barcode type BarcodeFormat codeType = CalculateType(barcodeType); try { BarcodeWriter writer = new BarcodeWriter(); writer.Format = codeType; writer.Options = new EncodingOptions {Height = 100, Width = 300}; BitMatrix bitMatrix = writer.Encode(value); Bitmap bitmap = writer.Write(value); // Convert to the image bytes return ConvertToBase64String(bitmap); } catch (Exception exception) { throw new Exception("GenerateBarcode is failing", exception); } }
private Bitmap BarEncode( string text, BarcodeFormat barFormat = BarcodeFormat.CODE_128, bool isbn = false ) { var width = 256; var height = 128; var margin = 0; if ( string.IsNullOrEmpty( text ) ) return ( new Bitmap( width, height ) ); string barText = text; int maxText = 16; switch ( barFormat ) { case BarcodeFormat.CODE_128: width = 256; height = 72; margin = 7; maxText = 232; break; case BarcodeFormat.EAN_13: //height = (int) ( width * 25.93 / 37.29 ); height = (int) ( width * 26.26 / 37.29 ); if ( isbn ) { string isbn13 = calcISBN_13(barText); string isbn10 = calcISBN_10(barText); if ( string.IsNullOrEmpty( isbn13 ) ) return ( new Bitmap( width, height ) ); //if ( string.IsNullOrEmpty( isbn10 ) ) return ( new Bitmap( width, height ) ); barText = isbn13; } break; default: return ( new Bitmap( width, height ) ); } var bw = new BarcodeWriter(); bw.Options.Width = width; bw.Options.Height = height; //bw.Options.PureBarcode = false; bw.Options.Hints.Add( EncodeHintType.MARGIN, margin ); bw.Options.Hints.Add( EncodeHintType.DISABLE_ECI, true ); bw.Options.Hints.Add( EncodeHintType.CHARACTER_SET, "UTF-8" ); bw.Renderer = new BitmapRenderer(); bw.Format = barFormat; if ( barText.Length > maxText ) { barText = barText.Substring( 0, maxText ); } try { BitMatrix bm = bw.Encode( barText ); int[] rectangle = bm.getEnclosingRectangle(); var bmW = rectangle[2]; var bmH = rectangle[3]; //bw.Options.Width = ( bmW <= 256 ) ? ( bmW + 32 ) : (int)( bmW * ( 1 + 32 / 256 ) ); bw.Options.Width = (int)( bmW * 1.25); Bitmap barcodeBitmap = bw.Write(barText); return ( barcodeBitmap ); } catch ( WriterException ) { MessageBox.Show( this, I18N._( "Text data can not be encoded to barcode!" ), I18N._( "Error" ), MessageBoxButtons.OK, MessageBoxIcon.Error ); return ( new Bitmap( width, height ) ); } }
/// /// 生成二维码 /// /// 二维码信息 /// 图片 private Bitmap GenByZXingNet(string msg) { BarcodeWriter writer = new BarcodeWriter(); writer.Format = BarcodeFormat.QR_CODE; writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//编码问题 writer.Options.Hints.Add( EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H ); const int codeSizeInPixels = 100; //设置图片长宽 writer.Options.Height = writer.Options.Width = codeSizeInPixels; writer.Options.Margin = 0;//设置边框 ZXing.Common.BitMatrix bm = writer.Encode(msg); Bitmap img = writer.Write(bm); return img; }
private Bitmap GetQrCode( string text, Image overlay ) { var width = 512; var height = 512; var margin = 0; int MAX_TEXT = 750; if (String.IsNullOrEmpty(text)) { Bitmap blankBitmap = new Bitmap(width, height); return (blankBitmap); } var qrText = text; if (qrText.Length > MAX_TEXT) { qrText = qrText.Substring(0, MAX_TEXT); } var bw = new ZXing.BarcodeWriter(); var encOptions = new ZXing.Common.EncodingOptions { Width = width, Height = height, Margin = margin, PureBarcode = true }; encOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q); encOptions.Hints.Add(EncodeHintType.DISABLE_ECI, true); encOptions.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); encOptions.Hints.Add(EncodeHintType.PDF417_COMPACT, true); encOptions.Hints.Add(EncodeHintType.PDF417_COMPACTION, ZXing.PDF417.Internal.Compaction.AUTO); bw.Renderer = new BitmapRenderer(); bw.Options = encOptions; bw.Format = BarcodeFormat.QR_CODE; try { BitMatrix bm = bw.Encode( text ); int[] rectangle = bm.getEnclosingRectangle(); var bmW = rectangle[2]; var bmH = rectangle[3]; //bw.Options.Width = (int) ( bmW * 1.1 ); //bw.Options.Height = (int) ( bmH * 1.1 ); Bitmap barcodeBitmap = bw.Write(qrText); int deltaHeigth = barcodeBitmap.Height - overlay.Height; int deltaWidth = barcodeBitmap.Width - overlay.Width; using (Graphics g = Graphics.FromImage(barcodeBitmap)) { g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.DrawImage(overlay, new Point(deltaWidth / 2, deltaHeigth / 2)); } return (barcodeBitmap); } catch (WriterException) { return (new Bitmap(width, height)); } }