C#使用ThoughtWorks.QRCode生成二維碼
關(guān)于 ThoughtWorks.QRCode
二維碼是用某種特定的幾何圖形按一定規(guī)律在平面分布的、黑白相間的、記錄數(shù)據(jù)符號信息的圖形,在應(yīng)用程序開發(fā)中也被廣泛使用,諸如信息獲?。ㄈ珀P(guān)注微信公眾號)、網(wǎng)站跳轉(zhuǎn)(寫入Url)、防偽查詢(反饋查詢結(jié)果)、手機支付(如微信支付、支付寶支付)、會員登錄(掃碼登錄方式)等等。
ThoughtWorks.QRCode是一款功能強勁的動態(tài)鏈接庫,能夠為.net應(yīng)用生成二維碼,QR 全稱為 Quick Response,是一種編碼方式。
開發(fā)運行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
.net版本: .netFramework4.0 或以上
開發(fā)工具:VS2019 C#
方法設(shè)計
public bool CreateQrCode 方法(創(chuàng)建二維碼)調(diào)用參數(shù)見如下表格:
序號 | 參數(shù)名 | 類型 | 說明 |
---|---|---|---|
1 | Content | string | 要寫入的內(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)物理文件路徑,方法會判斷該文件是否存在,存在則嘗試添加 |
本方法返回 bool 值 ,表示填入?yún)?shù) ImagePath 的文件是否存在,為True則表示成功,程序可以后續(xù)繼續(xù)處理其它業(yè)務(wù)。
代碼實現(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掃描出來的都是數(shù)字) encoder.QRCodeScale = QRCodeScale;//大小(值越大生成的二維碼圖片像素越高) encoder.QRCodeVersion = 0;//版本(注意:設(shè)置為0主要是防止編碼的字符串太長時發(fā)生錯誤) encoder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M;//錯誤效驗、錯誤更正(有4個等級) 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。前端頁面放置 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)透明化,增加一點樂趣。public void ImageToPNG 方法參數(shù)調(diào)用說明如下:
序號 | 參數(shù)名 | 類型 | 說明 |
---|---|---|---|
1 | sourceFilename | string | 源圖片文件物理路徑 |
2 | pngFilename | string | 要生成的png文件物理路徑 |
3 | backcolor | System.Drawing.Color | 要變透明而要去除的主背景色 |
實現(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é)果如下圖所示:
實現(xiàn)方法可以根據(jù)我們的實際開發(fā)需要進一步進行修改,如二維碼圖片的大小、Logo的大小、質(zhì)量等。
如何獲取圖像 base64 數(shù)據(jù)的方法請參照我的文章:《C# 自動填充文字內(nèi)容到指定圖片》
以上就是C#使用ThoughtWorks.QRCode生成二維碼的詳細內(nèi)容,更多關(guān)于C# ThoughtWorks.QRCode生成二維碼的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實現(xiàn)的字符串轉(zhuǎn)MD5碼函數(shù)實例
這篇文章主要介紹了C#實現(xiàn)的字符串轉(zhuǎn)MD5碼函數(shù),結(jié)合簡單實例形式分析了C#字符串的轉(zhuǎn)換、遍歷、加密等操作技巧,需要的朋友可以參考下2016-07-07淺談C#在網(wǎng)絡(luò)波動時防重復(fù)提交的方法
這篇文章主要介紹了淺談C#在網(wǎng)絡(luò)波動時防重復(fù)提交的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器
這篇文章主要介紹了C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08在Framework 4.0中:找出新增的方法與新增的類(一)
經(jīng)??吹接型瑢W(xué)在討論Framework 4 的新特性,新方法,于是想寫個程序找出framework4.0中新增的方法和類2013-05-05