二维码生成组件:Gma.QrCodeNet,无论用哪个组件都一样,目的是获得二维码数据。
思路:先生成二维码图,然后在二维码图上画logo。再把画了logo的二维码图,画在背景图上。
string qrcodecont = $"https://www.yangshaofeng.com";//二维码内容 string logopath = AppDomain.CurrentDomain.BaseDirectory + $"poster/logo.png";//Logo图片路径 string bgpath = AppDomain.CurrentDomain.BaseDirectory + $"poster/bg.png";//背景图路径 string savepath = AppDomain.CurrentDomain.BaseDirectory + $"poster/qrcode.png";//成图保存路径 int qrcodewidth = 280;//二维码图片在背景图上的高宽 int qrcodetop = 465;//二维码距离背景图顶部的距离 var encoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = encoder.Encode(qrcodecont); GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(14, QuietZoneModules.Zero), Brushes.Black, Brushes.White); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); Bitmap mapqrcode = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); Graphics g = Graphics.FromImage(mapqrcode); render.Draw(g, qrCode.Matrix); Image logo = Image.FromFile(logopath); int logowidth = (int)(mapqrcode.Width / 2 * 0.5); Point iconPoint = new Point((mapqrcode.Width - logowidth) / 2, (mapqrcode.Height - logowidth) / 2); g.DrawImage(logo, iconPoint.X, iconPoint.Y, logowidth, logowidth); Bitmap map = new Bitmap(bgpath); Graphics g2 = Graphics.FromImage(map); g2.DrawImage(mapqrcode, (map.Width - qrcodewidth) / 2, qrcodetop, qrcodewidth, qrcodewidth); map.Save(savepath, ImageFormat.Png); mapqrcode?.Dispose(); g?.Dispose(); logo?.Dispose(); map?.Dispose(); g2?.Dispose();
留下您的脚步
最近评论