C#---生成条形码,二维码,分界线并打印

分享一个可以生成条形码,二维码的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         /// <summary>
 20         /// 打印类
 21         /// 所需参数:System.Drawing.Printing.PrintPageEventArgs;第一行内容距左Left值;第一行内容距上Top值;每行内容间隔值
 22         /// 默认值:无
 23         /// </summary>
 24         /// <param name="e">提供打印数据</param>
 25         /// <param name="Left">指定第一行数据的x</param>
 26         /// <param name="Top">指定第一行数据的y</param>
 27         /// <param name="Interval">指定每行数据间隔值,默认:20像素</param>
 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         /// <summary>
 37         /// 打印类
 38         /// 所需参数:System.Drawing.Printing.PrintPageEventArgs;每行内容间隔值
 39         /// 默认值:第一行内容距左Left值:25像素;第一行内容距上Top值:25像素
 40         /// </summary>
 41         /// <param name="e">提供打印数据</param>
 42         /// <param name="Interval">指定每行数据间隔值,默认:20像素</param>
 43         public Printer(System.Drawing.Printing.PrintPageEventArgs e, int Interval)
 44         {
 45             this.e = e;
 46             this.Interval = Interval;
 47         }
 48
 49         /// <summary>
 50         /// 打印类
 51         /// 所需参数:System.Drawing.Printing.PrintPageEventArgs
 52         /// 默认值:每行内容间隔值:25像素;第一行内容距左Left值:25像素;第一行内容距上Top值:25像素
 53         /// </summary>
 54         /// <param name="e">提供打印数据</param>
 55         public Printer(System.Drawing.Printing.PrintPageEventArgs e)
 56         {
 57             this.e = e;
 58         }
 59         #endregion
 60
 61         #region 根据流水号生成条形码
 62
 63         /// <summary>
 64         /// 根据流水号生成条形码
 65         /// 所需参数:流水号;距左Left值;距上Top值;条形码宽度;条形码高度
 66         /// 默认值:无
 67         /// </summary>
 68         /// <param name="serialNum">流水号</param>
 69         /// <param name="left">指定条形码x</param>
 70         /// <param name="top">指定条形码y</param>
 71         /// <param name="width">指定条形码宽度,默认值:240像素</param>
 72         /// <param name="height">指定条形码高度,默认值:50像素</param>
 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;//默认相隔20像素
 84         }
 85
 86         /// <summary>
 87         /// 根据流水号生成条形码
 88         /// 所需参数:流水号;距左Left值;距上Top值
 89         /// 默认值:条形码宽度:240像素;条形码高度:50像素
 90         /// </summary>
 91         /// <param name="serialNum">流水号</param>
 92         /// <param name="left">指定条形码x</param>
 93         /// <param name="top">指定条形码y</param>
 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;//默认相隔20像素
105         }
106
107         /// <summary>
108         /// 根据流水号生成条形码
109         /// 所需参数:流水号
110         /// 默认值:距左Left值:25像素;距上Top值:顺位值;条形码宽度:240像素;条形码高度:50像素
111         /// </summary>
112         /// <param name="serialNum">流水号</param>
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;//默认相隔20像素
124         }
125
126         #endregion
127
128         #region 根据链接生成二维码
129         /// <summary>
130         /// 根据链接获取二维码
131         /// 所需参数:URL;距左Left值;距上Top值
132         /// 默认值:无
133         /// </summary>
134         /// <param name="url">链接</param
135         /// <param name="left">二维码:x</param>
136         /// <param name="top">二维码:y</param>
137         /// <returns>返回二维码图片的高度</returns>
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         /// <summary>
154         /// 根据链接获取二维码
155         /// 所需参数:URL;距左Left值
156         /// 默认值:距上Top值:顺位值
157         /// </summary>
158         /// <param name="url">链接</param
159         /// <param name="left">二维码:x</param>
160         /// <returns>返回二维码图片的高度</returns>
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         /// <summary>
176         /// 根据链接获取二维码
177         /// 所需参数:URL
178         /// 默认值:距左Left值:25像素;距上Top值:顺位值
179         /// </summary>
180         /// <param name="url">链接</param>
181         /// <returns></returns>
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         /// <summary>
202         /// 打印内容
203         /// 所需参数:打印内容;内容字体;内容字体颜色;距左Left值;距上Top值
204         /// 默认值:无
205         /// </summary>
206         /// <param name="content">需要打印的内容</param>
207         /// <param name="contentFont">内容字体</param>
208         /// <param name="contentBrush">内容字体颜色,提供Brushes.Color格式</param>
209         /// <param name="left">打印区域的左边界</param>
210         /// <param name="top">打印区域的上边界</param>
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         /// <summary>
219         /// 打印内容
220         /// 所需参数:打印内容;内容字体;内容字体颜色;距左Left值
221         /// 默认值:距上Top值:顺位值
222         /// </summary>
223         /// <param name="content">需要打印的内容</param>
224         /// <param name="contentFont">内容字体</param>
225         /// <param name="contentBrush">内容字体颜色,提供Brushes.Color格式</param>
226         /// <param name="left">打印区域的左边界</param>
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         /// <summary>
235         /// 打印内容
236         /// 所需参数:打印内容;内容字体;内容字体颜色
237         /// 默认值:距左Left值:25像素;距上Top值:顺位值
238         /// </summary>
239         /// <param name="content">需要打印的内容</param>
240         /// <param name="contentFont">内容字体</param>
241         /// <param name="contentBrush">内容字体颜色,提供Brushes.Color格式</param>
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         /// <summary>
250         /// 打印内容
251         /// 所需参数:打印内容;内容字体
252         /// 默认值:字体:仿宋8号;内容字体颜色:黑色;距左Left值:25像素;距上Top值:顺位值
253         /// </summary>
254         /// <param name="content">需要打印的内容</param>
255         /// <param name="contentFont">内容字体</param>
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         /// <summary>
264         /// 打印内容
265         /// 所需参数:打印内容
266         /// 默认值:字体:仿宋8号;内容字体颜色:黑色;距左Left值:25像素;距上Top值:顺位值
267         /// </summary>
268         /// <param name="content">需要打印的内容</param>
269         /// <param name="contentFont">内容字体:默认仿宋,8号</param>
270         /// <param name="contentBrush">内容字体颜色:默认黑色</param>
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         /// <summary>
283         /// 打印内容
284         /// 所需参数:打印内容集合;内容字体;内容字体颜色;距左Left值;距上Top值
285         /// 默认值:无
286         /// </summary>
287         /// <param name="content">需要打印的内容</param>
288         /// <param name="contentFont">内容字体</param>
289         /// <param name="contentBrush">内容字体颜色,提供Brushes.Color格式</param>
290         /// <param name="left">打印区域的左边界</param>
291         /// <param name="top">打印区域的上边界</param>
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 + 10;
302         }
303
304         /// <summary>
305         /// 打印内容:
306         /// 所需参数:打印内容集合;内容字体;内容字体颜色
307         /// 默认值:距左Left值:25像素;距上Top值:顺位值
308         /// </summary>
309         /// <param name="content">需要打印的内容</param>
310         /// <param name="contentFont">内容字体</param>
311         /// <param name="contentBrush">内容字体颜色,提供Brushes.Color格式</param>
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 + 10;
322         }
323
324         /// <summary>
325         /// 打印内容
326         /// 所需参数:打印内容集合;内容字体
327         /// 默认值:内容字体颜色:黑色;距左Left值:25像素;距上Top值:顺位值
328         /// </summary>
329         /// <param name="content">需要打印的内容</param>
330         /// <param name="contentFont">内容字体</param>
331         /// <param name="contentBrush">内容字体颜色:默认黑色</param>
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 + 10;
343         }
344
345         /// <summary>
346         /// 打印内容
347         /// 所需参数:打印内容集合
348         /// 默认值:字体:仿宋8号;内容字体颜色:黑色;距左Left值:25像素;距上Top值:顺位值
349         /// </summary>
350         /// <param name="content">需要打印的内容</param>
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 + 10;
362         }
363         #endregion
364         #endregion
365
366         #region 分界线
367         /// <summary>
368         /// 分界线
369         /// 所需参数:起点Left坐标;起点Top坐标;终点Left坐标;终点Top坐标;线颜色;线宽:1像素
370         /// 默认值:线宽:1像素
371         /// </summary>
372         /// <param name="startPointLeft">分界线起点,起点与纸张左侧距离</param>
373         /// <param name="startPointTop">分界线起点,起点与纸张上侧距离</param>
374         /// <param name="endPointLeft">分界线终点,终点与纸张左侧距离</param>
375         /// <param name="endPointTop">分界线终点,终点与纸张上侧距离</param>
376         /// <param name="lineColor">分界线颜色</param>
377         /// <param name="lineWidth">分界线宽度,默认值为:1</param>
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         /// <summary>
386         /// 分界线
387         /// 所需参数:起点Left坐标;起点Top坐标;分界线长度;线颜色;线宽:1像素
388         /// 默认值:线宽:1像素
389         /// </summary>
390         /// <param name="startPointLeft">分界线起点,起点与纸张左侧距离</param>
391         /// <param name="startPointTop">分界线起点,起点与纸张上侧距离</param>
392         /// <param name="lineLength">分界线长度</param>
393         /// <param name="lineColor">分界线颜色</param>
394         /// <param name="lineWidth">分界线宽度,默认值为:1</param>
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         /// <summary>
403         /// 分界线
404         /// 所需参数:分界线长度;线颜色;线宽:1像素
405         /// 默认值:距上Top值:顺位值;线宽:1像素
406         /// </summary>
407         /// <param name="lineLength">分界线长度</param>
408         /// <param name="lineColor">分界线颜色</param>
409         /// <param name="lineWidth">分界线宽度,默认值为:1</param>
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         /// <summary>
418         /// 分界线
419         /// 所需参数:分界线长度;线宽:1像素
420         /// 默认值:距左Left值:25像素;距上Top值:顺位值;线宽:1像素
421         /// </summary>
422         /// <param name="lineLength">分界线长度</param>
423         /// <param name="lineWidth">分界线宽度,默认值为:1</param>
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         /// <summary>
432         /// 分界线
433         /// 所需参数:起点Left坐标;终点Left坐标;线宽:1像素
434         /// 默认值:距上Top值:顺位值;线颜色:绿色;线宽:1像素
435         /// </summary>
436         /// <param name="startPointLeft">开始点</param>
437         /// <param name="endPointLeft">结束点</param>
438         /// <param name="lineWidth">线宽:默认1像素</param>
439         /// <param name="lineColor">分界线颜色:默认绿色</param>
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         /// <summary>
448         /// 分界线
449         /// 所需参数:起点Left坐标;终点Left坐标;线颜色;线宽:1像素
450         /// 默认值:距上Top值:顺位值;线宽:1像素
451         /// </summary>
452         /// <param name="startPointLeft">开始点</param>
453         /// <param name="endPointLeft">结束点</param>
454         /// <param name="lineWidth">线宽:默认1像素</param>
455         /// <param name="lineColor">分界线颜色</param>
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         //    List<string> num = new List<string>();
 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         /// <summary>
 79         /// 全部使用默认值
 80         /// </summary>
 81         /// <param name="sender"></param>
 82         /// <param name="e"></param>
 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<string> num = new List<string>();
 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://www.cnblogs.com/EasonDongH/p/8308676.html

时间: 2024-07-29 09:47:57

C#---生成条形码,二维码,分界线并打印的相关文章

vue生成条形码/二维码/带logo二维码

条形码:https://blog.csdn.net/dakache11/article/details/83749410 //安装 cnpm install @xkeshi/vue-barcode //main.js中引入 import VueBarcode from '@xkeshi/vue-barcode' Vue.component('barcode', VueBarcode) //vue文件中使用 <!-- 条形码 --> <barcode :value="barcod

生成条形码和二维码并实现打印的功能

生成条形码和二维码并实现打印的功能     开篇:平台下编解条形码和二维码的工具. 下载地址:http://pan.baidu.com/s/1kTr3Vuf Step1:使用VS2010新建一个窗体程序项目: Step2:添加三个类:分别是BarCodeClass.cs.DocementBase.cs.imageDocument.cs.(下一步贴出这些类的代码);;;;添加下载回来的引用zxing.dll. >说明: <1>   BarCodeClass.cs主要用来实现条形码和二维码的

个人用户永久免费,可自动升级版Excel插件,使用VSTO开发,Excel催化剂功能第12波-快速生成、读取、导出条形码二维码

根据指定的内容生成对应的条形码或二维码,在如今移动互联网时代,并不是一件什么新鲜事,随便百度一下,都能找到好多的软件或在线网站可以帮我们做到,但细想一下,如果很偶然地只是生成一个两这样的图形,百度一下找个在线网站生成一下下载到本地,再复制粘贴一下,并不是什么多大问题的事情,但如果要批量处理,又如何呢?如果生成的二维码条形码,先进行排版一下打印出来,类似一个个标签或用作相应的产品说明的一部分,那又是怎样一种现成的解决方案呢?本次Excel催化剂再次刷新大家对Excel的认识,所有大家想做的事情,全

条形码/二维码之开源利器ZXing图文介绍(转)

继前面介绍的一个日本开源软件(该软件只能实现QRCode)原文: Java实现二维码QRCode的编码和解码(http://sjsky.iteye.com/blog/1136934 ),今发现又一优秀的开源利器-- ZXing,相比而言它更加灵活方便,可以实现多种编码格式. 全文目录: 基本介绍 二维码(比如:QRCode)的编码和解码演示 条形码(比如:EAN-13)的编码和解码演示 [一]. 基本介绍 : 1-1. ZXing是一个开源Java类库用于解析多种格式的条形码和二维码. 官网:h

Android应用--QR的生成(二维码)

二维码的定义: 二维码(2-dimensional bar code),是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的. 在许多种类的二维条码中,常用的码制有:Data Matrix, Maxi Code, Aztec, QR Code, Vericode, PDF417, Ultracode, Code 49, Code 16K等.1.堆叠式/行排式二维条码,如,Code 16K.Code 49.PDF417(如下图)等. 2.矩阵式二维码,最流行莫

Java使用ZXing生成/解析二维码图片

ZXing是一种开源的多格式1D/2D条形码图像处理库,在Java中的实现.重点是在手机上使用内置摄像头来扫描和解码设备上的条码,而不与服务器通信.然而,该项目也可以用于对桌面和服务器上的条形码进行编码和解码.目前支持这些格式: UPC-A and UPC-E EAN-8 and EAN-13 Code 39 Code 93 Code 128 ITF Codabar RSS-14 (all variants) RSS Expanded (most variants) QR Code Data M

Force.com微信开发系列(八)生成带二维码的参数

为了满足用户渠道推广分析的需要,公众平台提供了生成带二维码的接口.使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送.目前有两种类型的二维码,分别是临时二维码和永久二维码,前者有过期时间,最大为1800秒,但能够生成较多数量,后者无过期时间,数量较少(目前参数只支持1到100000).两种二维码分别适用于账号绑定.用户来源统计等场景. 用户扫描带场景值二维码时,可能推送以下两种事件: 1. 如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件

iOS 花式二维码生成和二维码识别

iOS 原生的二维码识别非常之棒,反正比 ZXing 和 ZBar 效果都好些,所以以后打算尽量用原生的二维码识别,然后最近把原生的二维码生成也顺便做了一遍,并且在原有基础上加了一些样式参数,封了一个小库方便以后使用. 项目地址:https://github.com/EyreFree/EFQRCode EFQRCode 是一个用 Swift 编写的用来生成和识别二维码的库,它基于系统二维码生成与识别进行开发. 生成:利用输入的水印图/图标等资源生成各种艺术二维码: 识别:识别率比 iOS 原生二

公司开发的APP,如何生成一个二维码,供客户下载使用

1.其实和简单,因为一般的用户使用扫一扫,大多数都是用微信自带的扫一扫工具 而,微信打开的二维码页面,会自动屏蔽apk文件,所以显然把apk的url生成一个二维码,让用户扫一扫就能直接下载,这样是行不通的. 2.正确的做法应该是这样的, (1).先做一个下载页面,放在你的服务器上,IOS和android链接都放上去,显得比较专业,如果ios没有做好,就不要放 如下所示: 2.把下载页面的url地址,通过“草料二维码”生成一个二维码 3.如果是在微信扫一扫打开的,当用户点击"Android下载&q

jquery.qrcode二维码插件生成彩色二维码

jquery.qrcode.js 是居于jquery类库的绘制二维码的插件,用它来实现二维码图形渲染支持canvas和table两种绘图方式. (jquery.qrcode.js 设置显示方式为table时在webkit核心浏览器如chrome下会变形) 以下是测试代码(增加了颜色控制,可以设置4个区块的颜色值,需要指定render为table.),效果: jquery.qrcode生成彩色二维码" src="http://www.jbxue.com/d/file/2014/08/20