C#利用QrCode.Net生成二维码,下面放文字
标签:https new res lex net http ati ring href
项目地址:https://archive.codeplex.com/?p=qrcodenet
QrCode使用方法:
新建项目添加对类库的引用,然后引入Gma.QrCodeNet.Encoding命名空间。
下面直接是实现方式:
///
/// 创建二维码
///
///
///
///
public static byte[] CreateBytes(string content, string name)
{
var bytes = qrCoderService.CreateQrCode(content);
var memory = new MemoryStream(bytes);
ImageUtility util = new ImageUtility();
var bitmap = util.InsertWords(new Bitmap(new Bitmap(memory)), name);
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Png);
byte[] data = new byte[stream.Length];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(data, 0, Convert.ToInt32(stream.Length));
return data;
}
}
public class ImageUtility
{
///
/// 二维码下面加上文字
///
/// QR图片
/// 文字内容
///
///
public Bitmap InsertWords(Bitmap qrImg, string content = "")
{
Bitmap backgroudImg = new Bitmap(qrImg.Width, qrImg.Height);
backgroudImg.MakeTransparent();
Graphics g2 = Graphics.FromImage(backgroudImg);
g2.Clear(Color.Transparent);
//画二维码到新的面板上
g2.DrawImage(qrImg, 0, 0);
if (!string.IsNullOrEmpty(content))
{
FontFamily fontFamily = new FontFamily("楷体");
Font font1 = new Font(fontFamily, 20f, FontStyle.Bold, GraphicsUnit.Pixel);
//文字长度
int strWidth = (int)g2.MeasureString(content, font1).Width;
//总长度减去文字长度的一半 (居中显示)
int wordStartX = (qrImg.Width - strWidth) / 2;
int wordStartY = qrImg.Height - 30;
g2.DrawString(content, font1, Brushes.Black, wordStartX, wordStartY);
}
g2.Dispose();
return backgroudImg;
}
}
C#利用QrCode.Net生成二维码,下面放文字
标签:https new res lex net http ati ring href
原文地址:https://www.cnblogs.com/wendus/p/12800326.html
评论