C# 二维码、条形码生成
1.工具类:BarCodeHelper(条码生成类),二维码生成类(QRCodeHelper)
2.BarCodeHelper(条码生成类)代码:
1 public class BarCodeHelper 2 { 3 public class Code128 4 { 5 private readonly DataTable _mCode128 = new DataTable(); 6 private uint _mHeight = 40; 7 8 /// <summary> 9 /// 高度 10 /// </summary> 11 public uint Height { get { return _mHeight; } set { _mHeight = value; } } 12 private Font _mValueFont; 13 14 /// <summary> 15 /// 是否显示可见号码 如果为NULL不显示号码 16 /// </summary> 17 public Font ValueFont { get { return _mValueFont; } set { _mValueFont = value; } } 18 private byte _mMagnify; 19 20 /// <summary> 21 /// 放大倍数 22 /// </summary> 23 public byte Magnify { get { return _mMagnify; } set { _mMagnify = value; } } 24 25 /// <summary> 26 /// 条码类别 27 /// </summary> 28 public enum Encode 29 { 30 Code128A, 31 Code128B, 32 Code128C, 33 Ean128 34 } 35 36 public Code128() 37 { 38 _mCode128.Columns.Add("ID"); 39 _mCode128.Columns.Add("Code128A"); 40 _mCode128.Columns.Add("Code128B"); 41 _mCode128.Columns.Add("Code128C"); 42 _mCode128.Columns.Add("BandCode"); 43 _mCode128.CaseSensitive = true; 44 #region 数据表 45 _mCode128.Rows.Add("0", " ", " ", "00", "212222"); 46 _mCode128.Rows.Add("1", "!", "!", "01", "222122"); 47 _mCode128.Rows.Add("2", "\"", "\"", "02", "222221"); 48 _mCode128.Rows.Add("3", "#", "#", "03", "121223"); 49 _mCode128.Rows.Add("4", "$", "$", "04", "121322"); 50 _mCode128.Rows.Add("5", "%", "%", "05", "131222"); 51 _mCode128.Rows.Add("6", "&", "&", "06", "122213"); 52 _mCode128.Rows.Add("7", "'", "'", "07", "122312"); 53 _mCode128.Rows.Add("8", "(", "(", "08", "132212"); 54 _mCode128.Rows.Add("9", ")", ")", "09", "221213"); 55 _mCode128.Rows.Add("10", "*", "*", "10", "221312"); 56 _mCode128.Rows.Add("11", "+", "+", "11", "231212"); 57 _mCode128.Rows.Add("12", ",", ",", "12", "112232"); 58 _mCode128.Rows.Add("13", "-", "-", "13", "122132"); 59 _mCode128.Rows.Add("14", ".", ".", "14", "122231"); 60 _mCode128.Rows.Add("15", "/", "/", "15", "113222"); 61 _mCode128.Rows.Add("16", "0", "0", "16", "123122"); 62 _mCode128.Rows.Add("17", "1", "1", "17", "123221"); 63 _mCode128.Rows.Add("18", "2", "2", "18", "223211"); 64 _mCode128.Rows.Add("19", "3", "3", "19", "221132"); 65 _mCode128.Rows.Add("20", "4", "4", "20", "221231"); 66 _mCode128.Rows.Add("21", "5", "5", "21", "213212"); 67 _mCode128.Rows.Add("22", "6", "6", "22", "223112"); 68 _mCode128.Rows.Add("23", "7", "7", "23", "312131"); 69 _mCode128.Rows.Add("24", "8", "8", "24", "311222"); 70 _mCode128.Rows.Add("25", "9", "9", "25", "321122"); 71 _mCode128.Rows.Add("26", ":", ":", "26", "321221"); 72 _mCode128.Rows.Add("27", ";", ";", "27", "312212"); 73 _mCode128.Rows.Add("28", "<", "<", "28", "322112"); 74 _mCode128.Rows.Add("29", "=", "=", "29", "322211"); 75 _mCode128.Rows.Add("30", ">", ">", "30", "212123"); 76 _mCode128.Rows.Add("31", "?", "?", "31", "212321"); 77 _mCode128.Rows.Add("32", "@", "@", "32", "232121"); 78 _mCode128.Rows.Add("33", "A", "A", "33", "111323"); 79 _mCode128.Rows.Add("34", "B", "B", "34", "131123"); 80 _mCode128.Rows.Add("35", "C", "C", "35", "131321"); 81 _mCode128.Rows.Add("36", "D", "D", "36", "112313"); 82 _mCode128.Rows.Add("37", "E", "E", "37", "132113"); 83 _mCode128.Rows.Add("38", "F", "F", "38", "132311"); 84 _mCode128.Rows.Add("39", "G", "G", "39", "211313"); 85 _mCode128.Rows.Add("40", "H", "H", "40", "231113"); 86 _mCode128.Rows.Add("41", "I", "I", "41", "231311"); 87 _mCode128.Rows.Add("42", "J", "J", "42", "112133"); 88 _mCode128.Rows.Add("43", "K", "K", "43", "112331"); 89 _mCode128.Rows.Add("44", "L", "L", "44", "132131"); 90 _mCode128.Rows.Add("45", "M", "M", "45", "113123"); 91 _mCode128.Rows.Add("46", "N", "N", "46", "113321"); 92 _mCode128.Rows.Add("47", "O", "O", "47", "133121"); 93 _mCode128.Rows.Add("48", "P", "P", "48", "313121"); 94 _mCode128.Rows.Add("49", "Q", "Q", "49", "211331"); 95 _mCode128.Rows.Add("50", "R", "R", "50", "231131"); 96 _mCode128.Rows.Add("51", "S", "S", "51", "213113"); 97 _mCode128.Rows.Add("52", "T", "T", "52", "213311"); 98 _mCode128.Rows.Add("53", "U", "U", "53", "213131"); 99 _mCode128.Rows.Add("54", "V", "V", "54", "311123"); 100 _mCode128.Rows.Add("55", "W", "W", "55", "311321"); 101 _mCode128.Rows.Add("56", "X", "X", "56", "331121"); 102 _mCode128.Rows.Add("57", "Y", "Y", "57", "312113"); 103 _mCode128.Rows.Add("58", "Z", "Z", "58", "312311"); 104 _mCode128.Rows.Add("59", "[", "[", "59", "332111"); 105 _mCode128.Rows.Add("60", "\\", "\\", "60", "314111"); 106 _mCode128.Rows.Add("61", "]", "]", "61", "221411"); 107 _mCode128.Rows.Add("62", "^", "^", "62", "431111"); 108 _mCode128.Rows.Add("63", "_", "_", "63", "111224"); 109 _mCode128.Rows.Add("64", "NUL", "`", "64", "111422"); 110 _mCode128.Rows.Add("65", "SOH", "a", "65", "121124"); 111 _mCode128.Rows.Add("66", "STX", "b", "66", "121421"); 112 _mCode128.Rows.Add("67", "ETX", "c", "67", "141122"); 113 _mCode128.Rows.Add("68", "EOT", "d", "68", "141221"); 114 _mCode128.Rows.Add("69", "ENQ", "e", "69", "112214"); 115 _mCode128.Rows.Add("70", "ACK", "f", "70", "112412"); 116 _mCode128.Rows.Add("71", "BEL", "g", "71", "122114"); 117 _mCode128.Rows.Add("72", "BS", "h", "72", "122411"); 118 _mCode128.Rows.Add("73", "HT", "i", "73", "142112"); 119 _mCode128.Rows.Add("74", "LF", "j", "74", "142211"); 120 _mCode128.Rows.Add("75", "VT", "k", "75", "241211"); 121 _mCode128.Rows.Add("76", "FF", "I", "76", "221114"); 122 _mCode128.Rows.Add("77", "CR", "m", "77", "413111"); 123 _mCode128.Rows.Add("78", "SO", "n", "78", "241112"); 124 _mCode128.Rows.Add("79", "SI", "o", "79", "134111"); 125 _mCode128.Rows.Add("80", "DLE", "p", "80", "111242"); 126 _mCode128.Rows.Add("81", "DC1", "q", "81", "121142"); 127 _mCode128.Rows.Add("82", "DC2", "r", "82", "121241"); 128 _mCode128.Rows.Add("83", "DC3", "s", "83", "114212"); 129 _mCode128.Rows.Add("84", "DC4", "t", "84", "124112"); 130 _mCode128.Rows.Add("85", "NAK", "u", "85", "124211"); 131 _mCode128.Rows.Add("86", "SYN", "v", "86", "411212"); 132 _mCode128.Rows.Add("87", "ETB", "w", "87", "421112"); 133 _mCode128.Rows.Add("88", "CAN", "x", "88", "421211"); 134 _mCode128.Rows.Add("89", "EM", "y", "89", "212141"); 135 _mCode128.Rows.Add("90", "SUB", "z", "90", "214121"); 136 _mCode128.Rows.Add("91", "ESC", "{", "91", "412121"); 137 _mCode128.Rows.Add("92", "FS", "|", "92", "111143"); 138 _mCode128.Rows.Add("93", "GS", "}", "93", "111341"); 139 _mCode128.Rows.Add("94", "RS", "~", "94", "131141"); 140 _mCode128.Rows.Add("95", "US", "DEL", "95", "114113"); 141 _mCode128.Rows.Add("96", "FNC3", "FNC3", "96", "114311"); 142 _mCode128.Rows.Add("97", "FNC2", "FNC2", "97", "411113"); 143 _mCode128.Rows.Add("98", "SHIFT", "SHIFT", "98", "411311"); 144 _mCode128.Rows.Add("99", "CODEC", "CODEC", "99", "113141"); 145 _mCode128.Rows.Add("100", "CODEB", "FNC4", "CODEB", "114131"); 146 _mCode128.Rows.Add("101", "FNC4", "CODEA", "CODEA", "311141"); 147 _mCode128.Rows.Add("102", "FNC1", "FNC1", "FNC1", "411131"); 148 _mCode128.Rows.Add("103", "StartA", "StartA", "StartA", "211412"); 149 _mCode128.Rows.Add("104", "StartB", "StartB", "StartB", "211214"); 150 _mCode128.Rows.Add("105", "StartC", "StartC", "StartC", "211232"); 151 _mCode128.Rows.Add("106", "Stop", "Stop", "Stop", "2331112"); 152 #endregion 153 } 154 155 /// <summary> 156 /// 获取128图形 157 /// </summary> 158 /// <param name="pText">文字</param> 159 /// <param name="pCode">编码</param> 160 /// <returns>图形</returns> 161 public Bitmap GetCodeImage(string pText, Encode pCode) 162 { 163 string viewText = pText; 164 string text = ""; 165 IList<int> textNumb = new List<int>(); 166 int examine; //首位 167 switch (pCode) 168 { 169 case Encode.Code128C: 170 examine = 105; 171 if ((pText.Length & 1) != 0) throw new Exception("128C长度必须是偶数"); 172 while (pText.Length != 0) 173 { 174 var temp = 0; 175 try 176 { 177 var codeNumb128 = Int32.Parse(pText.Substring(0, 2)); 178 } 179 catch 180 { 181 throw new Exception("128C必须是数字!"); 182 } 183 text += GetValue(pCode, pText.Substring(0, 2), ref temp); 184 textNumb.Add(temp); 185 pText = pText.Remove(0, 2); 186 } 187 break; 188 case Encode.Ean128: 189 examine = 105; 190 if ((pText.Length & 1) != 0) throw new Exception("EAN128长度必须是偶数"); 191 textNumb.Add(102); 192 text += "411131"; 193 while (pText.Length != 0) 194 { 195 var temp = 0; 196 try 197 { 198 var codeNumb128 = Int32.Parse(pText.Substring(0, 2)); 199 } 200 catch 201 { 202 throw new Exception("128C必须是数字!"); 203 } 204 text += GetValue(Encode.Code128C, pText.Substring(0, 2), ref temp); 205 textNumb.Add(temp); 206 pText = pText.Remove(0, 2); 207 } 208 break; 209 default: 210 examine = pCode == Encode.Code128A ? 103 : 104; 211 212 while (pText.Length != 0) 213 { 214 var temp = 0; 215 var valueCode = GetValue(pCode, pText.Substring(0, 1), ref temp); 216 if (valueCode.Length == 0) throw new Exception("无效的字符集!" + pText.Substring(0, 1)); 217 text += valueCode; 218 textNumb.Add(temp); 219 pText = pText.Remove(0, 1); 220 } 221 break; 222 } 223 if (textNumb.Count == 0) throw new Exception("错误的编码,无数据"); 224 text = text.Insert(0, GetValue(examine)); //获取开始位 225 226 for (int i = 0; i != textNumb.Count; i++) 227 { 228 examine += textNumb[i] * (i + 1); 229 } 230 examine = examine % 103; //获得严效位 231 text += GetValue(examine); //获取严效位 232 text += "2331112"; //结束位 233 var codeImage = GetImage(text); 234 GetViewText(codeImage, viewText); 235 return codeImage; 236 } 237 238 /// <summary> 239 /// 获取目标对应的数据 240 /// </summary> 241 /// <param name="pCode">编码</param> 242 /// <param name="pValue">数值 A b 30</param> 243 /// <param name="pSetId">返回编号</param> 244 /// <returns>编码</returns> 245 private string GetValue(Encode pCode, string pValue, ref int pSetId) 246 { 247 if (_mCode128 == null) return ""; 248 var row = _mCode128.Select(pCode + "='" + pValue + "'"); 249 if (row.Length != 1) throw new Exception("错误的编码" + pValue); 250 pSetId = Int32.Parse(row[0]["ID"].ToString()); 251 return row[0]["BandCode"].ToString(); 252 } 253 254 /// <summary> 255 /// 根据编号获得条纹 256 /// </summary> 257 /// <param name="pCodeId"></param> 258 /// <returns></returns> 259 private string GetValue(int pCodeId) 260 { 261 var row = _mCode128.Select("ID='" + pCodeId + "'"); 262 if (row.Length != 1) throw new Exception("验效位的编码错误" + pCodeId); 263 return row[0]["BandCode"].ToString(); 264 } 265 266 /// <summary> 267 /// 获得条码图形 268 /// </summary> 269 /// <param name="pText">文字</param> 270 /// <returns>图形</returns> 271 private Bitmap GetImage(string pText) 272 { 273 var value = pText.ToCharArray(); 274 var width = 0; 275 for (int i = 0; i != value.Length; i++) 276 { 277 width += Int32.Parse(value[i].ToString(CultureInfo.InvariantCulture)) * (_mMagnify + 1); 278 } 279 var codeImage = new Bitmap(width, (int)_mHeight); 280 var garphics = Graphics.FromImage(codeImage); 281 //Pen _Pen; 282 var lenEx = 0; 283 for (int i = 0; i != value.Length; i++) 284 { 285 int valueNumb = Int32.Parse(value[i].ToString(CultureInfo.InvariantCulture)) * (_mMagnify + 1); //获取宽和放大系数 286 garphics.FillRectangle((i & 1) != 0 ? Brushes.White : Brushes.Black, 287 new Rectangle(lenEx, 0, valueNumb, (int)_mHeight)); 288 //_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height)); 289 lenEx += valueNumb; 290 } 291 garphics.Dispose(); 292 return codeImage; 293 } 294 295 /// <summary> 296 /// 显示可见条码文字 如果小于40 不显示文字 297 /// </summary> 298 /// <param name="pBitmap">图形</param> 299 /// <param name="pViewText"></param> 300 private void GetViewText(Bitmap pBitmap, string pViewText) 301 { 302 if (_mValueFont == null) return; 303 304 Graphics graphics = Graphics.FromImage(pBitmap); 305 SizeF drawSize = graphics.MeasureString(pViewText, _mValueFont); 306 if (drawSize.Height > pBitmap.Height - 10 || drawSize.Width > pBitmap.Width) 307 { 308 graphics.Dispose(); 309 return; 310 } 311 312 int starY = pBitmap.Height - (int)drawSize.Height; 313 graphics.FillRectangle(Brushes.White, new Rectangle(0, starY, pBitmap.Width, (int)drawSize.Height)); 314 graphics.DrawString(pViewText, _mValueFont, Brushes.Black, 5, starY); 315 } 316 } 317 }
3.二维码生成类(QRCodeHelper)引用:ThoughtWorks.QRCode.dll
1 public class QRCodeHelper 2 { 3 private System.Drawing.Printing.PrintPageEventArgs e; 4 private int Top = 20;//提供一个基准Top,用户不提供Top时,就取该Top,每次有内容增加都会刷新 5 private int Left = 20;//提供一个基准Left,用户不提供Left时,就取该Left 6 private int Interval = 5;//提供一个基准间隔值 7 8 9 #region 根据链接生成二维码 10 /// <summary> 11 /// 根据链接获取二维码 12 /// 所需参数:URL;距左Left值;距上Top值 13 /// 默认值:无 14 /// </summary> 15 /// <param name="url">链接</param 16 /// <param name="left">二维码:x</param> 17 /// <param name="top">二维码:y</param> 18 /// <returns>返回二维码图片的高度</returns> 19 public int DrawQRCodeBmp(string url, int left, int top) 20 { 21 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); 22 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; 23 qrCodeEncoder.QRCodeScale = 4; 24 qrCodeEncoder.QRCodeVersion = 0; 25 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; 26 Image bmp = qrCodeEncoder.Encode(url); 27 e.Graphics.DrawImage(bmp, new Point(left, top));//不同的URL图片大小不同,可以根据需要调整left坐标 28 29 Top += bmp.Height + this.Interval; 30 31 return bmp.Height; 32 } 33 34 /// <summary> 35 /// 根据链接获取二维码 36 /// 所需参数:URL;距左Left值 37 /// 默认值:距上Top值:顺位值 38 /// </summary> 39 /// <param name="url">链接</param 40 /// <param name="left">二维码:x</param> 41 /// <returns>返回二维码图片的高度</returns> 42 public int DrawQRCodeBmp(string url, int left) 43 { 44 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); 45 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; 46 qrCodeEncoder.QRCodeScale = 4; 47 qrCodeEncoder.QRCodeVersion = 0; 48 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; 49 Image bmp = qrCodeEncoder.Encode(url); 50 e.Graphics.DrawImage(bmp, new Point(left, this.Top));//不同的URL图片大小不同,可以根据需要调整left坐标 51 52 Top += bmp.Height + this.Interval; 53 54 return bmp.Height; 55 } 56 /// <summary> 57 /// 根据链接获取二维码 58 /// 所需参数:URL 59 /// 默认值:距左Left值;距上Top值:顺位值 60 /// </summary> 61 /// <param name="url">链接</param> 62 /// <returns></returns> 63 //public int DrawQRCodeBmp(string url) 64 //{ 65 // QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); 66 // qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; 67 // qrCodeEncoder.QRCodeScale = 4; 68 // qrCodeEncoder.QRCodeVersion = 0; 69 // qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; 70 // Image bmp = qrCodeEncoder.Encode(url); 71 // e.Graphics.DrawImage(bmp, new Point(Left, Top));//不同的URL图片大小不同,可以根据需要调整left坐标 72 73 // Top += bmp.Height + this.Interval; 74 75 // return bmp.Height; 76 //} 77 #endregion 78 79 public Bitmap DrawQRCodeBmp(string url) 80 { 81 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); 82 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; 83 qrCodeEncoder.QRCodeScale = 4; 84 qrCodeEncoder.QRCodeVersion = 0; 85 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; 86 return qrCodeEncoder.Encode(url); 87 } 88 }
4.form窗体调用:
1 /// <summary> 2 /// 获取条形码保存路径 3 /// </summary> 4 /// <returns></returns> 5 private string GetBarCodeMap() 6 { 7 var path = string.Empty; 8 var code = DateTime.Now.ToString("yyyymmddhhmmss"); 9 10 path = System.AppDomain.CurrentDomain.BaseDirectory + "BarCode\\" + Guid.NewGuid().ToString() + ".gif"; 11 12 Code128 _Code = new Code128(); 13 _Code.Height = 61; 14 _Code.ValueFont = new Font("宋体", 10); 15 System.Drawing.Bitmap imgTemp = _Code.GetCodeImage(code, Code128.Encode.Code128A); 16 imgTemp.Save(path, System.Drawing.Imaging.ImageFormat.Gif); 17 18 return path; 19 } 20 21 /// <summary> 22 /// 获取二维码保存路径 23 /// </summary> 24 /// <returns></returns> 25 private string GetQRCodeMap() 26 { 27 var path = string.Empty; 28 var url = "www.baidu.com"; 29 30 path = System.AppDomain.CurrentDomain.BaseDirectory + "BarCode\\" + Guid.NewGuid().ToString() + ".gif"; 31 32 QRCodeHelper _QRCode = new QRCodeHelper(); 33 System.Drawing.Bitmap imgTemp = _QRCode.DrawQRCodeBmp(url); 34 imgTemp.Save(path, System.Drawing.Imaging.ImageFormat.Gif); 35 36 return path; 37 }