C#⽣成带背景和⽂字的⼆维码图⽚
来电大头贴/// <summary>
/// ⽣成带背景和⽂字的⼆维码图⽚
/// </summary>
/// <param name="content">内容</param>
/// <param name="text">⼆维码底部⽂字</param>
/// <param name="logoPath">背景图⽚路径</param>
/// <param name="savePath">保存路径</param>
/// <param name="moduleSize">⼆维码⼤⼩</param>
有关兔的成语十大悍匪/// <returns>返回⼆维码图⽚路径</returns>
public static string CreateQRCode(string content, string text, string logoPath, string savePath, int moduleSize = 9) {
//绘制⼆维码图⽚
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
QrCode qrCode = qrEncoder.Encode(content);
GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White);
DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
Graphics g = Graphics.FromImage(map);
render.Draw(g, qrCode.Matrix);
//添加底部⽂字效果
Font font = new Font("GB2312", 8, FontStyle.Regular);//设置字体,⼤⼩,粗细后门进狼
SolidBrush sbrush = new SolidBrush(Color.Black);//设置颜⾊
//g.Clear(Color.White);
//g.DrawString(name, font, sbrush, new PointF((Int32)(map.Width - name.Length * 10.8) / 2, map.Height - 15));
g.DrawString(text, font, sbrush, new PointF((Int32)(map.Width - text.Length * 10.8) / 2, map.Height - 15));
//添加背景图
Image img = Image.FromFile(logoPath);
Point imgPoint = new Point((img.Width - map.Width) / 2, (img.Height - map.Height) / 2);
实习单位鉴定怎么写>五二零图片大全Graphics gg = Graphics.FromImage(img);
gg.DrawImage(map, imgPoint.X, imgPoint.Y, map.Width, map.Height);
//重命名,保存
string fileName = Guid.NewGuid().ToString().Replace("-", "") + "." + ImageFormat.Png;
string filePath = savePath + fileName;
img.Save(filePath);
//释放资源
map.Dispose();
img.Dispose();
g.Dispose();
gg.Dispose();
return filePath;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论