首页 > 编程语言 >C# 生成二维码方法(QRCoder)

C# 生成二维码方法(QRCoder)

时间:2022-11-23 12:44:58浏览次数:49  
标签:C# void System private pictureBoxQRCode 二维码 new using QRCoder

前言

二维码很多地方都有使用到。如果是静态的二维码还是比较好处理的,通过在线工具就可以直接生成一张二维码图片,比如:草料二维码。

但有的时候是需要动态生成的(根据动态数据生成),这个使用在线就工具就无法实现了。最好是能在代码中直接生成一个二维码图片,介绍下使用QRCoder类库在代码中生成二维码。

网上生成二维码的组件还是挺多的,但是真正好用且快速的却不多。QRCoder就是我在众多中找到的,它的生成速度快、而且使用也相当方便。

开始编码

1、安装 QRCoder组件。在项目上通过NuGet包管理器来安装,搜索名称:QRCoder

2、在代码中添加引用:using QRCoder;

3、编码生成

private void RenderQrCode()
{
string level = "Q";// comboBoxECC.SelectedItem.ToString();
QRCodeGenerator.ECCLevel eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3);
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
{
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode("9f3274ec-1481-4988-a3c8-698bbafbf14b", eccLevel))
{
using (QRCode qrCode = new QRCode(qrCodeData))
{

pictureBoxQRCode.BackgroundImage = qrCode.GetGraphic(20, Color.Black, Color.White,
null, 50);

this.pictureBoxQRCode.Size = new System.Drawing.Size(pictureBoxQRCode.Width, pictureBoxQRCode.Height);
//Set the SizeMode to center the image.
this.pictureBoxQRCode.SizeMode = PictureBoxSizeMode.CenterImage;

pictureBoxQRCode.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
}
}

 

上面代码运行的结果

 

 

还可以加上logo

private Bitmap GetIconBitmap()
{
    Bitmap img = null;
    if (iconPath.Text.Length > 0)
    {
        try
        {
            img = new Bitmap(iconPath.Text);
        }
        catch (Exception)
        {
        }
    }
    return img;
}

完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QRCoder;
using System.Drawing.Imaging;
using System.IO;

namespace QRCoderDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxECC.SelectedIndex = 0; //Pre-select ECC level "L"
            RenderQrCode();
        }

        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            RenderQrCode();
        }

        private void RenderQrCode()
        {
            string level = comboBoxECC.SelectedItem.ToString();
            QRCodeGenerator.ECCLevel eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3);
            using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
            {
                using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(textBoxQRCode.Text, eccLevel))
                {
                    using (QRCode qrCode = new QRCode(qrCodeData))
                    {

                        pictureBoxQRCode.BackgroundImage = qrCode.GetGraphic(20, Color.Black, Color.White,
                            GetIconBitmap(), (int) iconSize.Value);

                         this.pictureBoxQRCode.Size = new System.Drawing.Size(pictureBoxQRCode.Width, pictureBoxQRCode.Height);
                        //Set the SizeMode to center the image.
                        this.pictureBoxQRCode.SizeMode = PictureBoxSizeMode.CenterImage;

                        pictureBoxQRCode.SizeMode = PictureBoxSizeMode.StretchImage;
                    }
                }
            }
        }

        private Bitmap GetIconBitmap()
        {
            Bitmap img = null;
            if (iconPath.Text.Length > 0)
            {
                try
                {
                    img = new Bitmap(iconPath.Text);
                }
                catch (Exception)
                {
                }
            }
            return img;
        }

        private void selectIconBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Title = "Select icon";
            openFileDlg.Multiselect = false;
            openFileDlg.CheckFileExists = true;
            if (openFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                iconPath.Text = openFileDlg.FileName;
                if (iconSize.Value == 0)
                {
                    iconSize.Value = 15;
                }
            }
            else
            {
                iconPath.Text = "";
            }
        }


        private void btn_save_Click(object sender, EventArgs e)
        {

            // Displays a SaveFileDialog so the user can save the Image
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Bitmap Image|*.bmp|PNG Image|*.png|JPeg Image|*.jpg|Gif Image|*.gif";
            saveFileDialog1.Title = "Save an Image File";
            saveFileDialog1.ShowDialog();

            // If the file name is not an empty string open it for saving.
            if (saveFileDialog1.FileName != "")
            {
                // Saves the Image via a FileStream created by the OpenFile method.
                using (FileStream fs = (System.IO.FileStream) saveFileDialog1.OpenFile())
                {
                    // Saves the Image in the appropriate ImageFormat based upon the
                    // File type selected in the dialog box.
                    // NOTE that the FilterIndex property is one-based.

                    ImageFormat imageFormat = null;
                    switch (saveFileDialog1.FilterIndex)
                    {
                        case 1:
                            imageFormat = ImageFormat.Bmp;
                            break;
                        case 2:
                            imageFormat = ImageFormat.Png;
                            break;
                        case 3:
                            imageFormat = ImageFormat.Jpeg;
                            break;
                        case 4:
                            imageFormat = ImageFormat.Gif;
                            break;
                        default:
                            throw new NotSupportedException("File extension is not supported");
                    }

                    pictureBoxQRCode.BackgroundImage.Save(fs, imageFormat);
                    fs.Close();
                }
            }





        }

        public void ExportToBmp(string path)
        {

        }

        private void textBoxQRCode_TextChanged(object sender, EventArgs e)
        {
            RenderQrCode();
        }

        private void comboBoxECC_SelectedIndexChanged(object sender, EventArgs e)
        {
            RenderQrCode();
        }
    }
}

 

标签:C#,void,System,private,pictureBoxQRCode,二维码,new,using,QRCoder
From: https://www.cnblogs.com/yakniu/p/16917897.html

相关文章

  • 深入理解css 笔记(9)
    模块化CSS是指把页面分割成不同的组成部分,这些组成部分可以在多种上下文中重复使用,并且互相之间没有依赖关系。最终目的是,当我们修改其中一部分css时,不会对其他部分产......
  • linux免密ssh-copy-id命令:指定和非指定远程端口两种情况
    https://blog.csdn.net/weixin_42025270/article/details/125721971一、命令介绍ssh-copy-id命令可以把本地主机的公钥复制到远程主机的authorized_keys文件上,ssh-copy-......
  • 3.2 Docker最新入门教程-Docker入门-将应用程序容器化
    3.2将应用程序容器化对于本指南的其余部分,您将使用一个在Node.js中运行的简单待办事项列表管理器。如果您不熟悉Node.js,请不要担心。本指南不需要JavaScript经验。......
  • 3.1 Docker最新入门教程-Docker入门-概述
    3.1概述欢迎!我们很高兴您想学习Docker。本指南包含有关如何开始使用Docker的分步说明。您将在本指南中学到和做的一些事情是:构建并运行镜像作为容器使用DockerHub共......
  • ASP.NET Core教程-Logging(日志)
    更新记录转载请注明出处:2022年11月23日发布。2022年11月20日从笔记迁移到博客。日志(Logging)基础日志说明日志并不会为应用程序增加实质性的功能,常用于记录错误信......
  • el-popconfirm不显示的问题
    今天想做个表格删除列的确认框,想写个这种(我这菜鸡直接拿的elementUI,不是自己写的)然后呢,我用el-popconfirm包裹了一个按钮,按钮不显示了,我就问了一嘴我朋友是怎么回事。(没......
  • The 2021 CCPC Guilin Onsite (XXII Open Cup, Grand Prix of EDG
    https://codeforces.com/gym/103409/problem/BB.APlusBProblem—————数据结构(set)题意给你两个n位的数a,b(有前导零),c是a+b的结果(最高位的进位已省略)q次询......
  • Docker Registry部署+基本使用
    目录1、容器部署2、部署后操作3、上传镜像4、查看镜像官网部署文档1、容器部署docker单节点:mkdir-p/root/container/registrydockerrun-itd-p5000:5000--res......
  • 3.1 Docker最新入门教程-Docker入门-概述
    3.1概述欢迎!我们很高兴您想学习Docker。本指南包含有关如何开始使用Docker的分步说明。您将在本指南中学到和做的一些事情是:构建并运行镜像作为容器使用DockerH......
  • SQL Server数据类型转换函数cast()和convert()详解
    https://blog.csdn.net/m0_67401382/article/details/126117592常用的函数有cast()和convert()。cast()和convert()函数比较:(1)cast一般更容易使用,convert的优点是可以格......