QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

泡泡马甲APP 更多内容请下载泡泡马甲手机客户端APP 立即下载 ×
查看: 3286|回复: 0

[C#] C# 代码生成二维码方法及代码示例(QRCoder)

[复制链接]

等级头衔

积分成就    金币 : 2806
   泡泡 : 1516
   精华 : 6
   在线时间 : 1244 小时
   最后登录 : 2024-5-5

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2021-10-14 10:17:36 | 显示全部楼层 |阅读模式
一、背景
4 z/ o  e4 g( Q; b0 d4 D       二维码是越来越流行了,很多地方都有可能是使用到。如果是静态的二维码还是比较好处理的,通过在线工具就可以直接生成一张二维码图片,比如:草料二维码。但有的时候是需要动态生成的(根据动态数据生成),这个使用在线就工具就无法实现了。最好是能在代码中直接生成一个二维码图片,这里我就介绍下使用QRCoder类库在代码中生成二维码。
! I4 T! v9 s: b; z+ S$ Y       网上生成二维码的组件还是挺多的,但是真正好用且快速的却不多。QRCoder就是我在众多中找到的,它的生成速度快、而且使用也相当方便。& z# p3 @6 |  M) H0 ]
二、开始编码
6 Q( q  H  G" I% p) C) R1、安装 QRCoder组件。在项目上通过NuGet包管理器来安装,搜索名称:QRCoder% F3 ]# o7 t3 n6 }! S
2、在代码中添加引用:using QRCoder;
3 W/ q3 ^& U* ~# e3、编码生成
; B  [- v: S# S- \$ s$ s
  1.   private void RenderQrCode()
  2.         {
  3.             string level = comboBoxECC.SelectedItem.ToString();
  4.             QRCodeGenerator.ECCLevel eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3);
  5.             using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
  6.             {
  7.                 using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(textBoxQRCode.Text, eccLevel))
  8.                 {
  9.                     using (QRCode qrCode = new QRCode(qrCodeData))
  10.                     {
  11.                         pictureBoxQRCode.BackgroundImage = qrCode.GetGraphic(20, Color.Black, Color.White,
  12.                             GetIconBitmap(), (int) iconSize.Value);
  13.                          this.pictureBoxQRCode.Size = new System.Drawing.Size(pictureBoxQRCode.Width, pictureBoxQRCode.Height);
  14.                         //Set the SizeMode to center the image.
  15.                         this.pictureBoxQRCode.SizeMode = PictureBoxSizeMode.CenterImage;
  16.                         pictureBoxQRCode.SizeMode = PictureBoxSizeMode.StretchImage;
  17.                     }
  18.                 }
  19.             }
  20.         }
1.jpg

0 T! e: a& C( K/ v三、加个Logo吧; I0 L9 l% S% B/ t3 Z8 \# M1 e
还可以加上logo
1 H. B4 z0 Y# H) f' C
7 b+ T3 \$ ]+ S, F( y7 Y
  1.   private Bitmap GetIconBitmap()
  2.         {
  3.             Bitmap img = null;
  4.             if (iconPath.Text.Length > 0)
  5.             {
  6.                 try
  7.                 {
  8.                     img = new Bitmap(iconPath.Text);
  9.                 }
  10.                 catch (Exception)
  11.                 {
  12.                 }
  13.             }
  14.             return img;
  15.         }
2.jpg

/ |, a! W) p6 U# f0 I" {! s五、完整代码( E0 N1 C8 Y$ E* V% d! S
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using QRCoder;
  10. using System.Drawing.Imaging;
  11. using System.IO;
  12. namespace QRCoderDemo
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.             comboBoxECC.SelectedIndex = 0; //Pre-select ECC level "L"
  23.             RenderQrCode();
  24.         }
  25.         private void buttonGenerate_Click(object sender, EventArgs e)
  26.         {
  27.             RenderQrCode();
  28.         }
  29.         private void RenderQrCode()
  30.         {
  31.             string level = comboBoxECC.SelectedItem.ToString();
  32.             QRCodeGenerator.ECCLevel eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3);
  33.             using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
  34.             {
  35.                 using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(textBoxQRCode.Text, eccLevel))
  36.                 {
  37.                     using (QRCode qrCode = new QRCode(qrCodeData))
  38.                     {
  39.                         pictureBoxQRCode.BackgroundImage = qrCode.GetGraphic(20, Color.Black, Color.White,
  40.                             GetIconBitmap(), (int) iconSize.Value);
  41.                          this.pictureBoxQRCode.Size = new System.Drawing.Size(pictureBoxQRCode.Width, pictureBoxQRCode.Height);
  42.                         //Set the SizeMode to center the image.
  43.                         this.pictureBoxQRCode.SizeMode = PictureBoxSizeMode.CenterImage;
  44.                         pictureBoxQRCode.SizeMode = PictureBoxSizeMode.StretchImage;
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.         private Bitmap GetIconBitmap()
  50.         {
  51.             Bitmap img = null;
  52.             if (iconPath.Text.Length > 0)
  53.             {
  54.                 try
  55.                 {
  56.                     img = new Bitmap(iconPath.Text);
  57.                 }
  58.                 catch (Exception)
  59.                 {
  60.                 }
  61.             }
  62.             return img;
  63.         }
  64.         private void selectIconBtn_Click(object sender, EventArgs e)
  65.         {
  66.             OpenFileDialog openFileDlg = new OpenFileDialog();
  67.             openFileDlg.Title = "Select icon";
  68.             openFileDlg.Multiselect = false;
  69.             openFileDlg.CheckFileExists = true;
  70.             if (openFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  71.             {
  72.                 iconPath.Text = openFileDlg.FileName;
  73.                 if (iconSize.Value == 0)
  74.                 {
  75.                     iconSize.Value = 15;
  76.                 }
  77.             }
  78.             else
  79.             {
  80.                 iconPath.Text = "";
  81.             }
  82.         }
  83.         private void btn_save_Click(object sender, EventArgs e)
  84.         {
  85.             // Displays a SaveFileDialog so the user can save the Image
  86.             SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  87.             saveFileDialog1.Filter = "Bitmap Image|*.bmp|PNG Image|*.png|JPeg Image|*.jpg|Gif Image|*.gif";
  88.             saveFileDialog1.Title = "Save an Image File";
  89.             saveFileDialog1.ShowDialog();
  90.             // If the file name is not an empty string open it for saving.
  91.             if (saveFileDialog1.FileName != "")
  92.             {
  93.                 // Saves the Image via a FileStream created by the OpenFile method.
  94.                 using (FileStream fs = (System.IO.FileStream) saveFileDialog1.OpenFile())
  95.                 {
  96.                     // Saves the Image in the appropriate ImageFormat based upon the
  97.                     // File type selected in the dialog box.
  98.                     // NOTE that the FilterIndex property is one-based.
  99.                     ImageFormat imageFormat = null;
  100.                     switch (saveFileDialog1.FilterIndex)
  101.                     {
  102.                         case 1:
  103.                             imageFormat = ImageFormat.Bmp;
  104.                             break;
  105.                         case 2:
  106.                             imageFormat = ImageFormat.Png;
  107.                             break;
  108.                         case 3:
  109.                             imageFormat = ImageFormat.Jpeg;
  110.                             break;
  111.                         case 4:
  112.                             imageFormat = ImageFormat.Gif;
  113.                             break;
  114.                         default:
  115.                             throw new NotSupportedException("File extension is not supported");
  116.                     }
  117.                     pictureBoxQRCode.BackgroundImage.Save(fs, imageFormat);
  118.                     fs.Close();
  119.                 }
  120.             }
  121.         }
  122.         public void ExportToBmp(string path)
  123.         {
  124.         }
  125.         private void textBoxQRCode_TextChanged(object sender, EventArgs e)
  126.         {
  127.             RenderQrCode();
  128.         }
  129.         private void comboBoxECC_SelectedIndexChanged(object sender, EventArgs e)
  130.         {
  131.             RenderQrCode();
  132.         }
  133.     }
  134. }
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-5-5 17:22

Powered by paopaomj X3.4 © 2016-2024 sitemap

快速回复 返回顶部 返回列表