二维码生成 - QrCodeNet

载QrCodeNet

 1 /// <summary>
 2         /// 生成QR码
 3         /// </summary>
 4         /// <param name="output_path">输出路径</param>
 5         /// <param name="text">写入文本</param>
 6         /// <param name="scale">缩放比例</param>
 7         public static void GetQRCode(String output_path, String text, Int32 scale)
 8         {
 9             QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.L);
10             QrCode qrCode = new QrCode();
11             qrEncoder.TryEncode(text, out qrCode);
12 
13             Bitmap img = new Bitmap(qrCode.Matrix.Width * scale, qrCode.Matrix.Height * scale);
14             img.SetResolution(180f, 180f);
15             Graphics gdiobj = Graphics.FromImage(img);
16             gdiobj.CompositingQuality = CompositingQuality.HighQuality;
17             gdiobj.SmoothingMode = SmoothingMode.HighQuality;
18             gdiobj.InterpolationMode = InterpolationMode.HighQualityBicubic;
19             gdiobj.PixelOffsetMode = PixelOffsetMode.HighQuality;
20 
21             for (Int32 i = 0; i < qrCode.Matrix.Height; i++)
22             {
23                 for (Int32 j = 0; j < qrCode.Matrix.Width; j++)
24                 {
25                     if (qrCode.Matrix.InternalArray[i, j])
26                     {
27                         gdiobj.FillRectangle(new SolidBrush(Color.Black), i * scale, j * scale, scale, scale);
28                     }
29                 }
30             }
31 
32             System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
33             ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 800L);
34 
35             System.Drawing.Imaging.ImageCodecInfo ici = GetEncoder(ImageFormat.Png);
36 
37             if (ici != null)
38             {
39                 img.Save(output_path, ici, ep);
40             }
41             else
42             {
43                 img.Save(output_path, ImageFormat.Png);
44             }
45             img.Dispose();
46             gdiobj.Dispose();
47         }
View Code

 比例的缩放没写好

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。