C#基于QRCode實現動態(tài)生成自定義二維碼圖片功能示例
本文實例講述了C#基于QRCode實現動態(tài)生成自定義二維碼圖片功能。分享給大家供大家參考,具體如下:
二維碼早就傳遍大江南北了,總以為它是個神奇的東西,其實細細研究之后發(fā)現也沒想象的那么神秘,碰巧最近項目中需要動態(tài)生成二維碼,解決完實際問題之后,簡單總結整理一下。項目中除了動態(tài)生成二維碼之外,還實現了動態(tài)生成自定義圖片,二維碼可以是其中的元素。
設置圖片的數據源為動態(tài)圖片
<body> <form id="form1" runat="server" > <div> <img src="GenerateImage.aspx?type=2" /> </div> </form> </body>
動態(tài)生成圖片
GenerateImage.aspx.cs文件內容
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(); //設置編碼模式 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; //設置編碼測量度 qrCodeEncoder.QRCodeScale = size; //設置編碼版本 qrCodeEncoder.QRCodeVersion = 10; //設置編碼錯誤糾正 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>自定義圖片內存流</returns> private MemoryStream Create_ImgCode(Bitmap codeImage, string objectName, string objectCode, string type) { string path = string.Empty; if (type == "1") { //設置背景圖片 path = Server.MapPath("Images/backimg1.png"); } else if (type == "2") { //設置背景圖片 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也是畫到畫布上的,當畫的string長度大于112px時會自動換行 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("幸運數字:", 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("幸運數字:", 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點擊此處本站下載。
PS:本站還提供了一個功能十分強悍的在線二維碼生成工具,可實現文本、電話號碼、短信、郵件、網址等的二維碼生成及l(fā)ogo圖標添加功能:
在線生成二維碼工具(加強版):
http://tools.jb51.net/transcoding/jb51qrcode
更多關于C#相關內容感興趣的讀者可查看本站專題:《C#圖片操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數據結構與算法教程》、《C#面向對象程序設計入門教程》及《C#程序設計之線程使用技巧總結》
希望本文所述對大家C#程序設計有所幫助。
相關文章
C#實體對象序列化成Json并讓字段的首字母小寫的兩種解決方法
這篇文章主要介紹了C#實體對象序列化成Json并讓字段的首字母小寫的兩種方法,在這兩種方法中小編比較推薦使用第二種方法,需要的朋友可以參考下2018-06-06