3.4 生成带Logo的二维码
二维码带有校验功能,故可以在中间区域展示一定尺寸的图片。
例图:
代码:
// 1.设置QR二维码的规格
ZXing.QrCode.QrCodeEncodingOptions qrEncodeOption = new ZXing.QrCode.QrCodeEncodingOptions();
qrEncodeOption.CharacterSet = "UTF-8"; // 设置编码格式,否则读取'中文'乱码
qrEncodeOption.Height = 200;
qrEncodeOption.Width = 200;
qrEncodeOption.Margin = 1; // 设置周围空白边距
// 2.生成条形码图片
ZXing.BarcodeWriter wr = new BarcodeWriter();
wr.Format = BarcodeFormat.QR_CODE; // 二维码
wr.Options = qrEncodeOption;
Bitmap img = wr.Write(this.ContentTxt.Text);
// 3.在二维码的Bitmap对象上绘制logo图片
Bitmap logoImg = Bitmap.FromFile(System.AppDomain.CurrentDomain.BaseDirectory + "logo.jpg") as Bitmap;
Graphics g = Graphics.FromImage(img);
Rectangle logoRec = new Rectangle(); // 设置logo图片的大小和绘制位置
logoRec.Width = img.Width / 6;
logoRec.Height = img.Height / 6;
logoRec.X = img.Width / 2 - logoRec.Width / 2; // 中心点
logoRec.Y = img.Height / 2 - logoRec.Height / 2;
g.DrawImage(logoImg, logoRec);
// 4.保存绘制后的图片
string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "QR-" + this.ContentTxt.Text + ".jpg";
img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
4. 源码下载
4.1 运行图
4.2 下载地址
百度网盘:http://www.easck.com/s/1qWRJMAo
CSDN:http://www.easck.com/detail/polk6/9383226
注:相关教程知识阅读请移步到c#教程频道。












