您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在C#中使用QRCode動態生成自定義二維碼圖片,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
設置圖片的數據源為動態圖片
<body> <form id="form1" runat="server" > <div> <img src="GenerateImage.aspx?type=2" /> </div> </form> </body>
動態生成圖片
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) { //創建二維碼生成類 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)創建一只畫筆 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; }
上述就是小編為大家分享的怎么在C#中使用QRCode動態生成自定義二維碼圖片了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。