本文實(shí)例講述了C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能。分享給大家供大家參考,具體如下:
二維碼早就傳遍大江南北了,總以為它是個(gè)神奇的東西,其實(shí)細(xì)細(xì)研究之后發(fā)現(xiàn)也沒(méi)想象的那么神秘,碰巧最近項(xiàng)目中需要?jiǎng)討B(tài)生成二維碼,解決完實(shí)際問(wèn)題之后,簡(jiǎn)單總結(jié)整理一下。項(xiàng)目中除了動(dòng)態(tài)生成二維碼之外,還實(shí)現(xiàn)了動(dòng)態(tài)生成自定義圖片,二維碼可以是其中的元素。
設(shè)置圖片的數(shù)據(jù)源為動(dòng)態(tài)圖片
1
2
3
4
5
6
7
|
< body > < form id = "form1" runat = "server" > < div > < img src = "GenerateImage.aspx?type=2" /> </ div > </ form > </ body > |
動(dòng)態(tài)生成圖片
GenerateImage.aspx.cs文件內(nèi)容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
protected void Page_Load( object sender, EventArgs e) { string type = Request.QueryString[ "type" ].ToString(); Bitmap codeImage = Create_QRCode( "分享才能獲得更多,我盡力而為(5201314)" , 6); MemoryStream ms = Create_ImgCode(codeImage, "分享才能獲得更多,我盡力而為" , "5201314" , type); Response.ClearContent(); Response.ContentType = "image/Png" ; Response.BinaryWrite(ms.ToArray()); Response.End(); } private Bitmap Create_QRCode( string codeNumber, int size) { //創(chuàng)建二維碼生成類 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); //設(shè)置編碼模式 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; //設(shè)置編碼測(cè)量度 qrCodeEncoder.QRCodeScale = size; //設(shè)置編碼版本 qrCodeEncoder.QRCodeVersion = 10; //設(shè)置編碼錯(cuò)誤糾正 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; //生成二維碼圖片 System.Drawing.Bitmap codeImage = qrCodeEncoder.Encode(codeNumber, Encoding.UTF8); return codeImage; } /// <summary> /// 生成自定義圖片 /// </summary> /// <param name="codeImage">生成的二維碼</param> /// <param name="objectName">物體名稱</param> /// <returns>自定義圖片內(nèi)存流</returns> private MemoryStream Create_ImgCode(Bitmap codeImage, string objectName, string objectCode, string type) { string path = string .Empty; if (type == "1" ) { //設(shè)置背景圖片 path = Server.MapPath( "Images/backimg1.png" ); } else if (type == "2" ) { //設(shè)置背景圖片 path = Server.MapPath( "Images/backimg2.png" ); } System.Drawing.Image img = System.Drawing.Image.FromFile(path); Bitmap bg = new Bitmap(img); //為畫(huà)布bg(圖片bg)創(chuàng)建一只畫(huà)筆 Graphics g = Graphics.FromImage(bg); if (type == "1" ) { //【1】將位圖文件codeImage畫(huà)到畫(huà)布g上 //【2】codeImage左上角距畫(huà)布左邊界25px、距畫(huà)布上邊界56px //【3】codeImage的長(zhǎng)為原長(zhǎng)、寬為原寬 g.DrawImage(codeImage, 25, 56, codeImage.Width, codeImage.Height); } else if (type == "2" ) { g.DrawImage(codeImage, 132, 19, 162, 162); System.Drawing.Brush b = new SolidBrush(Color.Black); Font font = new Font( "宋體" , 8, FontStyle.Regular); StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; // 垂直居中 sf.Alignment = StringAlignment.Near; // 水平左對(duì)齊 //string也是畫(huà)到畫(huà)布上的,當(dāng)畫(huà)的string長(zhǎng)度大于112px時(shí)會(huì)自動(dòng)換行 SizeF stringSize = g.MeasureString( "我的宣言:" , font, 112, sf); int nWidth = ( int )stringSize.Width + 1; int nHeight = ( int )stringSize.Height + 1; RectangleF rf = new Rectangle( new Point(12, 64), new Size(nWidth, nHeight)); g.DrawString( "我的宣言:" , font, b, rf, sf); stringSize = g.MeasureString(objectName, font, 112, sf); int objectWidth = ( int )stringSize.Width + 1; int objectHeight = ( int )stringSize.Height + 1; rf = new Rectangle( new Point(12, 64 + nHeight + 8), new Size(objectWidth, objectHeight)); g.DrawString(objectName, font, b, rf, sf); SizeF stringSize1 = g.MeasureString( "幸運(yùn)數(shù)字:" , font, 112, sf); nWidth = ( int )stringSize1.Width + 1; nHeight = ( int )stringSize1.Height + 1; RectangleF rf1 = new Rectangle( new Point(12, 136), new Size(nWidth, nHeight)); g.DrawString( "幸運(yùn)數(shù)字:" , font, b, rf1, sf); stringSize1 = g.MeasureString(objectCode, font, 112, sf); objectWidth = ( int )stringSize1.Width + 1; objectHeight = ( int )stringSize1.Height + 1; rf1 = new Rectangle( new Point(12, 136 + nHeight + 8), new Size(objectWidth, objectHeight)); g.DrawString(objectCode, font, b, rf1, sf); } g.Dispose(); GC.Collect(); System.IO.MemoryStream ms = new System.IO.MemoryStream(); bg.Save(ms, System.Drawing.Imaging.ImageFormat.Png); //將畫(huà)布bg(圖片bg)保存到指定路徑 path = Server.MapPath( "Images" ); bg.Save(path + "\\photoName.png" , System.Drawing.Imaging.ImageFormat.Png); codeImage.Dispose(); bg.Dispose(); return ms; } |
ThoughtWorks.QRCode.dll點(diǎn)擊此處本站下載。
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
原文鏈接:https://blog.csdn.net/xiaouncle/article/details/52588947