C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和邊框寬度的二維碼

本文介紹在 C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和邊框寬度的二維碼。網上文章大多只是簡單介紹內置參數的設置,根據我的使用目的,增加了自定義目標二維碼圖片尺寸和白邊邊框。有需要的朋友們可以試一下,如有bug歡迎指正。

?首先,將 ThoughtWorks.QRCode.dll 放在 bin 目錄后,在頁面中引用:

using?ThoughtWorks.QRCode.Codec;
生成二維碼圖片:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/// <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頁面調用和輸出為圖片流:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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),效果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/// <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;
}

?

?

分類: ASP.NET
本文轉自左正博客園博客,原文鏈接:http://www.cnblogs.com/soundcode/p/7458375.html,如需轉載請自行聯系原作者

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/257304.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/257304.shtml
英文地址,請注明出處:http://en.pswp.cn/news/257304.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

html設置百度協議,網站HTML結構SEO要求說明(含移動站)

網頁結構一、網頁中主體結構標簽一一對應。網頁頭部區域網頁底部區域網頁邊框區域網頁導航區域網頁章節、頁眉、頁腳詳情頁文章區域詳情頁作者信息詳情頁中文章的發布日期列表頁中文章列表區域二、其他說明1、首頁head中標注Meta標簽協議&#xff0c;標識對應的網頁瀏覽&#x…

【Fanvas技術解密】HTML5 canvas實現臟區重繪

先說明一下&#xff0c;fanvas是筆者在企鵝公司開發的&#xff0c;即將開源的flash轉canvas工具。 臟區重繪(dirty rectangle)并不是一門新鮮的技術了&#xff0c;這在最早2D游戲誕生的時候就已經存在。 復雜的術語或概念就不多說&#xff0c;簡單說&#xff0c;臟區重繪就是每…

javascript 學習教程

1&#xff0c;JavaScript 是一種輕量級的編程語言&#xff0c;是可插入 HTML 頁面的編程代碼。 2,以<script>標簽開始&#xff0c;以</script>標簽結束。 3,引用放在外部文件的擴展名為.js的腳本文件 <script src"myScript.js"></script> 4&…

我國非按勞分配收入

我國現階段非按勞分配收入探討 我國社會主義初級階段的個人消費品分配&#xff0c;是通過多種分配方式實現的。除了占主體地位的按勞分配方式之外&#xff0c;還存在許多種類的非按勞分配方式。因此&#xff0c;在我國城鄉居民個人收入總額中&#xff0c;除了一部分按勞分配收入…

html調用js里面的函數,html如何調用js函數

html調用js函數的方法&#xff1a;1、用控件本身進行調用&#xff1b;2、通過javascript中的時間控件定時執行&#xff1b;3、通過getElementById調用&#xff1b;4、通過document.getElementsByName調用。本文操作環境&#xff1a;Windows7系統、html5&&javascript1.8…

大部分人都會做錯的經典JS閉包面試題

2019獨角獸企業重金招聘Python工程師標準>>> 由工作中演變而來的面試題 JS中有幾種函數 創建函數的幾種方式 三個fun函數的關系是什么&#xff1f; 函數作用域鏈的問題 到底在調用哪個函數&#xff1f; 后話 轉載于:https://my.oschina.net/u/3687565/blog/1549046

STM32片上Flash內存映射、頁面大小、寄存器映射

轉自&#xff1a;http://blog.chinaunix.net/uid-20617446-id-3847242.html 一、怎么看Flash大小 1.1 通過型號 型號會印在MCU表面&#xff0c;可以通過觀察獲得&#xff0c;我的是STM32F103RBT6(以下分析基于這個型號)&#xff0c;對照下圖的STM32產品命名&#xff0c;可知STM…

如何設計實現一個地址反解析服務?

http://www.cnblogs.com/LBSer/p/4507829.html 一、什么是地址反解析 我們都知道手機定位服務&#xff0c;其本質是匯總各種信號得出一個經緯度坐標&#xff08;x,y&#xff09;&#xff08;具體定位原理可以參考&#xff1a;LBS定位技術、基于樸素貝葉斯的定位算法&#xff09…

html5 hr代碼縮減比例,HTML HR size用法及代碼示例

DOM HR size屬性用于設置或返回元素的size屬性的vlue。用法:它返回HR大小屬性。hrobject.size用于設置HR大小屬性。hrobject.size"value"屬性值&#xff1a;value:它包含指定HR元素高度的像素值。返回值&#xff1a;它返回一個字符串值&#xff0c;該值代表HR元素的高…

【轉】C++標準轉換運算符static_cast

static_cast<new_type> (expression) 雖然const_cast是用來去除變量的const限定&#xff0c;但是static_cast卻不是用來去除變量的static引用。其實這是很容易理解的&#xff0c;static決定的是一個變量的作用域和生命周期&#xff0c;比如&#xff1a;在一個文件中將變量…

MySQL Binlog Mixed模式記錄成Row格式

背景&#xff1a; 一個簡單的主從結構&#xff0c;主的binlog format是Mixed模式&#xff0c;在執行一條簡單的導入語句時&#xff0c;通過mysqlbinlog導出發現記錄的Binlog全部變成了Row的格式&#xff08;明明設置的是Mixed&#xff09;&#xff0c;現在就說說在什么情況下Bi…

update語句中使用子查詢

55. 更改 108 員工的信息: 使其工資變為所在部門中的最高工資, job 變為公司中平均工資最低的 job 1). 搭建骨架 update employees set salary (), job_id () where employee_id 108; 2). 所在部門中的最高工資 select max(salary) from employees where department_id ( s…

html后臺數據分類管理,細分數據.html

&#xfeff;細分數據$axure.utils.getTransparentGifPath function() { return resources/images/transparent.gif; };$axure.utils.getOtherPath function() { return resources/Other.html; };$axure.utils.getReloadPath function() { return resources/reload.html; };…

SpringBoot的配置文件加載順序和使用方式

1、bootstrap.properties bootstrap.properties 配置文件是由“根”上下文優先加載&#xff0c;程序啟動之初就感知 如&#xff1a;Spring Cloud Config指定遠程配置中心地址&#xff0c;就要在這個文件中指定。這樣才能在啟動之初發現遠程配置中心&#xff0c;并從遠程獲取配置…

Get請求

寫在前面的話 XMLHttpRequest對象的open方法的第一個參數為request-type,取值可以為get或post.本篇介紹get請求. get請求的目的,主要是為了獲取數據.雖然get請求可以傳遞數據,但傳遞數據的目的是為了告訴服務器,給我們什么內容. 使用get請求時,參數都是隨url進行傳遞的. 使用ge…

css3中的BFC,IFC,GFC和FFC(轉載)

作者原文網址&#xff1a;http://www.cnblogs.com/dingyufenglian/p/4845477.html What‘s FC&#xff1f; 一定不是KFC&#xff0c;FC的全稱是&#xff1a;Formatting Contexts&#xff0c;是W3C CSS2.1規范中的一個概念。它是頁面中的一塊渲染區域&#xff0c;并且有一套渲染…

javaweb學習總結——Filter高級開發

在filter中可以得到代表用戶請求和響應的request、response對象&#xff0c;因此在編程中可以使用Decorator(裝飾器)模式對request、response對象進行包裝&#xff0c;再把包裝對象傳給目標資源&#xff0c;從而實現一些特殊需求。 一、Decorator設計模式 1.1、Decorator設計模…

html期末網頁設計,求網頁設計的期末作業一份 HTML的

1&#xff0e; 課程設計建議主題方向&#xff1a;電子商務類網站、門戶類網站、專題類網站。整體要求&#xff1a;主題鮮明、健康&#xff1b;風格自然、內容充實、完整&#xff1b;布局合理&#xff0c;配色和諧。(5分)2&#xff0e; 網站至少包括15張頁面(包括首頁)&#x…

Android(java)學習筆記10:同步中的死鎖問題以及線程通信問題

1. 同步弊端&#xff1a; &#xff08;1&#xff09;效率低 &#xff08;2&#xff09;如果出現了同步嵌套&#xff0c;就容易產生死鎖問題 死鎖問題及其代碼 &#xff1a; &#xff08;1&#xff09;是指兩個或者兩個以上的線程在執行的過程中&#xff0c;因爭奪資源產生的一種…

4源代碼的下載和編譯

1、Android移植主要就是Linux內核移植&#xff0c;而Linux內核移植主要是Linux驅動移植&#xff0c;為了開發和測試Linux驅動&#xff0c;要在Ubuntu下搭建兩套開發環境&#xff1a;Android應用程序開發環境和Linux內核開發環境&#xff1b; 2、Android源代碼包括&#xff1a;內…