码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2018-01-18 10:15:25      阅读:378      评论:0      收藏:0      [点我收藏+]

标签:alt   odex   数据   sms   oat   区域   调整   convert   next   

分享一个可以生成条形码,二维码的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 }
View Code

测试:

技术分享图片
  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 }
View Code

测试结果:

技术分享图片

提供了若干方法的重载,可在我github下载完整代码:

https://github.com/EasonDongH/Printer.git

也可以直接下载项目dll使用:

 

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

标签:alt   odex   数据   sms   oat   区域   调整   convert   next   

原文地址:https://www.cnblogs.com/EasonDongH/p/8308676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!