C#使用QRCode生成海報(bào)圖并嵌入定位帶logo的二維碼
簡(jiǎn)介
本案例適用在市場(chǎng)部同事做推廣營(yíng)銷時(shí)推送個(gè)人專屬鏈接,綁定自身專屬客戶,引導(dǎo)客戶了解產(chǎn)品等各方面業(yè)務(wù)的一種引導(dǎo)模式。
框架環(huán)境介紹
- 控制臺(tái)應(yīng)用程序
- .NET Framework v4.5.2
- 組件 QRCoder
- vs2019
創(chuàng)建項(xiàng)目
使用vs創(chuàng)建控制臺(tái)應(yīng)用程序,框架版本可以根據(jù)自己實(shí)際情況選擇,
引用與其框架對(duì)應(yīng)不沖突版本組件QRCoder
右鍵項(xiàng)目 吊起NuGet引用資源窗口,在瀏覽處搜索QRCoder插件
準(zhǔn)備業(yè)務(wù)場(chǎng)景所需要的東西
在項(xiàng)目\bin\Debug下新建四個(gè)文件夾,dingwei 海報(bào)圖模板 ,logo 二維碼logo,new 生成的海報(bào),
user 生成的二維碼logo臨時(shí)存儲(chǔ)區(qū)
業(yè)務(wù)代碼
using QRCoder; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; namespace QRCoder_img { class Program { static void Main(string[] args) { //業(yè)務(wù)場(chǎng)景:本次是通過(guò)一個(gè)表單帶上市場(chǎng)部員工id //生成二維碼,并定位到海報(bào)模板上,隨后綁定到該 //員工,此刻該員工就擁有自己的推廣二維碼 //假設(shè) 員工id ,真實(shí)業(yè)務(wù)場(chǎng)景應(yīng)從庫(kù)中抓取未生成二維碼的員工id,定時(shí)作業(yè)執(zhí)行。 var id = 1; // 要生成的二維碼鏈接 var url="http://baidu.com?id="+id; //生成的二維碼路徑 string ewm = ""; //生成二維碼,并返回路徑 RenderQrCode(url, id, ref ewm); //定位生成新的圖 Image img = Image.FromFile(ewm); Image imgBack = Image.FromFile(System.IO.Directory.GetCurrentDirectory() + "/dingwei/1.png"); Bitmap bmp = CombinImage(270, 270, imgBack, img, 227, 668); //最終生成圖的路徑,可在此之后同步數(shù)據(jù)庫(kù),或者其他不同形式業(yè)務(wù) string str = System.IO.Directory.GetCurrentDirectory() + "/new/1tg1.png"; if (System.IO.File.Exists(str)) System.IO.File.Delete(str); bmp.Save(str, ImageFormat.Png); bmp.Clone(); bmp.Dispose(); img.Dispose(); System.IO.File.Delete(ewm); } /// <summary> /// 生成帶有l(wèi)ogo的二維碼 /// </summary> /// <param name="url">二維碼鏈接</param> /// <param name="id">市場(chǎng)部用戶id,也作為生成圖的名字標(biāo)識(shí)</param> /// <param name="ewm">生成圖的路徑</param> public static void RenderQrCode(string url, int id, ref string ewm) { string level = "L"; QRCodeGenerator.ECCLevel eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3); QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(url, eccLevel); QRCode qrCode = new QRCode(qrCodeData); var imageewm = qrCode.GetGraphic(20, Color.Black, Color.White, GetIconBitmap(System.IO.Directory.GetCurrentDirectory() + "/logo/appicon.png")); string str = System.IO.Directory.GetCurrentDirectory() + "/user/user" + id + ".jpg"; if (System.IO.File.Exists(str)) System.IO.File.Delete(str); imageewm.Save(str, ImageFormat.Jpeg); imageewm.Clone(); imageewm.Dispose(); qrCode.Dispose(); qrCodeData.Dispose(); ewm = str; } public static Bitmap GetIconBitmap(string url) { Bitmap img = null; if (url.Length > 0) { try { img = new Bitmap(url); } catch (Exception) { } } return img; } /// <summary> /// 兩張圖合并 /// </summary> /// <param name="width">二維碼繪畫大小</param> /// <param name="height">二維碼繪畫大小</param> /// <param name="imgBack">模板圖</param> /// <param name="img">二維碼圖</param> /// <param name="xDeviation">定位x</param> /// <param name="yDeviation">定位Y</param> /// <returns></returns> public static Bitmap CombinImage(int width, int height, Image imgBack, Image img, int xDeviation = 0, int yDeviation = 0) { Bitmap bmp = new Bitmap(imgBack.Width, imgBack.Height); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); g.DrawImage(img, xDeviation, yDeviation, width, height); GC.Collect(); g.Dispose(); return bmp; } } }
最終效果
到此這篇關(guān)于C#使用QRCode生成海報(bào)圖并嵌入定位帶logo的二維碼的文章就介紹到這了,更多相關(guān)C# QRCode生成海報(bào)圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
剖析設(shè)計(jì)模式編程中C#對(duì)于組合模式的運(yùn)用
這篇文章主要介紹了設(shè)計(jì)模式編程中C#對(duì)于組合模式的運(yùn)用,理論上來(lái)說(shuō)組合模式包含抽象構(gòu)件、樹(shù)葉構(gòu)件和樹(shù)枝構(gòu)件三個(gè)角色,需要的朋友可以參考下2016-02-02C#?WinForm?RichTextBox文本動(dòng)態(tài)滾動(dòng)顯示文本方式
這篇文章主要介紹了C#?WinForm?RichTextBox文本動(dòng)態(tài)滾動(dòng)顯示文本方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法
這篇文章主要介紹了C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法,涉及C#基于Process類操作WinRar命令的相關(guān)實(shí)現(xiàn)技巧,代碼簡(jiǎn)潔實(shí)用,需要的朋友可以參考下2016-06-06C#使用GZipStream實(shí)現(xiàn)文件的壓縮與解壓
這篇文章主要為大家詳細(xì)介紹了C#使用GZipStream實(shí)現(xiàn)文件的壓縮與解壓,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10.Net6開(kāi)發(fā)winform程序使用依賴注入
本文詳細(xì)講解了.Net6開(kāi)發(fā)winform程序使用依賴注入的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12