C#利用QrCode.Net生成二维码,下面放文字
项目地址:https://archive.codeplex.com/?p=qrcodenet
QrCode使用方法:
新建项目添加对类库的引用,然后引入Gma.QrCodeNet.Encoding命名空间。
下面直接是实现方式:
/// <summary> /// 创建二维码 /// </summary> /// <param name="content"></param> /// <param name="name"></param> /// <returns></returns> 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 { /// <summary> /// 二维码下面加上文字 /// </summary> /// <param name="qrImg">QR图片</param> /// <param name="content">文字内容</param> /// <param name="n"></param> /// <returns></returns> 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; } }
版权声明:本文为wendus原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。