C#使用ThoughtWorks.QRCode生成二維碼
關(guān)于 ThoughtWorks.QRCode
二維碼是用某種特定的幾何圖形按一定規(guī)律在平面分布的、黑白相間的、記錄數(shù)據(jù)符號(hào)信息的圖形,在應(yīng)用程序開(kāi)發(fā)中也被廣泛使用,諸如信息獲取(如關(guān)注微信公眾號(hào))、網(wǎng)站跳轉(zhuǎn)(寫(xiě)入U(xiǎn)rl)、防偽查詢(反饋查詢結(jié)果)、手機(jī)支付(如微信支付、支付寶支付)、會(huì)員登錄(掃碼登錄方式)等等。
ThoughtWorks.QRCode是一款功能強(qiáng)勁的動(dòng)態(tài)鏈接庫(kù),能夠?yàn)?net應(yīng)用生成二維碼,QR 全稱為 Quick Response,是一種編碼方式。
開(kāi)發(fā)運(yùn)行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
.net版本: .netFramework4.0 或以上
開(kāi)發(fā)工具:VS2019 C#
方法設(shè)計(jì)
public bool CreateQrCode 方法(創(chuàng)建二維碼)調(diào)用參數(shù)見(jiàn)如下表格:
序號(hào) | 參數(shù)名 | 類型 | 說(shuō)明 |
---|---|---|---|
1 | Content | string | 要寫(xiě)入的內(nèi)容,如Url鏈接地址等 |
2 | ImagePath | string | 要生成的目標(biāo)二維碼圖片物理文件路徑 |
3 | QRCodeScale | int | 二維碼像素大小,值越大生成的二維碼圖片像素越高尺寸越大 |
4 | backgroundColor | System.Drawing.Color | 二維碼的背景顏色,建議設(shè)置為白色 |
5 | foreColor | System.Drawing.Color | 二維碼的前景顏色,建議設(shè)置為黑色 |
6 | logoImage="" | string | 可選擇是否在二維碼圖片中間添加Logo小圖標(biāo),默認(rèn)值為“”字符串,即表示不設(shè)置,如果設(shè)置則填入圖標(biāo)物理文件路徑,方法會(huì)判斷該文件是否存在,存在則嘗試添加 |
本方法返回 bool 值 ,表示填入?yún)?shù) ImagePath 的文件是否存在,為True則表示成功,程序可以后續(xù)繼續(xù)處理其它業(yè)務(wù)。
代碼實(shí)現(xiàn)
public bool CreateQrCode(string Content, string ImagePath, int QRCodeScale, System.Drawing.Color backgroundColor,System.Drawing.Color foreColor,string logoImage="") { ThoughtWorks.QRCode.Codec.QRCodeEncoder encoder = new ThoughtWorks.QRCode.Codec.QRCodeEncoder(); encoder.QRCodeEncodeMode = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE;//編碼方式(注意:BYTE能支持中文,ALPHA_NUMERIC掃描出來(lái)的都是數(shù)字) encoder.QRCodeScale = QRCodeScale;//大小(值越大生成的二維碼圖片像素越高) encoder.QRCodeVersion = 0;//版本(注意:設(shè)置為0主要是防止編碼的字符串太長(zhǎng)時(shí)發(fā)生錯(cuò)誤) encoder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M;//錯(cuò)誤效驗(yàn)、錯(cuò)誤更正(有4個(gè)等級(jí)) encoder.QRCodeBackgroundColor = backgroundColor; encoder.QRCodeForegroundColor = foreColor; System.Drawing.Bitmap bcodeBitmap = encoder.Encode(Content,Encoding.UTF8); //FileStream fs = new FileStream(ImagePath, FileMode.OpenOrCreate); bcodeBitmap.Save(ImagePath,System.Drawing.Imaging.ImageFormat.Jpeg); if (logoImage != "") { System.Drawing.Bitmap btm = new System.Drawing.Bitmap(logoImage); System.Drawing.Bitmap copyImage = new System.Drawing.Bitmap(btm,bcodeBitmap.Width / 4,bcodeBitmap.Height / 4); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bcodeBitmap); int x = bcodeBitmap.Width / 2 - copyImage.Width / 2; int y = bcodeBitmap.Height / 2 - copyImage.Height / 2; g.DrawImage(copyImage, x, y); bcodeBitmap.Save(ImagePath, System.Drawing.Imaging.ImageFormat.Jpeg); // CombinImage(bcodeBitmap, "").Save(ImagePath); } IntPtr ip = bcodeBitmap.GetHbitmap(); bcodeBitmap.Dispose(); //fs.Close(); //fs.Dispose(); DeleteObject(ip); GC.Collect(); // File.Delete(ImagePath); return File.Exists(ImagePath); }
調(diào)用示例
假設(shè)服務(wù)器有 d:\logo.jpg 做為二維碼附加圖標(biāo),最終組合生成到 d:\1.jpg。前端頁(yè)面放置 Image控件 image1,則示例代碼如下:
bool ss=CreateQrCode("https://www.baidu.com", "d:\\1.jpg", 20, System.Drawing.Color.White, System.Drawing.Color.Black, "d:\\logo.jpg"); if (ss == true) { string result_base64 = ImgToBase64String("d:\\1.jpg", true); image1.ImageUrl = result_base64; return; }
生成結(jié)果如下圖所示:
Logo圖標(biāo)透明化
可以將Logo圖標(biāo)透明化,增加一點(diǎn)樂(lè)趣。public void ImageToPNG 方法參數(shù)調(diào)用說(shuō)明如下:
序號(hào) | 參數(shù)名 | 類型 | 說(shuō)明 |
---|---|---|---|
1 | sourceFilename | string | 源圖片文件物理路徑 |
2 | pngFilename | string | 要生成的png文件物理路徑 |
3 | backcolor | System.Drawing.Color | 要變透明而要去除的主背景色 |
實(shí)現(xiàn)代碼如下:
public void ImageToPNG(string sourceFilename,string pngFilename,System.Drawing.Color backcolor) { System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(sourceFilename); bmp.MakeTransparent(System.Drawing.Color.FromArgb(0, backcolor)); bmp.Save(pngFilename,System.Drawing.Imaging.ImageFormat.Png); }
調(diào)用示例:
ImageToPNG("d:\\logo.jpg", "d:\\logo.png", System.Drawing.Color.White); bool ss=mb.CreateQrCode("https://www.baidu.com", "d:\\1.jpg", 20, System.Drawing.Color.White, System.Drawing.Color.Black, "d:\\logo.png"); if (ss == true) { string result_base64 = mb.ImgToBase64String("d:\\1.jpg", true); image1.ImageUrl = result_base64; return; }
生成結(jié)果如下圖所示:
實(shí)現(xiàn)方法可以根據(jù)我們的實(shí)際開(kāi)發(fā)需要進(jìn)一步進(jìn)行修改,如二維碼圖片的大小、Logo的大小、質(zhì)量等。
如何獲取圖像 base64 數(shù)據(jù)的方法請(qǐng)參照我的文章:《C# 自動(dòng)填充文字內(nèi)容到指定圖片》
以上就是C#使用ThoughtWorks.QRCode生成二維碼的詳細(xì)內(nèi)容,更多關(guān)于C# ThoughtWorks.QRCode生成二維碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實(shí)現(xiàn)的字符串轉(zhuǎn)MD5碼函數(shù)實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的字符串轉(zhuǎn)MD5碼函數(shù),結(jié)合簡(jiǎn)單實(shí)例形式分析了C#字符串的轉(zhuǎn)換、遍歷、加密等操作技巧,需要的朋友可以參考下2016-07-07C#單例模式Singleton的實(shí)現(xiàn)詳解
單例模式(Singleton?Pattern)是日常開(kāi)發(fā)中最簡(jiǎn)單的設(shè)計(jì)模式之一,它提供了一種創(chuàng)建對(duì)象的最佳方式,本文主要為大家介紹的是C#單例模式的實(shí)現(xiàn)方法,需要的可以參考一下2023-05-05淺談C#在網(wǎng)絡(luò)波動(dòng)時(shí)防重復(fù)提交的方法
這篇文章主要介紹了淺談C#在網(wǎng)絡(luò)波動(dòng)時(shí)防重復(fù)提交的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04C#實(shí)現(xiàn)提高xml讀寫(xiě)速度的方法
這篇文章主要介紹了C#實(shí)現(xiàn)提高xml讀寫(xiě)速度的方法,并且針對(duì)各類文件的讀寫(xiě)做了較為細(xì)致的分析,非常實(shí)用,需要的朋友可以參考下2014-11-11winform使用委托和事件來(lái)完成兩個(gè)窗體之間通信的實(shí)例
這篇文章介紹了winform使用委托和事件來(lái)完成兩個(gè)窗體之間通信的實(shí)例,有需要的朋友可以參考一下2013-09-09C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器
這篇文章主要介紹了C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08C#中參數(shù)個(gè)數(shù)可變的方法實(shí)例分析
這篇文章主要介紹了C#中參數(shù)個(gè)數(shù)可變的方法,以一個(gè)簡(jiǎn)單實(shí)例分析了C#中參數(shù)個(gè)數(shù)可變的方法,主要是使用params關(guān)鍵字來(lái)實(shí)現(xiàn)的,是C#編程中比較實(shí)用的技巧,需要的朋友可以參考下2014-11-11在Framework 4.0中:找出新增的方法與新增的類(一)
經(jīng)??吹接型瑢W(xué)在討論Framework 4 的新特性,新方法,于是想寫(xiě)個(gè)程序找出framework4.0中新增的方法和類2013-05-05