.NET生成二维码

随着移动设备的快速发展,二维码越来越大众普及,今天给大家介绍一下我的总结:

先来两张效果图(左图不带文字说明,右图带文字说明)

1. 从Nuget上找到这一个程序包(这个程序包兼容.net framework 4.7和.net core,之前引用过其他的程序包,发现并不兼容.net framework 4.7)

2.我重载了一个方法GenerateQRCode,用于生成带文字说明和不带文字说明的二维码。不带文字说明的二维码大小尺寸没有加限制,返回的是原始大小,带文字说明的二维码大小限制为了600*600px(包括文字,可以根据需要自行调整),同时,我将二维码的文字说明放在了二维码的下面(当然了,文字位置也是可以自行调整的,后续有时间我会继续补充)

  1 using QRCoder;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Drawing;
  5 using System.Drawing.Imaging;
  6 using System.IO;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Threading.Tasks;
 10 
 11 namespace App.Common.QRCode
 12 {
 13     /// <summary>
 14     /// 二维码帮助类
 15     /// </summary>
 16     public class QRCodeHelper
 17     {
 18         /// <summary>
 19         /// 生成二维码图片
 20         /// </summary>
 21         /// <param name="str"></param>
 22         /// <returns></returns>
 23         public Bitmap CreateQRimg(string str)
 24         {
 25             QRCodeGenerator qrGenerator = new QRCodeGenerator();
 26             QRCodeData qrCodeData = qrGenerator.CreateQrCode(str, QRCodeGenerator.ECCLevel.Q);
 27             QRCoder.QRCode qrCode = new QRCoder.QRCode(qrCodeData);
 28             Bitmap bt = qrCode.GetGraphic(20);
 29 
 30             return bt;
 31         }
 32 
 33         /// <summary>
 34         /// 生成二维码图片流(二维码上含文字)
 35         /// </summary>
 36         /// <param name="str1">二维码中要传递的数据(地址)</param>
 37         /// <param name="str2">二维码上显示的文字说明</param>
 38         public byte[] GenerateQRCode(string str1, string str2)
 39         {
 40             using (Image codeImage = CreateQRimg(str1), strImage = ConvertStringToImage(str2))
 41             {
 42                 Image img = CombineImage(600, 600, codeImage, 60, 50, strImage, 0, 530);
 43                 using (var stream = new MemoryStream())
 44                 {
 45                     img.Save(stream, ImageFormat.Jpeg);
 46                     //输出图片流
 47                     return stream.ToArray();
 48                 }
 49             }
 50         }
 51 
 52         /// <summary>
 53         /// 生成二维码图片流(不含文字)
 54         /// </summary>
 55         /// <param name="str">二维码中要传递的数据(地址)</param>
 56         /// <returns></returns>
 57         public byte[] GenerateQRCode(string str)
 58         {
 59             using (Image codeImage = CreateQRimg(str))
 60             {
 61                 using (var stream = new MemoryStream())
 62                 {
 63                     codeImage.Save(stream, ImageFormat.Jpeg);
 64 
 65                     return stream.ToArray();
 66                 }
 67             }
 68         }
 69 
 70         /// <summary>
 71         /// 生成文字图片
 72         /// </summary>
 73         /// <param name="str"></param>
 74         /// <returns></returns>
 75         public Image ConvertStringToImage(string str)
 76         {
 77             Bitmap image = new Bitmap(600, 40, PixelFormat.Format24bppRgb);
 78 
 79             Graphics g = Graphics.FromImage(image);
 80 
 81             try
 82             {
 83                 Font font = new Font("SimHei", 14, FontStyle.Regular);
 84 
 85                 g.Clear(Color.White);
 86 
 87                 StringFormat format = new StringFormat();
 88                 format.Alignment = StringAlignment.Center;
 89                 format.LineAlignment = StringAlignment.Center;
 90 
 91                 Rectangle rectangle = new Rectangle(0, 0, 600, 40);
 92 
 93                 g.DrawString(str, font, new SolidBrush(Color.Black), rectangle, format);
 94 
 95                 return image;
 96             }
 97             catch (Exception e)
 98             {
 99                 throw e;
100             }
101             finally
102             {
103                 GC.Collect();
104             }
105         }
106 
107         /// <summary>
108         /// 在画板中合并二维码图片和文字图片
109         /// </summary>
110         /// <param name="width"></param>
111         /// <param name="height"></param>
112         /// <param name="imgLeft"></param>
113         /// <param name="imgLeft_left"></param>
114         /// <param name="imgLeft_top"></param>
115         /// <param name="imgRight"></param>
116         /// <param name="imgRight_left"></param>
117         /// <param name="imgRight_top"></param>
118         /// <returns></returns>
119         public Image CombineImage(int width, int height, Image imgLeft, int imgLeft_left, int imgLeft_top, Image imgRight, int imgRight_left, int imgRight_top)
120         {
121             Bitmap image = new Bitmap(width, height, PixelFormat.Format24bppRgb);
122 
123             Graphics g = Graphics.FromImage(image);
124 
125             try
126             {
127                 g.Clear(Color.White);
128                 g.DrawImage(imgLeft, imgLeft_left, imgLeft_top, 500, 500);
129                 g.DrawImage(imgRight, imgRight_left, imgRight_top, imgRight.Width, imgRight.Height);
130 
131                 return image;
132             }
133             catch (Exception e)
134             {
135                 throw e;
136             }
137             finally
138             {
139                 g.Dispose();
140             }
141         }
142     }
143 }

最后,再补充两个调用方法

 1 /// <summary>
 2     /// Mvc
 3     /// </summary>
 4     public class QRCodeController : Controller
 5     {
 6         /// <summary>
 7         /// 显示二维码
 8         /// </summary>
 9         /// <returns></returns>
10         public ActionResult Index()
11         {
12             var qrCode = new QRCodeHelper();
13             var qrImg = qrCode.GenerateQRCode("https://www.cnblogs.com/wenha");
14             
15             var base64 = "data:image/jpeg;base64," + Convert.ToBase64String(qrImg);
16 
17             ViewBag.CodeImg = base64;
18             return View();
19         }
20     }
MVC模式下调用
 1 /// <summary>
 2     /// WebApi
 3     /// </summary>
 4     public class QRCodeApiController : ApiController
 5     {
 6         /// <summary>
 7         /// 获取二维码图片(base64格式)
 8         /// </summary>
 9         /// <returns></returns>
10         [HttpGet]
11         public HttpResponseMessage GetQRCode()
12         {
13             var qrCode = new QRCodeHelper();
14 
15             var url = "https://www.cnblogs.com/wenha";
16             var image = qrCode.GenerateQRCode(url, "博客园地址");
17 
18             var resp = new HttpResponseMessage(HttpStatusCode.OK)
19             {
20                 Content = new StringContent("data:image/jpeg;base64," + Convert.ToBase64String(image))
21             };
22 
23             return resp;
24         }
25 
26         /// <summary>
27         /// 获取二维码图片(图片格式)
28         /// </summary>
29         /// <returns></returns>
30         [HttpGet]
31         public HttpResponseMessage GetQRCodeImg()
32         {
33 
34             var qrCode = new QRCodeHelper();
35 
36             var url = "https://www.cnblogs.com/wenha";
37             var image = qrCode.GenerateQRCode(url, "博客园地址");
38 
39             var resp = new HttpResponseMessage(HttpStatusCode.OK)
40             {
41                 Content = new ByteArrayContent(image)
42             };
43             resp.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
44             return resp;
45         }
46     }
WebApi模式下调用

 

posted on 2019-08-29 15:44  那么远这么近  阅读(5714)  评论(0编辑  收藏  举报

导航