企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 方法调用 ``` string qrcode = QRCode.Generate("http://" + HttpContext.Current.Request.Url.Host + "/CLindex/QRCodeView.aspx?KeyID=" + KeyId, @"CLindex\images\logo_1.png", model.identNum); ``` # 代码 ### 其中NetWing.BPM.Core.WeChatApi里面调用了随机数Operation.Number(6)方法,自行替换或删除即可 ``` using NetWing.BPM.Core.WeChatApi; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using ZXing; using ZXing.Common; using ZXing.QrCode.Internal; namespace NetWing.BPM.Core { public class QRCode { /// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name="url">链接</param> /// <param name="LogoPath">logo地址</param> /// <param name="PathUrl">文件夹名</param> public static string Generate(string url, string LogoPath, string PathUrl) { //Logo 图片 string path = AppDomain.CurrentDomain.BaseDirectory + LogoPath; Bitmap logo = new Bitmap(@path); //构造二维码写码器 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>(); hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //生成二维码 BitMatrix bm = writer.encode(url, BarcodeFormat.QR_CODE, 300, 300, hint); BarcodeWriter barcodeWriter = new BarcodeWriter(); Bitmap map = barcodeWriter.Write(bm); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) int[] rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置 int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width); int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height); int middleL = (map.Width - middleW) / 2; int middleT = (map.Height - middleH) / 2; //将img转换成bmp格式,否则后面无法创建Graphics对象 Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(map, 0, 0); } //将二维码插入图片 Graphics myGraphic = Graphics.FromImage(bmpimg); //白底 myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH); myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH); //保存成图片 string SavePath = AppDomain.CurrentDomain.BaseDirectory; string dates = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string SavePathUrl = @"Upload/QRCode/" + PathUrl + @"/"; if (!string.IsNullOrEmpty(SavePath)) { SavePath += @"Upload\QRCode\" + PathUrl + @"\"; if (!Directory.Exists(SavePath)) { Directory.CreateDirectory(SavePath); } SavePath += dates + "_" + Operation.Number(6) + ".png"; SavePathUrl += dates + "_" + Operation.Number(6) + ".png"; bmpimg.Save(@SavePath, ImageFormat.Png); } return SavePathUrl; } } } ```