C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能示例
本文實(shí)例講述了C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能。分享給大家供大家參考,具體如下:
二維碼早就傳遍大江南北了,總以為它是個(gè)神奇的東西,其實(shí)細(xì)細(xì)研究之后發(fā)現(xiàn)也沒想象的那么神秘,碰巧最近項(xiàng)目中需要?jiǎng)討B(tài)生成二維碼,解決完實(shí)際問題之后,簡(jiǎn)單總結(jié)整理一下。項(xiàng)目中除了動(dòng)態(tài)生成二維碼之外,還實(shí)現(xiàn)了動(dòng)態(tài)生成自定義圖片,二維碼可以是其中的元素。
設(shè)置圖片的數(shù)據(jù)源為動(dòng)態(tài)圖片
<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)容
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); //為畫布bg(圖片bg)創(chuàng)建一只畫筆 Graphics g = Graphics.FromImage(bg); if (type == "1") { //【1】將位圖文件codeImage畫到畫布g上 //【2】codeImage左上角距畫布左邊界25px、距畫布上邊界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也是畫到畫布上的,當(dāng)畫的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); //將畫布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)擊此處本站下載。
PS:本站還提供了一個(gè)功能十分強(qiáng)悍的在線二維碼生成工具,可實(shí)現(xiàn)文本、電話號(hào)碼、短信、郵件、網(wǎng)址等的二維碼生成及l(fā)ogo圖標(biāo)添加功能:
在線生成二維碼工具(加強(qiáng)版):
http://tools.jb51.net/transcoding/jb51qrcode
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#圖片操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#中38個(gè)常用運(yùn)算符的優(yōu)先級(jí)的劃分和理解
這只我自己在學(xué)C#中的一些總結(jié),其中對(duì)于各級(jí)的劃分方式、各操作符的優(yōu)先級(jí)的理解并不見得正確,只是自己的看法,拿出來與大家分享2012-08-08C#事件處理和委托event delegate實(shí)例簡(jiǎn)述
這篇文章主要介紹了C#事件處理和委托event delegate的簡(jiǎn)單實(shí)例,較為詳細(xì)的講述了C#事件處理和委托的聲明與實(shí)現(xiàn)過程,代碼簡(jiǎn)單易懂,需要的朋友可以參考下2014-09-09C#實(shí)現(xiàn)獲取系統(tǒng)目錄并以Tree樹叉顯示的方法
這篇文章主要介紹了C#實(shí)現(xiàn)獲取系統(tǒng)目錄并以Tree樹叉顯示的方法,可以加深讀者對(duì)于C#下數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)方法的認(rèn)識(shí),需要的朋友可以參考下2014-07-07基于C#實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的HTTP服務(wù)器實(shí)例
這篇文章主要介紹了基于C#實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的HTTP服務(wù)器的方法,詳細(xì)分析了http服務(wù)器的實(shí)現(xiàn)原理與相關(guān)技巧,以及對(duì)應(yīng)的注意事項(xiàng),需要的朋友可以參考下2014-12-12.NET/C# 使用Stopwatch測(cè)量運(yùn)行時(shí)間
這篇文章主要介紹了.NET/C# 使用Stopwatch測(cè)量運(yùn)行時(shí)間,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01C#實(shí)體對(duì)象序列化成Json并讓字段的首字母小寫的兩種解決方法
這篇文章主要介紹了C#實(shí)體對(duì)象序列化成Json并讓字段的首字母小寫的兩種方法,在這兩種方法中小編比較推薦使用第二種方法,需要的朋友可以參考下2018-06-06