C#使用QRCode生成海報圖并嵌入定位帶logo的二維碼
簡介
本案例適用在市場部同事做推廣營銷時推送個人專屬鏈接,綁定自身專屬客戶,引導客戶了解產(chǎn)品等各方面業(yè)務的一種引導模式。
框架環(huán)境介紹
- 控制臺應用程序
- .NET Framework v4.5.2
- 組件 QRCoder
- vs2019
創(chuàng)建項目
使用vs創(chuàng)建控制臺應用程序,框架版本可以根據(jù)自己實際情況選擇,

引用與其框架對應不沖突版本組件QRCoder
右鍵項目 吊起NuGet引用資源窗口,在瀏覽處搜索QRCoder插件


準備業(yè)務場景所需要的東西
在項目\bin\Debug下新建四個文件夾,dingwei 海報圖模板 ,logo 二維碼logo,new 生成的海報,
user 生成的二維碼logo臨時存儲區(qū)

業(yè)務代碼
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è)務場景:本次是通過一個表單帶上市場部員工id
//生成二維碼,并定位到海報模板上,隨后綁定到該
//員工,此刻該員工就擁有自己的推廣二維碼
//假設 員工id ,真實業(yè)務場景應從庫中抓取未生成二維碼的員工id,定時作業(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ù)庫,或者其他不同形式業(yè)務
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">市場部用戶id,也作為生成圖的名字標識</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;
}
}
}最終效果



到此這篇關于C#使用QRCode生成海報圖并嵌入定位帶logo的二維碼的文章就介紹到這了,更多相關C# QRCode生成海報圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#?WinForm?RichTextBox文本動態(tài)滾動顯示文本方式
這篇文章主要介紹了C#?WinForm?RichTextBox文本動態(tài)滾動顯示文本方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
C#使用WinRar命令進行壓縮和解壓縮操作的實現(xiàn)方法
這篇文章主要介紹了C#使用WinRar命令進行壓縮和解壓縮操作的實現(xiàn)方法,涉及C#基于Process類操作WinRar命令的相關實現(xiàn)技巧,代碼簡潔實用,需要的朋友可以參考下2016-06-06
C#使用GZipStream實現(xiàn)文件的壓縮與解壓
這篇文章主要為大家詳細介紹了C#使用GZipStream實現(xiàn)文件的壓縮與解壓,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10

