生成条形码:
引用BarcodeX.dll --- 该dll下载,返回Image类型
public Image DrawBarCode(string serialNum, int width, int height) { Fath.BarcodeX barCode = new Fath.BarcodeX();//创建条码生成对象 //生成条形码 barCode.Text = serialNum;//条码数据 barCode.Symbology = Fath.bcType.Code128;//设置条码格式 barCode.ShowText = true;//同时显示文本 return barCode.Image(width, height); }
生成二维码:
引用ThoughtWorks.QRCode.dll --- 该dll下载,返回Image类型,不知道怎么控制二维码大小,还请各位指教
using ThoughtWorks.QRCode.Codec;
public Image DrawQRCodeBmp(string url) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrCodeEncoder.QRCodeScale = 4; qrCodeEncoder.QRCodeVersion = 0; qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; return qrCodeEncoder.Encode(url); }
效果:
再分享一个可以集成好的生成条形码,二维码的dll,不多说,直接上代码:
Printer:


1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Drawing; 7 using ThoughtWorks.QRCode.Codec; 8 9 namespace PrintFun 10 { 11 public class Printer 12 { 13 private System.Drawing.Printing.PrintPageEventArgs e; 14 private int Top = 20;//提供一个基准Top,用户不提供Top时,就取该Top,每次有内容增加都会刷新 15 private int Left = 20;//提供一个基准Left,用户不提供Left时,就取该Left 16 private int Interval = 5;//提供一个基准间隔值 17 18 #region 构造函数重载 19 ///20 /// 打印类 21 /// 所需参数:System.Drawing.Printing.PrintPageEventArgs;第一行内容距左Left值;第一行内容距上Top值;每行内容间隔值 22 /// 默认值:无 23 /// 24 /// 提供打印数据 25 /// 指定第一行数据的x 26 /// 指定第一行数据的y 27 /// 指定每行数据间隔值 28 public Printer(System.Drawing.Printing.PrintPageEventArgs e, int Left, int Top,int Interval=20) 29 { 30 this.e = e; 31 this.Left = Left; 32 this.Top = Top; 33 this.Interval = Interval; 34 } 35 36 /// 37 /// 打印类 38 /// 所需参数:System.Drawing.Printing.PrintPageEventArgs;每行内容间隔值 39 /// 默认值:第一行内容距左Left值;第一行内容距上Top值 40 /// 41 /// 提供打印数据 42 /// 指定每行数据间隔值 43 public Printer(System.Drawing.Printing.PrintPageEventArgs e, int Interval) 44 { 45 this.e = e; 46 this.Interval = Interval; 47 } 48 49 /// 50 /// 打印类 51 /// 所需参数:System.Drawing.Printing.PrintPageEventArgs 52 /// 默认值:每行内容间隔值;第一行内容距左Left值;第一行内容距上Top值 53 /// 54 /// 提供打印数据 55 public Printer(System.Drawing.Printing.PrintPageEventArgs e) 56 { 57 this.e = e; 58 } 59 #endregion 60 61 #region 根据流水号生成条形码 62 63 /// 64 /// 根据流水号生成条形码 65 /// 所需参数:流水号;距左Left值;距上Top值;条形码宽度;条形码高度 66 /// 默认值:无 67 /// 68 /// 流水号 69 /// 指定条形码x 70 /// 指定条形码y 71 /// 指定条形码宽度 72 /// 指定条形码高度 73 public void DrawBarCode(string serialNum, int left, int top,int width,int height) 74 { 75 Fath.BarcodeX barCode = new Fath.BarcodeX();//创建条码生成对象 76 //生成条形码 77 barCode.Text = serialNum;//条码数据 78 barCode.Symbology = Fath.bcType.Code128;//设置条码格式 79 barCode.ShowText = true;//同时显示文本 80 81 e.Graphics.DrawImage(barCode.Image(width, height), new Point(left, top));//画条形码 82 83 this.Top += height+this.Interval; 84 } 85 86 /// 87 /// 根据流水号生成条形码 88 /// 所需参数:流水号;距左Left值;距上Top值 89 /// 默认值:条形码宽度:240像素;条形码高度:50像素 90 /// 91 /// 流水号 92 /// 指定条形码x 93 /// 指定条形码y 94 public void DrawBarCode(string serialNum, int left, int top) 95 { 96 Fath.BarcodeX barCode = new Fath.BarcodeX();//创建条码生成对象 97 //生成条形码 98 barCode.Text = serialNum;//条码数据 99 barCode.Symbology = Fath.bcType.Code128;//设置条码格式 100 barCode.ShowText = true;//同时显示文本 101 102 e.Graphics.DrawImage(barCode.Image(240, 50), new Point(left, top));//画条形码 103 104 this.Top += 50+this.Interval; 105 } 106 107 /// 108 /// 根据流水号生成条形码 109 /// 所需参数:流水号 110 /// 默认值:距左Left值:25像素;距上Top值:顺位值;条形码宽度:240像素;条形码高度:50像素 111 /// 112 /// 流水号 113 public void DrawBarCode(string serialNum) 114 { 115 Fath.BarcodeX barCode = new Fath.BarcodeX();//创建条码生成对象 116 //生成条形码 117 barCode.Text = serialNum;//条码数据 118 barCode.Symbology = Fath.bcType.Code128;//设置条码格式 119 barCode.ShowText = true;//同时显示文本 120 121 e.Graphics.DrawImage(barCode.Image(240, 50), new Point(this.Left, this.Top));//画条形码 122 123 this.Top += 50 + this.Interval; 124 } 125 126 #endregion 127 128 #region 根据链接生成二维码 129 /// 130 /// 根据链接获取二维码 131 /// 所需参数:URL;距左Left值;距上Top值 132 /// 默认值:无 133 /// 134 /// 链接135 /// 二维码:x 136 /// 二维码:y 137 /// 返回二维码图片的高度 138 public int DrawQRCodeBmp(string url,int left,int top) 139 { 140 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); 141 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; 142 qrCodeEncoder.QRCodeScale = 4; 143 qrCodeEncoder.QRCodeVersion = 0; 144 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; 145 Image bmp = qrCodeEncoder.Encode(url); 146 e.Graphics.DrawImage(bmp, new Point(left, top));//不同的URL图片大小不同,可以根据需要调整left坐标 147 148 Top += bmp.Height + this.Interval; 149 150 return bmp.Height; 151 } 152 153 /// 154 /// 根据链接获取二维码 155 /// 所需参数:URL;距左Left值 156 /// 默认值:距上Top值:顺位值 157 /// 158 /// 链接159 /// 二维码:x 160 /// 返回二维码图片的高度 161 public int DrawQRCodeBmp(string url, int left) 162 { 163 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); 164 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; 165 qrCodeEncoder.QRCodeScale = 4; 166 qrCodeEncoder.QRCodeVersion = 0; 167 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; 168 Image bmp = qrCodeEncoder.Encode(url); 169 e.Graphics.DrawImage(bmp, new Point(left, this.Top));//不同的URL图片大小不同,可以根据需要调整left坐标 170 171 Top += bmp.Height + this.Interval; 172 173 return bmp.Height; 174 } 175 /// 176 /// 根据链接获取二维码 177 /// 所需参数:URL 178 /// 默认值:距左Left值;距上Top值:顺位值 179 /// 180 /// 链接 181 /// 182 public int DrawQRCodeBmp(string url) 183 { 184 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); 185 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; 186 qrCodeEncoder.QRCodeScale = 4; 187 qrCodeEncoder.QRCodeVersion = 0; 188 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; 189 Image bmp = qrCodeEncoder.Encode(url); 190 e.Graphics.DrawImage(bmp, new Point(Left, Top));//不同的URL图片大小不同,可以根据需要调整left坐标 191 192 Top += bmp.Height + this.Interval; 193 194 return bmp.Height; 195 } 196 #endregion 197 198 #region 打印内容 199 200 #region 打印单条内容 201 /// 202 /// 打印内容 203 /// 所需参数:打印内容;内容字体;内容字体颜色;距左Left值;距上Top值 204 /// 默认值:无 205 /// 206 /// 需要打印的内容 207 /// 内容字体 208 /// 内容字体颜色,提供Brushes.Color格式 209 /// 打印区域的左边界 210 /// 打印区域的上边界 211 public void DrawContent(string content, Font contentFont, Brush contentBrush, int left , int top ) 212 { 213 this.e.Graphics.DrawString(content, contentFont, contentBrush, left, top, new StringFormat()); 214 215 Top += Convert.ToInt32(contentFont.GetHeight(e.Graphics)) + this.Interval; 216 } 217 218 /// 219 /// 打印内容 220 /// 所需参数:打印内容;内容字体;内容字体颜色;距左Left值 221 /// 默认值:距上Top值:顺位值 222 /// 223 /// 需要打印的内容 224 /// 内容字体 225 /// 内容字体颜色,提供Brushes.Color格式 226 /// 打印区域的左边界 227 public void DrawContent(string content, Font contentFont, Brush contentBrush, int left) 228 { 229 this.e.Graphics.DrawString(content, contentFont, contentBrush, left, this.Top, new StringFormat()); 230 231 Top += Convert.ToInt32(contentFont.GetHeight(e.Graphics)) + this.Interval; 232 } 233 234 /// 235 /// 打印内容 236 /// 所需参数:打印内容;内容字体;内容字体颜色 237 /// 默认值:距左Left值:25像素;距上Top值:顺位值 238 /// 239 /// 需要打印的内容 240 /// 内容字体 241 /// 内容字体颜色,提供Brushes.Color格式 242 public void DrawContent(string content, Font contentFont, Brush contentBrush) 243 { 244 this.e.Graphics.DrawString(content, contentFont, contentBrush, Left, Top, new StringFormat()); 245 246 Top += Convert.ToInt32(contentFont.GetHeight(e.Graphics)) + this.Interval; 247 } 248 249 /// 250 /// 打印内容 251 /// 所需参数:打印内容;内容字体 252 /// 默认值:字体:仿宋8号;内容字体颜色:黑色;距左Left值:25像素;距上Top值:顺位值 253 /// 254 /// 需要打印的内容 255 /// 内容字体 256 public void DrawContent(string content, Font contentFont) 257 { 258 this.e.Graphics.DrawString(content, contentFont, Brushes.Black, Left, Top, new StringFormat()); 259 260 Top += Convert.ToInt32(contentFont.GetHeight(e.Graphics)) + this.Interval; 261 } 262 263 /// 264 /// 打印内容 265 /// 所需参数:打印内容 266 /// 默认值:字体:仿宋8号;内容字体颜色:黑色;距左Left值:25像素;距上Top值:顺位值 267 /// 268 /// 需要打印的内容 269 /// 内容字体:默认仿宋,8号 270 /// 内容字体颜色:默认黑色 271 public void DrawContent(string content) 272 { 273 Font contentFont = new Font("仿宋", 8); 274 this.e.Graphics.DrawString(content, contentFont, Brushes.Black, Left, Top, new StringFormat()); 275 276 Top += Convert.ToInt32(contentFont.GetHeight(e.Graphics)) + this.Interval; 277 } 278 #endregion 279 280 #region 打印多条内容 281 282 /// 283 /// 打印内容 284 /// 所需参数:打印内容集合;内容字体;内容字体颜色;距左Left值;距上Top值 285 /// 默认值:无 286 /// 287 /// 需要打印的内容 288 /// 内容字体 289 /// 内容字体颜色,提供Brushes.Color格式 290 /// 打印区域的左边界 291 /// 打印区域的上边界 292 public void DrawContent(List<string> contentList, Font contentFont,Brush contentBrush, int left, int top) 293 { 294 int height = Convert.ToInt32(contentFont.GetHeight(e.Graphics)); 295 int nextTop = Top + this.Interval; 296 for (int i=0;i< contentList.Count;i++) 297 { 298 this.e.Graphics.DrawString(contentList[i], contentFont, contentBrush, left, nextTop , new StringFormat()); 299 nextTop += height; 300 } 301 Top += height * contentList.Count + this.Interval ; 302 } 303 304 /// 305 /// 打印内容: 306 /// 所需参数:打印内容集合;内容字体;内容字体颜色 307 /// 默认值:距左Left值:25像素;距上Top值:顺位值 308 /// 309 /// 需要打印的内容 310 /// 内容字体 311 /// 内容字体颜色,提供Brushes.Color格式 312 public void DrawContent(List<string> contentList, Font contentFont, Brush contentBrush) 313 { 314 int height = Convert.ToInt32(contentFont.GetHeight(e.Graphics)); 315 int nextTop = Top + this.Interval; 316 for (int i = 0; i < contentList.Count; i++) 317 { 318 this.e.Graphics.DrawString(contentList[i], contentFont, contentBrush, Left, nextTop, new StringFormat()); 319 nextTop += height; 320 } 321 Top += height * contentList.Count + this.Interval ; 322 } 323 324 /// 325 /// 打印内容 326 /// 所需参数:打印内容集合;内容字体 327 /// 默认值:内容字体颜色:黑色;距左Left值:25像素;距上Top值:顺位值 328 /// 329 /// 需要打印的内容 330 /// 内容字体 331 /// 内容字体颜色:默认黑色 332 public void DrawContent(List<string> contentList, Font contentFont) 333 { 334 int height = Convert.ToInt32(contentFont.GetHeight(e.Graphics)); 335 int nextTop = Top + this.Interval; 336 for (int i = 0; i < contentList.Count; i++) 337 { 338 339 this.e.Graphics.DrawString(contentList[i], contentFont, Brushes.Black, Left, nextTop, new StringFormat()); 340 nextTop += height; 341 } 342 Top += height * contentList.Count + this.Interval ; 343 } 344 345 /// 346 /// 打印内容 347 /// 所需参数:打印内容集合 348 /// 默认值:字体:仿宋8号;内容字体颜色:黑色;距左Left值:25像素;距上Top值:顺位值 349 /// 350 /// 需要打印的内容 351 public void DrawContent(List<string> contentList) 352 { 353 Font contentFont = new Font("仿宋", 8); 354 int height = Convert.ToInt32(contentFont.GetHeight(e.Graphics)); 355 int nextTop = Top + this.Interval; 356 for (int i = 0; i < contentList.Count; i++) 357 { 358 this.e.Graphics.DrawString(contentList[i], contentFont, Brushes.Black, Left, nextTop, new StringFormat()); 359 nextTop += height; 360 } 361 Top += height * contentList.Count + this.Interval ; 362 } 363 #endregion 364 #endregion 365 366 #region 分界线 367 /// 368 /// 分界线 369 /// 所需参数:起点Left坐标;起点Top坐标;终点Left坐标;终点Top坐标;线颜色;线宽:1像素 370 /// 默认值:线宽:1像素 371 /// 372 /// 分界线起点,起点与纸张左侧距离 373 /// 分界线起点,起点与纸张上侧距离 374 /// 分界线终点,终点与纸张左侧距离 375 /// 分界线终点,终点与纸张上侧距离 376 /// 分界线颜色 377 /// 分界线宽度,默认值为:1 378 public void DrawDoundary(int startPointLeft, int startPointTop, int endPointLeft, int endPointTop, Color lineColor, int lineWidth = 1) 379 { 380 Pen pen = new Pen(lineColor, lineWidth); 381 e.Graphics.DrawLine(pen, new Point(startPointLeft, startPointTop), new Point(endPointLeft, endPointTop)); 382 Top += lineWidth + this.Interval; 383 } 384 385 /// 386 /// 分界线 387 /// 所需参数:起点Left坐标;起点Top坐标;分界线长度;线颜色;线宽:1像素 388 /// 默认值:线宽:1像素 389 /// 390 /// 分界线起点,起点与纸张左侧距离 391 /// 分界线起点,起点与纸张上侧距离 392 /// 分界线长度 393 /// 分界线颜色 394 /// 分界线宽度,默认值为:1 395 public void DrawDoundary(int startPointLeft, int startPointTop, int lineLength, Color lineColor, int lineWidth = 1) 396 { 397 Pen pen = new Pen(lineColor, lineWidth); 398 e.Graphics.DrawLine(pen, new Point(startPointLeft, startPointTop), new Point(startPointLeft+lineLength, startPointTop)); 399 Top += lineWidth + this.Interval; 400 } 401 402 /// 403 /// 分界线 404 /// 所需参数:分界线长度;线颜色;线宽:1像素 405 /// 默认值:距上Top值:顺位值;线宽:1像素 406 /// 407 /// 分界线长度 408 /// 分界线颜色 409 /// 分界线宽度,默认值为:1 410 public void DrawDoundary(int lineLength, Color lineColor, int lineWidth = 1) 411 { 412 Pen pen = new Pen(lineColor, lineWidth); 413 e.Graphics.DrawLine(pen, new Point(Left, Top), new Point(Left + lineLength, Top)); 414 Top += lineWidth + this.Interval; 415 } 416 417 /// 418 /// 分界线 419 /// 所需参数:分界线长度;线宽:1像素 420 /// 默认值:距左Left值:25像素;距上Top值:顺位值;线宽:1像素 421 /// 422 /// 分界线长度 423 /// 分界线宽度,默认值为:1 424 public void DrawDoundary(int lineLength, int lineWidth = 1) 425 { 426 Pen pen = new Pen(Color.Green, lineWidth); 427 e.Graphics.DrawLine(pen, new Point(Left, Top), new Point(Left + lineLength, Top)); 428 Top += lineWidth + this.Interval; 429 } 430 431 /// 432 /// 分界线 433 /// 所需参数:起点Left坐标;终点Left坐标;线宽:1像素 434 /// 默认值:距上Top值:顺位值;线颜色:绿色;线宽:1像素 435 /// 436 /// 开始点 437 /// 结束点 438 /// 线宽:默认1像素 439 /// 分界线颜色:默认绿色 440 public void DrawDoundary(int startPointLeft,int endPointLeft, int lineWidth = 1) 441 { 442 Pen pen = new Pen(Color.Green, lineWidth); 443 e.Graphics.DrawLine(pen, new Point(startPointLeft, Top), new Point(endPointLeft, Top)); 444 Top += lineWidth + this.Interval; 445 } 446 447 /// 448 /// 分界线 449 /// 所需参数:起点Left坐标;终点Left坐标;线颜色;线宽:1像素 450 /// 默认值:距上Top值:顺位值;线宽:1像素 451 /// 452 /// 开始点 453 /// 结束点 454 /// 线宽:默认1像素 455 /// 分界线颜色 456 public void DrawDoundary(int startPointLeft, int endPointLeft, Color lineColor, int lineWidth = 1) 457 { 458 Pen pen = new Pen(lineColor, lineWidth); 459 e.Graphics.DrawLine(pen, new Point(startPointLeft, Top), new Point(endPointLeft, Top)); 460 Top += lineWidth + this.Interval; 461 } 462 #endregion 463 } 464 }
测试:


1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Drawing.Printing; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 using PrintFun; 12 13 namespace PrintDemo 14 { 15 public partial class Form1 : Form 16 { 17 public Form1() 18 { 19 InitializeComponent(); 20 21 //关联打印对象的事件 22 this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.LotteryPrintPage); 23 } 24 private PrintDocument printDoc = new PrintDocument();//创建打印对象 25 26 27 private void button1_Click(object sender, EventArgs e) 28 { 29 this.printDoc.Print(); 30 } 31 32 //具体打印实现过程 33 //private void LotteryPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 34 //{ 35 // int left = 2; //打印区域的左边界 36 // int top = 70;//打印区域的上边界 37 // Font titlefont = new Font("仿宋", 10);//标题字体 38 // Font font = new Font("仿宋", 8);//内容字体 39 // Printer objPrint = new Printer(e); 40 41 // //生成彩票信息 42 43 // //条形码 44 // string serialNum = DateTime.Now.ToString("yyyyMMddHHmmssms");//流水号(生成条码使用) 45 // objPrint.DrawBarCode(serialNum, 20, 5); 46 // //标题 47 // objPrint.DrawContent("天津百万奖彩票中心", titlefont, Brushes.Blue, left + 20, top); 48 // //分界线 49 // objPrint.DrawDoundary((int)left - 2, (int)top + 20, (int)left + (int)180, (int)top + 20, Color.Green, 1); 50 51 // //写入内容 52 // Listnum = new List 53 // num.Add("4 9 6 1 0 3 9"); 54 // num.Add("7 0 1 2 3 5 5"); 55 // num.Add("7 1 3 2 4 6 5"); 56 // num.Add("5 7 8 2 7 8 5"); 57 // num.Add("2 1 2 1 1 6 9"); 58 59 // objPrint.DrawContent(num, font, Brushes.Blue, left, (int)(top + titlefont.GetHeight(e.Graphics))); 60 61 // //分界线 62 // float topPoint = titlefont.GetHeight(e.Graphics) + font.GetHeight(e.Graphics) * (num.Count) + 22; 63 // objPrint.DrawDoundary((int)left - 2, (int)top + (int)topPoint, (int)left + (int)180, (int)top + (int)topPoint, Color.Green, 1); 64 65 // string time = "购买时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"); 66 // objPrint.DrawContent(time, font, Brushes.Blue, left, top + (int)titlefont.GetHeight(e.Graphics) 67 // + (int)font.GetHeight(e.Graphics) * (num.Count + 1) + 12); 68 69 // //二维码图片left和top坐标 70 // int qrcodetop = (int)(top + titlefont.GetHeight(e.Graphics) + font.GetHeight(e.Graphics) * (num.Count + 3) + 12); 71 // int qrcodeleft = (int)left + 32; 72 // int height = objPrint.DrawQRCodeBmp("http://www.baidu.com", qrcodeleft, qrcodetop); 73 74 // objPrint.DrawContent("扫描二维码可直接查询兑奖结果", font, Brushes.Blue, left, qrcodetop + height + 10); 75 //} 76 77 78 ///(); 79 /// 全部使用默认值 80 /// 81 /// 82 /// 83 //private void LotteryPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 84 //{ 85 // Printer objPrint = new Printer(e); 86 87 // //生成彩票信息 88 // //条形码 89 // string serialNum = DateTime.Now.ToString("yyyyMMddHHmmssms");//流水号(生成条码使用) 90 // objPrint.DrawBarCode(serialNum); 91 // //标题 92 // objPrint.DrawContent("天津百万奖彩票中心"); 93 // //分界线 94 // objPrint.DrawDoundary(100); 95 // //写入内容 96 // List num = new List 97 // num.Add("4 9 6 1 0 3 9"); 98 // num.Add("7 0 1 2 3 5 5"); 99 // num.Add("7 1 3 2 4 6 5"); 100 // num.Add("5 7 8 2 7 8 5"); 101 // num.Add("2 1 2 1 1 6 9"); 102 // objPrint.DrawContent(num); 103 // //分界线 104 // objPrint.DrawDoundary(100); 105 // //购买时间 106 // string time = "购买时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"); 107 // objPrint.DrawContent(time); 108 // //二维码图片 109 // int height = objPrint.DrawQRCodeBmp("http://www.baidu.com"); 110 // objPrint.DrawContent("扫描二维码可直接查询兑奖结果"); 111 //} 112 113 114 private void LotteryPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 115 { 116 Printer objPrint = new Printer(e); 117 118 //生成彩票信息 119 //条形码 120 string serialNum = DateTime.Now.ToString("yyyyMMddHHmmssms");//流水号(生成条码使用) 121 objPrint.DrawBarCode(serialNum); 122 //标题 123 objPrint.DrawContent("中国百万奖彩票中心",new Font("仿宋",12),Brushes.Black,25); 124 //分界线 125 objPrint.DrawDoundary(150); 126 //写入内容 127 List<string> num = new List<string>(); 128 num.Add("4 9 6 1 0 3 9"); 129 num.Add("7 0 1 2 3 5 5"); 130 num.Add("7 1 3 2 4 6 5"); 131 num.Add("5 7 8 2 7 8 5"); 132 num.Add("2 1 2 1 1 6 9"); 133 objPrint.DrawContent(num); 134 //分界线 135 objPrint.DrawDoundary(150); 136 //购买时间 137 string time = "购买时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"); 138 objPrint.DrawContent(time); 139 //二维码图片 140 int height = objPrint.DrawQRCodeBmp("http://www.baidu.com",40); 141 objPrint.DrawContent("扫描二维码可直接查询兑奖结果"); 142 } 143 } 144 }();
测试结果:
提供了若干方法的重载,可在我github下载完整代码:
https://github.com/EasonDongH/Printer.git
也可以直接下载项目dll使用:
https://files.cnblogs.com/files/EasonDongH/%E6%89%93%E5%8D%B0%E7%9B%B8%E5%85%B3.zip
再分享一个开源的打印与识别二维码、条形码代码,可以控制生成图片的高、宽、边缘等属性。且可存储本地,以及识二维码条形码。
引用dll--ZXing.dll


using System; using System.Drawing; using System.IO; using System.Windows.Forms; using ZXing; using ZXing.Common; using ZXing.QrCode; namespace ZXingDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCreateQR_Click(object sender, EventArgs e) { QrCodeEncodingOptions options = new QrCodeEncodingOptions() { CharacterSet = "UTF-8", DisableECI = true, // Extended Channel Interpretation (ECI) 主要用于特殊的字符集。并不是所有的扫描器都支持这种编码。 ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H, // 纠错级别 Width = this.pBQR.Width, Height = this.pBQR.Height, Margin = 1 // options.Hints,更多属性,也可以在这里添加。 }; BarcodeWriter writer = new BarcodeWriter(); writer.Format = BarcodeFormat.QR_CODE; writer.Options = options; Bitmap bitmap = writer.Write("http://www.baidu.com"); // Write 具备生成、写入两个功能 bitmap.Save(@"C:\2Study\CSharpWorkSpace\Common\BarCode\" + DateTime.Now.ToString("MMddHHmmss") + "QR.png", System.Drawing.Imaging.ImageFormat.Png); this.pBQR.Image = bitmap; } private void btnCreateBarCode_Click(object sender, EventArgs e) { EncodingOptions options = new EncodingOptions() { Width = pBBarCode.Width, Height = pBBarCode.Height, Margin = 1,//图片边缘 PureBarcode = false// 是否是纯码,如果为 false,则会在图片下方显示数字 }; BarcodeWriter writer = new BarcodeWriter(); writer.Format = BarcodeFormat.CODE_128;//writer.Format = BarcodeFormat.ITF; writer.Options = options; Bitmap bitmap = writer.Write("2018031512345"); bitmap.Save(@"C:\2Study\CSharpWorkSpace\Common\BarCode\" + DateTime.Now.ToString("MMddHHmmss") + "BarCode.png", System.Drawing.Imaging.ImageFormat.Png); pBBarCode.Image = bitmap; } private void btnReadQR_Click(object sender, EventArgs e) { BarcodeReader reader = new BarcodeReader(); reader.Options.CharacterSet = "UTF-8"; Result result = null; if (this.pBQR.Image == null) { //从本地读取 FileStream fs = File.OpenRead(@"C:\2Study\CSharpWorkSpace\Common\BarCode\0315094034QR.png"); //OpenRead int fileLength = (int)fs.Length; //获得文件长度 Byte[] byteReault = new Byte[fileLength]; //建立一个字节数组 fs.Read(byteReault, 0, fileLength); //按字节流读取 System.Drawing.Image image = System.Drawing.Image.FromStream(fs); fs.Close(); Bitmap bit = new Bitmap(image); result = reader.Decode(bit); this.pBQR.Image = bit; } else { result = reader.Decode((Bitmap)this.pBQR.Image); } this.lblReadQRResult.Text = result.Text; } private void btnReadBarCode_Click(object sender, EventArgs e) { BarcodeReader reader = new BarcodeReader(); reader.Options.CharacterSet = "UTF-8"; Result result = reader.Decode((Bitmap)this.pBBarCode.Image); this.lblReadBarCodeResult.Text = result.Text; } } }