using SixLabors.Fonts; using SixLabors.ImageSharp.Drawing.Processing; using SixLabors.ImageSharp.Formats.Png; using ZXing; namespace Crm.Core.Common.Helpers { public class QRCodeHelper { public byte[] Create(string content, int width) { var writer = new ZXing.ImageSharp.BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = new ZXing.QrCode.QrCodeEncodingOptions { DisableECI = true, CharacterSet = "UTF-8", Width = width, Height = width, Margin = 1 } }; var image = writer.WriteAsImageSharp(content); using (var ms = new MemoryStream()) { image.Save(ms, new PngEncoder()); return ms.ToArray(); } } public byte[] Create(string content, int width, string bg, int x, int y) { var qrcode = Create(content, width); var qrcode_img = Image.Load(qrcode); var bg_img = Image.Load(bg); bg_img.Mutate(m => m.DrawImage(qrcode_img, new Point(x, y), 1)); using (var ms = new MemoryStream()) { bg_img.SaveAsPngAsync(ms); return ms.ToArray(); } } /// /// 生成二维码 /// /// /// /// /// /// /// /// -1表示自动居中 /// /// public byte[] Create(string content, int width, string bg, int x, int y, string title, int title_x, int title_y) { var qrcode = Create(content, width); var qrcode_img = Image.Load(qrcode); var bg_img = Image.Load(bg); bg_img.Mutate(m => m.DrawImage(qrcode_img, new Point(x, y), 1)); var fonts = new FontCollection(); var fontFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "fonts", "SIMHEI.TTF"); var fontFamily = fonts.Add(fontFile); var font = new Font(fontFamily, 14, FontStyle.Bold); if (title_x<0) { var fontRect = TextMeasurer.Measure(title, new TextOptions(font)); title_x = Convert.ToInt32(0.5*bg_img.Width) - Convert.ToInt32(0.5*fontRect.Width); } bg_img.Mutate(m => m.DrawText(title, font, Color.White, new PointF(title_x, title_y))); using (var ms = new MemoryStream()) { bg_img.SaveAsPngAsync(ms); return ms.ToArray(); } } /// /// 生成二维码 /// /// /// /// /// /// /// /// -1表示自动居中 /// /// /// public byte[] Create(string content, int width, string bg, int x, int y, string title, int title_x, int title_y, string subtitle) { var qrcode = Create(content, width); var qrcode_img = Image.Load(qrcode); var bg_img = Image.Load(bg); bg_img.Mutate(m => m.DrawImage(qrcode_img, new Point(x, y), 1)); var fonts = new FontCollection(); var fontFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "fonts", "SIMHEI.TTF"); var fontFamily = fonts.Add(fontFile); var font = new Font(fontFamily, 14, FontStyle.Bold); var subtitle_x = 0; var subtitle_y = 0; if (title_x<0) { var fontRect = TextMeasurer.Measure(title, new TextOptions(font)); title_x = Convert.ToInt32(0.5*bg_img.Width) - Convert.ToInt32(0.5*fontRect.Width); var fontRect2 = TextMeasurer.Measure(subtitle, new TextOptions(font)); subtitle_x = Convert.ToInt32(0.5*bg_img.Width) - Convert.ToInt32(0.5*fontRect2.Width); subtitle_y = Convert.ToInt32(fontRect.Height) + title_y + 10; } bg_img.Mutate(m => m.DrawText(title, font, Color.White, new PointF(title_x, title_y))); bg_img.Mutate(m => m.DrawText(subtitle, font, Color.White, new PointF(subtitle_x, subtitle_y))); using (var ms = new MemoryStream()) { bg_img.SaveAsPngAsync(ms); return ms.ToArray(); } } } }