添加NuGet包引用:到浏览 搜索,添加引用 ZXing.Net 包 安装。
废话不都说,直接上代码,添加一个方法名为:QRCode,text参数是二维码内容。
public void QRCode(string text)
{
//获取中间图片logo
Bitmap logo = new Bitmap(Request.PhysicalApplicationPath + "templets/yixin/images/wmdfw1.jpg");
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
//生成二维码
QrCodeEncodingOptions options = new QrCodeEncodingOptions();
options.DisableECI = true;
options.CharacterSet = "UTF-8";
options.Width = 258;
options.Height = 258;
options.Margin = 1;
writer.Options = options;
Bitmap bmpimg = writer.Write(text);
//将二维码插入图片
using (Graphics myGraphic = Graphics.FromImage(bmpimg))
{
int Width = 60;
int Height = 60;
int middleL = (bmpimg.Width - Width) / 2;
int middleT = (bmpimg.Height - Height) / 2;
myGraphic.DrawImage(logo, middleL, middleT, Width, Height);
}
//保存成图片
string filename = Request.PhysicalApplicationPath + "/templets/yixin/images/wxkf.png";
bmpimg.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
bmpimg.Dispose();
}