/// <summary> /// 添加icon /// </summary> public static byte[] AddIcon(this QrCode coder, byte[] bs, string icon_path) { using (var bm = ConvertHelper.BytesToBitmap(bs)) { using (var ms = new MemoryStream()) { using (var imageFactory = new ImageFactory(preserveExifData: true)) { using (var iconFactory = new ImageFactory(preserveExifData: true)) { var icon = iconFactory.Load(icon_path) .Resize(new Size() { Width = bm.Width / 5, Height = bm.Height / 5 }).Image; var overlay = new ImageLayer() { Image = icon, Position = new Point((bm.Width - icon.Width) / 2, (bm.Height - icon.Height) / 2), Size = new Size(icon.Width, icon.Height) }; imageFactory.Load(bm) .Overlay(overlay) .Format(new JpegFormat { Quality = 100 }) .Save(ms); return(ms.ToArray()); } } } } }
/// <summary> /// 带图标二维码,加上图标后请把容错级别调高,这样可以提高识别成功率 /// </summary> public static byte[] GetQrCodeWithIconBytes(this QrCode coder, string content, string icon_path, int size = QrCode.QRCODE_SIZE) { if (!File.Exists(icon_path)) { throw new Exception("二维码水印图片不存在"); } var bs = coder.GetQrCodeBytes(content, size); using (var bm = ConvertHelper.BytesToBitmap(bs)) { using (var g = Graphics.FromImage(bm)) { using (var logo = Image.FromFile(icon_path)) { using (var smallLogo = logo.GetThumbnailImage(bm.Width / 5, bm.Height / 5, null, IntPtr.Zero)) { //把压缩后的图片绘制到二维码上 g.DrawImage(smallLogo, (bm.Width - smallLogo.Width) / 2, (bm.Height - smallLogo.Height) / 2); } } } return(bm.ToBytes(coder.Formart)); } }