//生成中文二维码-支持中文(自定义大小) //暂时未完善 只定义了背景大小 static void Generate(string url, string filename, string savapath, ImgType type = ImgType.Png, int?width = null, int?high = null) { QrEncoder qrEncoder = new QrEncoder(); var qrCode = qrEncoder.Encode(url); //保存成png文件 //string filename = @"H:\桌面\截图\cn.png"; string suffix = ImgSuffix.Getsuffix(type);; //后缀 filename = $"{savapath}{filename}{suffix}"; GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White); int w = 500; int h = 500; if (width != null) { w = (int)width; } if (high != null) { h = (int)high; } Bitmap map = new Bitmap(w, h); Graphics g = Graphics.FromImage(map); g.FillRectangle(Brushes.White, 0, 0, w, h); render.Draw(g, qrCode.Matrix, new Point(20, 20)); map.Save(filename, ImageFormat.Png); }
/// <summary> /// 二维码生成 /// </summary> /// <param name="url">二维码网站地址或者数据</param> /// <param name="filename">文件名</param> /// <param name="savapath">保存地址</param> /// <param name="type">图片格式</param> public static void Generate(string url, string filename, string savapath, ImgType type = ImgType.Png) { QrEncoder qrEncoder = new QrEncoder(); var qrCode = qrEncoder.Encode(url); //保存成png文件 //string filename = @"H:\桌面\截图\url.png"; string suffix = ImgSuffix.Getsuffix(type);; //后缀 if (!Directory.Exists(savapath)) { Directory.CreateDirectory(savapath); } filename = $"{savapath}{filename}{suffix}"; GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White); using (FileStream stream = new FileStream(filename, FileMode.Create)) { render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream); } }
/// </summary> /// 文本内容 /// <param name="text">只支持数字 只支持偶数个 最大长度80</param> /// <param name="serverPth">存储路径 @"H:\桌面\截图\"</param> /// <param name="filename">文件名不带后缀</param> /// <param name="type">文件格式</param> /// <param name="width">宽度比例3</param> /// <param name="hight">高度比例1</param> public static void Generate2(string text, string serverPth, string filename, ImgType type, /*ImageFormat imgFrt,*/ int?width = null, int?hight = null) { BarcodeWriter writer = new BarcodeWriter(); //使用ITF 格式,不能被现在常用的支付宝、微信扫出来 //如果想生成可识别的可以使用 CODE_128 格式 //writer.Format = BarcodeFormat.ITF; writer.Format = BarcodeFormat.CODE_128; int w = 150; int h = 50; if (width != null) { w = (int)width; } if (hight != null) { h = (int)hight; } EncodingOptions options = new EncodingOptions() { Width = w, Height = h, Margin = 2 }; writer.Options = options; Bitmap map = writer.Write(text); string suffix = ImgSuffix.Getsuffix(type);; //后缀 if (!Directory.Exists(serverPth)) { Directory.CreateDirectory(serverPth); } filename = $"{serverPth}{filename}{suffix}";//请注意 保存格式和文件扩展名一致性 map.Save(filename); }
/// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name="text">文本内容</param> public static void Generate(string text, string LogoPth, string serverPth, string filename, ImgType type, /* ImageFormat imgFrt,*/ int?width = null, int?hight = null) { try { //Logo 图片 Bitmap logo = new Bitmap(LogoPth); //构造二维码写码器 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); int w = 300; int h = 300; if (width != null) { w = (int)width; } if (hight != null) { h = (int)hight; } //生成二维码 ZXing.Common.BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, w, h, 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 suffix = ImgSuffix.Getsuffix(type);; //后缀 if (!Directory.Exists(serverPth)) { Directory.CreateDirectory(serverPth); } //保存成图片 bmpimg.Save($"{serverPth}{filename}{suffix}"); } catch (Exception ex) { Console.WriteLine(ex.Message); } }