C#中使用ThoughtWorks.QRCode.dll生成指定尺寸和邊框?qū)挾鹊亩S碼標(biāo)簽
|
admin
2022年7月28日 23:43
本文熱度 943
|
本文介紹在 C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和邊框?qū)挾鹊亩S碼。網(wǎng)上文章大多只是簡單介紹內(nèi)置參數(shù)的設(shè)置,根據(jù)我的使用目的,增加了自定義目標(biāo)二維碼圖片尺寸和白邊邊框。有需要的朋友們可以試一下,如有bug歡迎指正。首先,將 ThoughtWorks.QRCode.dll 放在 bin 目錄后,在頁面中引用:using ThoughtWorks.QRCode.Codec;生成二維碼圖片:
/// <summary>
/// 生成二維碼
/// </summary>
/// <param name="Content">內(nèi)容文本</param>
/// <param name="QRCodeEncodeMode">二維碼編碼方式(注意:BYTE能支持中文,ALPHA_NUMERIC掃描出來的都是數(shù)字)</param>
/// <param name="QRCodeErrorCorrect">糾錯(cuò)碼等級,錯(cuò)誤效驗(yàn)、錯(cuò)誤更正(有4個(gè)等級)</param>
/// <param name="QRCodeVersion">二維碼版本號 0-40(注意:設(shè)置為0主要是防止編碼的字符串太長時(shí)發(fā)生錯(cuò)誤)</param>
/// <param name="QRCodeScale">每個(gè)小方格的預(yù)設(shè)寬度(像素),正整數(shù)(值越大生成的二維碼圖片像素越高)</param>
/// <param name="QRCodeBackgroundColor">Color.Yellow;//背景色</param> /// <param name="QRCodeForegroundColor">Color.Green;//前景色</param> /// <param name="size">圖片尺寸(像素),0表示不設(shè)置</param> /// <param name="border">圖片白邊(像素),當(dāng)size大于0時(shí)有效</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); System.Drawing.Image image = qrCodeEncoder.Encode(Content, Encoding.UTF8);
#region 根據(jù)設(shè)定的目標(biāo)圖片尺寸調(diào)整二維碼QRCodeScale設(shè)置,并添加邊框
if (size > 0)
{
//當(dāng)設(shè)定目標(biāo)圖片尺寸大于生成的尺寸時(shí),逐步增大方格尺寸
#region 當(dāng)設(shè)定目標(biāo)圖片尺寸大于生成的尺寸時(shí),逐步增大方格尺寸
while (image.Width < size)
{
qrCodeEncoder.QRCodeScale++;
System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content, Encoding.UTF8);
if (imageNew.Width < size)
{
image = new System.Drawing.Bitmap(imageNew);
imageNew.Dispose();
imageNew = null;
}
else
{
qrCodeEncoder.QRCodeScale--; //新尺寸未采用,恢復(fù)最終使用的尺寸
imageNew.Dispose();
imageNew = null;
break;
}
}
#endregion
//當(dāng)設(shè)定目標(biāo)圖片尺寸小于生成的尺寸時(shí),逐步減小方格尺寸
#region 當(dāng)設(shè)定目標(biāo)圖片尺寸小于生成的尺寸時(shí),逐步減小方格尺寸
while (image.Width > size && qrCodeEncoder.QRCodeScale > 1)
{
qrCodeEncoder.QRCodeScale--;
System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content, Encoding.UTF8);
image = new System.Drawing.Bitmap(imageNew);
imageNew.Dispose();
imageNew = null;
if (image.Width < size)
{
break;
}
}
#endregion
//如果目標(biāo)尺寸大于生成的圖片尺寸,則為圖片增加白邊
#region 如果目標(biāo)尺寸大于生成的圖片尺寸,則為圖片增加白邊
if (image.Width <= size)
{
//根據(jù)參數(shù)設(shè)置二維碼圖片白邊的最小寬度
#region 根據(jù)參數(shù)設(shè)置二維碼圖片白邊的最小寬度
if (border > 0)
{
while (image.Width <= size && size - image.Width < border * 2 && qrCodeEncoder.QRCodeScale > 1)
{
qrCodeEncoder.QRCodeScale--;
System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content, Encoding.UTF8);
image = new System.Drawing.Bitmap(imageNew);
imageNew.Dispose();
imageNew = null;
}
}
#endregion
//當(dāng)目標(biāo)圖片尺寸大于二維碼尺寸時(shí),將二維碼繪制在目標(biāo)尺寸白色畫布的中心位置
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) //如果原圖比目標(biāo)形狀寬
{
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類別對圖片進(jìn)行縮放的時(shí)候,需要一個(gè)返回bool類別的委托
{
return true;
}
在aspx頁面調(diào)用和輸出為圖片流:
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; 更新:
生成二維碼方法,增加定位點(diǎn)著色參數(shù)(System.Drawing.Color),效果如下:
如有效率更高的繪制方法還望指教,下面是代碼
/// <summary>
/// 生成二維碼
/// </summary>
/// <param name="Content">內(nèi)容文本</param>
/// <param name="QRCodeEncodeMode">二維碼編碼方式(注意:BYTE能支持中文,ALPHA_NUMERIC掃描出來的都是數(shù)字)</param>
/// <param name="QRCodeErrorCorrect">糾錯(cuò)碼等級,錯(cuò)誤效驗(yàn)、錯(cuò)誤更正(有4個(gè)等級)</param>
/// <param name="QRCodeVersion">二維碼版本號 0-40(注意:設(shè)置為0主要是防止編碼的字符串太長時(shí)發(fā)生錯(cuò)誤)</param>
/// <param name="QRCodeScale">每個(gè)小方格的預(yù)設(shè)寬度(像素),正整數(shù)(值越大生成的二維碼圖片像素越高)</param>
/// <param name="size">圖片尺寸(像素),0表示不設(shè)置</param>
/// <param name="border">圖片白邊(像素),當(dāng)size大于0時(shí)有效</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, Encoding.UTF8);
#region 根據(jù)設(shè)定的目標(biāo)圖片尺寸調(diào)整二維碼QRCodeScale設(shè)置,并添加邊框
if (size > 0)
{
//當(dāng)設(shè)定目標(biāo)圖片尺寸大于生成的尺寸時(shí),逐步增大方格尺寸
#region 當(dāng)設(shè)定目標(biāo)圖片尺寸大于生成的尺寸時(shí),逐步增大方格尺寸
while (image.Width < size)
{
qrCodeEncoder.QRCodeScale++;
System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content, Encoding.UTF8);
if (imageNew.Width < size)
{
image = new System.Drawing.Bitmap(imageNew);
imageNew.Dispose();
imageNew = null;
}
else
{
qrCodeEncoder.QRCodeScale--; //新尺寸未采用,恢復(fù)最終使用的尺寸
imageNew.Dispose();
imageNew = null;
break;
}
}
#endregion
//當(dāng)設(shè)定目標(biāo)圖片尺寸小于生成的尺寸時(shí),逐步減小方格尺寸
#region 當(dāng)設(shè)定目標(biāo)圖片尺寸小于生成的尺寸時(shí),逐步減小方格尺寸
while (image.Width > size && qrCodeEncoder.QRCodeScale > 1)
{
qrCodeEncoder.QRCodeScale--;
System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content, Encoding.UTF8);
image = new System.Drawing.Bitmap(imageNew);
imageNew.Dispose();
imageNew = null;
if (image.Width < size)
{
break;
}
}
#endregion
//根據(jù)參數(shù)設(shè)置二維碼圖片白邊的最小寬度(按需縮?。?/span>
#region 根據(jù)參數(shù)設(shè)置二維碼圖片白邊的最小寬度
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, Encoding.UTF8);
image = new System.Drawing.Bitmap(imageNew);
imageNew.Dispose();
imageNew = null;
}
}
#endregion
//已經(jīng)確認(rèn)二維碼圖像,為圖像染色修飾
if (true)
{
//定位點(diǎn)方塊邊長
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);
}
//當(dāng)目標(biāo)圖片尺寸大于二維碼尺寸時(shí),將二維碼繪制在目標(biāo)尺寸白色畫布的中心位置
#region 如果目標(biāo)尺寸大于生成的圖片尺寸,將二維碼繪制在目標(biāo)尺寸白色畫布的中心位置
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) //如果原圖比目標(biāo)形狀寬
{
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;
}
該文章在 2022/7/28 23:43:03 編輯過
| |
全部評論2 |
|
admin
2022年7月28日 23:51
///
/// 選擇生成二維碼的相關(guān)類型
/// 要生成的文字或者數(shù)字,支持中文。如: "4408810820 深圳-廣州" 或者:4444444444
/// 三種尺寸:BYTE ,ALPHA_NUMERIC,NUMERIC
/// 大?。篖 M Q H
/// 版本:如 8
/// 比例:如 4
///
///
public void createCode_Choose(string strData, string qrEncoding, string level, int version, int scale)
{
if (string.IsNullOrEmpty(strData))
{
throw new ArgumentNullException(strData);
}
if (string.IsNullOrEmpty(qrEncoding))
{
throw new ArgumentNullException(qrEncoding);
}
if (string.IsNullOrEmpty(level))
{
throw new ArgumentNullException(level);
}
var qrCodeEncoder = new QRCodeEncoder();
var encoding = qrEncoding;
switch (encoding)
{
case "Byte":
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
break;
case "AlphaNumeric":
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
break;
case "Numeric":
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
break;
default:
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
break;
}
qrCodeEncoder.QRCodeScale = scale;
qrCodeEncoder.QRCodeVersion = version;
switch (level)
{
case "L":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
break;
case "M":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
break;
case "Q":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
break;
default:
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
break;
}
Image image = null;
FileStream fs = null;
try
{
//文字生成圖片
image = qrCodeEncoder.Encode(strData);
var filename = DateTime.Now.ToString("yyyymmddhhmmssfff") + ".jpg";
var filepath = HttpContext.Current.Server.MapPath(@"~\Upload") + "\\" + filename;
fs = new FileStream(filepath, FileMode.OpenOrcreate, FileAccess.Write);
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (IOException ioex)
{
throw new IOException(ioex.Message);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (fs != null) fs.Close();
if (image != null) image.Dispose();
}
}
|
|
admin
2022年7月28日 23:53
詳解免費(fèi)開源的.Net二維碼操作組件ThoughtWorks.QRCode.dll(.NET組件介紹)
http://18318.oa22.cn 該評論在 2022/7/28 23:53:25 編輯過
|
|
|