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é)整理一下。項(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è)置編碼測量度 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的長為原長、寬為原寬 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; // 水平左對齊 //string也是畫到畫布上的,當(dāng)畫的string長度大于112px時(shí)會自動(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)文本、電話號碼、短信、郵件、網(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é)》
希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
上一篇:Unity3D實(shí)現(xiàn)播放gif圖功能
欄 目:C#教程
本文標(biāo)題:C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能示例
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4900.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)