ZXing.NET used to generate and interpret (bar code, two-dimensional code)
Add a reference: zxing.dll, zxing.presentation.dll
1 generates (bar code, two-dimensional code) is mainly used BarcodeWriter , BarcodeFormat , EncodingOptions , BitmapRenderer these classes.
2 interpretation (bar codes, two-dimensional code) is mainly used BarcodeReader , DecodingOptions these two categories.
/// <Summary> /// barcode, two-dimensional code /// </ Summary> public static class BarQrCodeHelper { /// <Summary> /// barcode generation, two-dimensional code /// </ Summary> // / <param name = "code"> to be encoded content </ param> /// <param name = "the format"> coding format (barcode CODE_128, two-dimensional code QR_CODE) </ param> /// <param name = " options "> encoding parameters (barcode EncodingOptions, two-dimensional code QrCodeEncodingOptions) </ param> /// <param name =" the renderer "> barcode, two-dimensional code Bitmap parameters </ param> /// <param name = "qrcodeoptions"> two-dimensional code parameter</param> /// <returns></returns> public static Bitmap EncodeBarQrCode(string code, BarcodeFormat format, EncodingOptions options, BitmapRenderer renderer = null, QrCodeOptions qrcodeoptions = null) { BarcodeWriter writer = new BarcodeWriter(); writer.Options = options; writer.Format = format; if (renderer != null) writer.Renderer = renderer; Bitmap bmp = writer.Write(code); if ((options is QrCodeEncodingOptions) && qrcodeoptions != null && qrcodeoptions.IsLogo) { int w = qrcodeoptions.Width - qrcodeoptions.LogoPadding * 2; int h = qrcodeoptions.Height - qrcodeoptions.LogoPadding * 2; Rectangle logo_rect = new Rectangle((bmp.Width - w) / 2, (bmp.Height - h) / 2, w, h); if (qrcodeoptions.LogoPadding == 0) { logo_rect = new Rectangle((bmp.Width - qrcodeoptions.Width) / 2, (bmp.Height - qrcodeoptions.Height) / 2, qrcodeoptions.Width, qrcodeoptions.Height); } Graphics bmp_g = Graphics.FromImage(bmp); bmp_g.InterpolationMode = InterpolationMode.HighQualityBicubic; bmp_g.CompositingQuality = CompositingQuality.HighQuality; bmp_g.SmoothingMode = SmoothingMode.AntiAlias; Bitmap logo_bmp = (Bitmap)qrcodeoptions.Logo;//按比例缩放Logo if ((qrcodeoptions.Logo.Width > logo_rect.Width || qrcodeoptions.Logo.Height > logo_rect.Height) || (qrcodeoptions.Logo.Width < logo_rect.Width && qrcodeoptions.Logo.Height < logo_rect.Height)) { logo_bmp = new Bitmap(logo_rect.Width, logo_rect.Height); Graphics logo_g = Graphics.FromImage(logo_bmp); logo_g.InterpolationMode = InterpolationMode.HighQualityBicubic; logo_g.CompositingQuality = CompositingQuality.HighQuality; logo_g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle rect = new Rectangle(0, 0, 0, 0); float num1 = (float)logo_rect.Width / (float)qrcodeoptions.Logo.Width; float num2 = (float)logo_rect.Height / (float)qrcodeoptions.Logo.Height; float num3 = Math.Min(num1, num2); rect.Width = (int)((float)qrcodeoptions.Logo.Width * (float)num3); rect.Height = (int)((float)qrcodeoptions.Logo.Height * (float)num3); rect.X = (logo_rect.Width - rect.Width) / 2; rect.Y = (logo_rect.Height - rect.Height) / 2; logo_g.DrawImage((Bitmap)qrcodeoptions.Logo, rect); logo_g.Dispose(); } Bitmap corner_bmp = new Bitmap(logo_rect.Width, logo_rect.Height);//圆角处理 Graphics corner_g = Graphics.FromImage(corner_bmp); corner_g.InterpolationMode = InterpolationMode.HighQualityBicubic; corner_g.CompositingQuality = CompositingQuality.HighQuality; corner_g.SmoothingMode = SmoothingMode.AntiAlias; corner_g.SetClip(CreateRoundedRectanglePath(new Rectangle(0, 0, logo_rect.Width, logo_rect.Height), qrcodeoptions.Radius)); corner_g.DrawImage(logo_bmp, new Rectangle(0, 0, logo_bmp.Width, logo_bmp.Height)); logo_bmp.Dispose(); if (qrcodeoptions.LogoPadding != 0) { Pen padding_pen = new Pen(Color.Gray); SolidBrush padding_sb = new SolidBrush(qrcodeoptions.LogoBackColor); Rectangle padding_rect = new Rectangle((bmp.Width - qrcodeoptions.Width) / 2, (bmp.Height - qrcodeoptions.Height) / 2, qrcodeoptions.Width, qrcodeoptions.Height); GraphicsPath padding_path = CreateRoundedRectanglePath(padding_rect, qrcodeoptions.Radius); bmp_g.FillPath(padding_sb, padding_path); bmp_g.DrawPath(padding_pen, padding_path); padding_sb.Dispose(); padding_pen.Dispose(); padding_path.Dispose(); } bmp_g.DrawImage(corner_bmp, logo_rect); corner_bmp.Dispose (); corner_g.Dispose (); bmp_g.Dispose (); } return BMP; } /// <Summary> /// explained barcode, two-dimensional code /// </ Summary> /// <param name = "code"> to decode the content </ param> /// <param name = "Options"> decoding parameters </ param> /// <Returns> </ Returns> public static String DecodeBarQrCode (Bitmap code, DecodingOptions Options ) { BarcodeReader Reader = new new BarcodeReader (); Reader.Options = options; reader.AutoRotate = true; Result result = reader.Decode(code); return result != null ? result.Text : String.Empty; } /// <summary> /// 创建圆角矩形 /// </summary> /// <param name="rect">区域</param> /// <param name="cornerRadius">圆角角度</param> /// <returns></returns> private static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius) { GraphicsPath result = new GraphicsPath(); int diameter = cornerRadius * 2; result.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90); result.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius, rect.Y); result.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90); result.AddLine(rect.Right, rect.Y + cornerRadius, rect.Right, rect.Bottom - cornerRadius); result.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90); result.AddLine(rect.Right - cornerRadius, rect.Bottom, rect.X + cornerRadius, rect.Bottom); result.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90); result.AddLine(rect.X, rect.Bottom - cornerRadius, rect.X, rect.Y + cornerRadius); result.CloseFigure(); return result; } /// <summary> /// 二维码参数 /// </summary> public class QrCodeOptions { public QrCodeOptions() { } public QrCodeOptions(int _Radius, bool _IsLogo, Image Logo, int _Width, int _Height, int _LogoPadding, Color _LogoBackColor, Color _LogoBorderColor) { this.Radius = _Radius; this.IsLogo = _IsLogo; this.Logo = Logo; this.Width = _Width; this.Height = _Height; this.LogoPadding = _LogoPadding; this.LogoBackColor = _LogoBackColor; this.LogoBorderColor = _LogoBorderColor; } /// <Summary> /// fillet degrees /// </ Summary> public int the Radius = . 4 ; /// <Summary> /// whether Logo /// </ Summary> public BOOL IsLogo = to false ; /// <Summary> /// Logo image /// </ Summary> public image Logo; /// <Summary> /// Logo width /// </ Summary> public int the width; /// <Summary> ///Logo height /// </ Summary> public int the Height; /// <Summary> /// Logo padding /// </ Summary> public int LogoPadding = . 4 ; /// <Summary> /// the Logo margins background color /// </ Summary> public color LogoBackColor = Color.White; /// <Summary> /// Logo border color /// </ Summary> public color LogoBorderColor = Color.Gray; } }