本文介紹在 C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和邊框寬度的二維碼。網上文章大多隻是簡單介紹內置參數的設置,根據個人使用目的,增長了自定義目標二維碼圖片尺寸和白邊邊框。有須要的朋友們能夠試一下,若有bug歡迎指正。
首先,將 ThoughtWorks.QRCode.dll 放在 bin 目錄後,在頁面中引用:
using ThoughtWorks.QRCode.Codec;
生成二維碼圖片:
編碼
/// <summary> /// 生成二維碼 /// </summary> /// <param name="Content">內容文本</param> /// <param name="QRCodeEncodeMode">二維碼編碼方式</param> /// <param name="QRCodeErrorCorrect">糾錯碼等級</param> /// <param name="QRCodeVersion">二維碼版本號 0-40</param> /// <param name="QRCodeScale">每一個小方格的預設寬度(像素),正整數</param> /// <param name="size">圖片尺寸(像素),0表示不設置</param> /// <param name="border">圖片白邊(像素),當size大於0時有效</param> /// <returns></returns> public System.Drawing.Image CreateQRCode(string Content, QRCodeEncoder.ENCODE_MODE QRCodeEncodeMode, QRCodeEncoder.ERROR_CORRECTION QRCodeErrorCorrect, int QRCodeVersion, int QRCodeScale, int size, int border) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncodeMode; qrCodeEncoder.QRCodeErrorCorrect = QRCodeErrorCorrect; qrCodeEncoder.QRCodeScale = QRCodeScale; qrCodeEncoder.QRCodeVersion = QRCodeVersion; System.Drawing.Image image = qrCodeEncoder.Encode(Content); #region 根據設定的目標圖片尺寸調整二維碼QRCodeScale設置,並添加邊框 if (size > 0) { //當設定目標圖片尺寸大於生成的尺寸時,逐步增大方格尺寸 #region 當設定目標圖片尺寸大於生成的尺寸時,逐步增大方格尺寸 while (image.Width < size) { qrCodeEncoder.QRCodeScale++; System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content); if (imageNew.Width < size) { image = new System.Drawing.Bitmap(imageNew); imageNew.Dispose(); imageNew = null; } else { qrCodeEncoder.QRCodeScale--; //新尺寸未採用,恢復最終使用的尺寸 imageNew.Dispose(); imageNew = null; break; } } #endregion //當設定目標圖片尺寸小於生成的尺寸時,逐步減少方格尺寸 #region 當設定目標圖片尺寸小於生成的尺寸時,逐步減少方格尺寸 while (image.Width > size && qrCodeEncoder.QRCodeScale > 1) { qrCodeEncoder.QRCodeScale--; System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content); image = new System.Drawing.Bitmap(imageNew); imageNew.Dispose(); imageNew = null; if (image.Width < size) { break; } } #endregion //若是目標尺寸大於生成的圖片尺寸,則爲圖片增長白邊 #region 若是目標尺寸大於生成的圖片尺寸,則爲圖片增長白邊 if (image.Width <= size) { //根據參數設置二維碼圖片白邊的最小寬度 #region 根據參數設置二維碼圖片白邊的最小寬度 if (border > 0) { while (image.Width <= size && size - image.Width < border * 2 && qrCodeEncoder.QRCodeScale > 1) { qrCodeEncoder.QRCodeScale--; System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content); image = new System.Drawing.Bitmap(imageNew); imageNew.Dispose(); imageNew = null; } } #endregion //當目標圖片尺寸大於二維碼尺寸時,將二維碼繪製在目標尺寸白色畫布的中心位置 if (image.Width < size) { //新建空白繪圖 System.Drawing.Bitmap panel = new System.Drawing.Bitmap(size, size); System.Drawing.Graphics graphic0 = System.Drawing.Graphics.FromImage(panel); int p_left = 0; int p_top = 0; if (image.Width <= size) //若是原圖比目標形狀寬 { p_left = (size - image.Width) / 2; } if (image.Height <= size) { p_top = (size - image.Height) / 2; } //將生成的二維碼圖像粘貼至繪圖的中心位置 graphic0.DrawImage(image, p_left, p_top, image.Width, image.Height); image = new System.Drawing.Bitmap(panel); panel.Dispose(); panel = null; graphic0.Dispose(); graphic0 = null; } } #endregion } #endregion return image; } private static bool IsTrue() // 在Image類別對圖片進行縮放的時候,須要一個返回bool類別的委託 { return true; }
在aspx頁面調用和輸出爲圖片流:spa
string content = string.IsNullOrEmpty(Request.QueryString["txt"]) ? "http://www.lenashane.com/" : Request.QueryString["txt"]; int size = Convert.ToInt32(string.IsNullOrEmpty(Request.QueryString["size"]) ? "200" : Request.QueryString["size"]); int border = Convert.ToInt32(string.IsNullOrEmpty(Request.QueryString["border"]) ? "10" : Request.QueryString["border"]); System.Drawing.Image image = CreateQRCode(content, QRCodeEncoder.ENCODE_MODE.BYTE, QRCodeEncoder.ERROR_CORRECTION.M, 0, 5, size, border); //將圖片輸出到頁面 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ContentType = "image/Png"; HttpContext.Current.Response.OutputStream.Write(ms.ToArray(), 0, ms.ToArray().Length); HttpContext.Current.Response.End(); ms.Close(); ms = null; image.Dispose(); image = null;
更新:
生成二維碼方法,增長定位點着色參數(System.Drawing.Color),效果以下:
若有效率更高的繪製方法還望指教,下面是代碼code
/// <summary> /// 生成二維碼 /// </summary> /// <param name="Content">內容文本</param> /// <param name="QRCodeEncodeMode">二維碼編碼方式</param> /// <param name="QRCodeErrorCorrect">糾錯碼等級</param> /// <param name="QRCodeVersion">二維碼版本號 0-40</param> /// <param name="QRCodeScale">每一個小方格的預設寬度(像素),正整數</param> /// <param name="size">圖片尺寸(像素),0表示不設置</param> /// <param name="border">圖片白邊(像素),當size大於0時有效</param> /// <returns></returns> public System.Drawing.Image CreateQRCode(string Content, QRCodeEncoder.ENCODE_MODE QRCodeEncodeMode, QRCodeEncoder.ERROR_CORRECTION QRCodeErrorCorrect, int QRCodeVersion, int QRCodeScale, int size, int border, Color codeEyeColor) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncodeMode; qrCodeEncoder.QRCodeErrorCorrect = QRCodeErrorCorrect; qrCodeEncoder.QRCodeScale = QRCodeScale; qrCodeEncoder.QRCodeVersion = QRCodeVersion; System.Drawing.Image image = qrCodeEncoder.Encode(Content); #region 根據設定的目標圖片尺寸調整二維碼QRCodeScale設置,並添加邊框 if (size > 0) { //當設定目標圖片尺寸大於生成的尺寸時,逐步增大方格尺寸 #region 當設定目標圖片尺寸大於生成的尺寸時,逐步增大方格尺寸 while (image.Width < size) { qrCodeEncoder.QRCodeScale++; System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content); if (imageNew.Width < size) { image = new System.Drawing.Bitmap(imageNew); imageNew.Dispose(); imageNew = null; } else { qrCodeEncoder.QRCodeScale--; //新尺寸未採用,恢復最終使用的尺寸 imageNew.Dispose(); imageNew = null; break; } } #endregion //當設定目標圖片尺寸小於生成的尺寸時,逐步減少方格尺寸 #region 當設定目標圖片尺寸小於生成的尺寸時,逐步減少方格尺寸 while (image.Width > size && qrCodeEncoder.QRCodeScale > 1) { qrCodeEncoder.QRCodeScale--; System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content); image = new System.Drawing.Bitmap(imageNew); imageNew.Dispose(); imageNew = null; if (image.Width < size) { break; } } #endregion //根據參數設置二維碼圖片白邊的最小寬度(按需縮小) #region 根據參數設置二維碼圖片白邊的最小寬度 if (image.Width <= size && border > 0) { while (image.Width <= size && size - image.Width < border * 2 && qrCodeEncoder.QRCodeScale > 1) { qrCodeEncoder.QRCodeScale--; System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content); image = new System.Drawing.Bitmap(imageNew); imageNew.Dispose(); imageNew = null; } } #endregion //已經確認二維碼圖像,爲圖像染色修飾 if (true) { //定位點方塊邊長 int beSize = qrCodeEncoder.QRCodeScale * 3; int bep1_l = qrCodeEncoder.QRCodeScale * 2; int bep1_t = qrCodeEncoder.QRCodeScale * 2; int bep2_l = image.Width - qrCodeEncoder.QRCodeScale * 5 - 1; int bep2_t = qrCodeEncoder.QRCodeScale * 2; int bep3_l = qrCodeEncoder.QRCodeScale * 2; int bep3_t = image.Height - qrCodeEncoder.QRCodeScale * 5 - 1; int bep4_l = image.Width - qrCodeEncoder.QRCodeScale * 7 - 1; int bep4_t = image.Height - qrCodeEncoder.QRCodeScale * 7 - 1; System.Drawing.Graphics graphic0 = System.Drawing.Graphics.FromImage(image); // Create solid brush. SolidBrush blueBrush = new SolidBrush(codeEyeColor); // Fill rectangle to screen. graphic0.FillRectangle(blueBrush, bep1_l, bep1_t, beSize, beSize); graphic0.FillRectangle(blueBrush, bep2_l, bep2_t, beSize, beSize); graphic0.FillRectangle(blueBrush, bep3_l, bep3_t, beSize, beSize); graphic0.FillRectangle(blueBrush, bep4_l, bep4_t, qrCodeEncoder.QRCodeScale, qrCodeEncoder.QRCodeScale); } //當目標圖片尺寸大於二維碼尺寸時,將二維碼繪製在目標尺寸白色畫布的中心位置 #region 若是目標尺寸大於生成的圖片尺寸,將二維碼繪製在目標尺寸白色畫布的中心位置 if (image.Width < size) { //新建空白繪圖 System.Drawing.Bitmap panel = new System.Drawing.Bitmap(size, size); System.Drawing.Graphics graphic0 = System.Drawing.Graphics.FromImage(panel); int p_left = 0; int p_top = 0; if (image.Width <= size) //若是原圖比目標形狀寬 { p_left = (size - image.Width) / 2; } if (image.Height <= size) { p_top = (size - image.Height) / 2; } //將生成的二維碼圖像粘貼至繪圖的中心位置 graphic0.DrawImage(image, p_left, p_top, image.Width, image.Height); image = new System.Drawing.Bitmap(panel); panel.Dispose(); panel = null; graphic0.Dispose(); graphic0 = null; } #endregion } #endregion return image; }