search
HomeBackend DevelopmentC#.Net TutorialThe latest solution for generating QR codes using C#, detailed explanation and examples (QRCoder)

I don’t know if anyone has made multiple QR codes? In this article, I will introduce to you how to use C# to generate QR codes. First, I will talk about the three classes QRCodeGenerator, QRCodeData, and QRCode required to generate QR codes. For easy understanding later. What are their responsibilities? QRCodeGenerator: Used to generate the data object stored in the QR code through the specified method, which is the Matrix in the middle of the QR code in QRCodeData. Then QRCode gets the QRCodeData and generates the QR code

QR code

1. Preface

I have been working on some things related to QR codes recently, so I came into contact with some QR codes, so since I have used them, I must have used them.

In fact, about There are really countless articles about QR codes, and many of them are written very seriously and are very good, but it is just like learning. Just because others know it does not mean that you have no value in learning and recording, so learning does not come sooner or later

Introducing the package

1.Introduce QRCoder through NuGet

a) First, we create a new Class Library project, named here chestnut_qrcode

b) Then introduce the QRCoder package through NuGet

c) See the picture for operation

The latest solution for generating QR codes using C#, detailed explanation and examples (QRCoder)

The latest solution for generating QR codes using C#, detailed explanation and examples (QRCoder)

d) After the installation is successful, the reference to QRCoder will appear in the project reference

e) At this time, the introduction work has been completed, but it can be easily Create a Encoder.cs static public class

QR code generation class

1. Preparation

Let’s talk about it first Let’s take a look at the three classes needed to generate QR codes:

  • QRCodeGenerator

  • ##QRCodeData

  • QRCode

What are their

responsibilities?

QRCodeGenerator: Used to generate the data object stored in the QR code through the specified method, that is, QRCodeData Matrix in the middle of the QR code , then QRCode gets QRCodeData and generates QR code

2. Encoding

Encoder.cs All codes are as follows:

using System.Drawing;

namespace chestnut_qrcode
{
    /// <summary>
    /// 二维码编码器
    /// </summary>
    public static class Encoder
    {
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="msg">信息</param>
        /// <param name="version">版本 1 ~ 40</param>
        /// <param name="pixel">像素点大小</param>
        /// <param name="icon_path">图标路径</param>
        /// <param name="icon_size">图标尺寸</param>
        /// <param name="icon_border">图标边框厚度</param>
        /// <param name="white_edge">二维码白边</param>
        /// <returns>位图</returns>
        public static Bitmap code(string msg, int version, int pixel, string icon_path, int icon_size, int icon_border, bool white_edge)
        {

            QRCoder.QRCodeGenerator code_generator = new QRCoder.QRCodeGenerator();

            QRCoder.QRCodeData code_data = code_generator.CreateQrCode(msg, QRCoder.QRCodeGenerator.ECCLevel.M/* 这里设置容错率的一个级别 */, true, true, QRCoder.QRCodeGenerator.EciMode.Utf8, version);

            QRCoder.QRCode code = new QRCoder.QRCode(code_data);

            Bitmap icon = new Bitmap(icon_path);

            Bitmap bmp = code.GetGraphic(pixel, Color.Black, Color.White, icon, icon_size, icon_border, white_edge);

            return bmp;

        }
    }
}

The parameters between fault tolerance and version are related to the encoding format. Some formats do not support Chinese.

Prepare the Form

1. Appearance

Seaconch here uses the

winform project, just take a screenshot

The latest solution for generating QR codes using C#, detailed explanation and examples (QRCoder)

2. Code

using System;
using System.Drawing;
using System.Windows.Forms;

namespace chestnut_form
{
    public partial class frm_qrcode : Form
    {
        public frm_qrcode()
        {
            InitializeComponent();
        }

        // 窗体加载
        private void frm_qrcode_Load(object sender, EventArgs e)
        {
            cb_version.SelectedIndex = 1;

            cb_pixel.SelectedIndex = 0;

            cb_icon_size.SelectedIndex = 0;

            cb_icon_border.SelectedIndex = 1;
        }

        // 编码
        private void btn_encode_Click(object sender, EventArgs e)
        {
            int version = Convert.ToInt16(cb_version.Text);

            int pixel = Convert.ToInt16(cb_pixel.Text);

            string str_msg = tb_msg.Text;

            int int_icon_size = Convert.ToInt16(cb_icon_size.Text);

            int int_icon_border = Convert.ToInt16(cb_icon_border.Text);

            bool b_we = rb_we_y.Checked ? true : false;

            Bitmap bmp = chestnut_qrcode.Encoder.code(str_msg, version, pixel, "E:/seaconch/git/1.jpg", int_icon_size, int_icon_border, b_we);

            pb_qrcode.Image = bmp;
        }

        // 保存
        private void btn_save_Click(object sender, EventArgs e)
        {
            if (pb_qrcode.Image != null)

                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    sfd.Filter = "(*.png)|*.png|(*.bmp)|*.bmp";

                    if (sfd.ShowDialog() == DialogResult.OK) pb_qrcode.Image.Save(sfd.FileName);

                }
        }
    }
}

Look at the effect of C# QR code generation

The latest solution for generating QR codes using C#, detailed explanation and examples (QRCoder)

Related articles:

[c# tutorial] C# data Type

Related videos:

Geek Academy C# video tutorial

The above is the detailed content of The latest solution for generating QR codes using C#, detailed explanation and examples (QRCoder). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何使用 PHP 实现动态生成二维码功能如何使用 PHP 实现动态生成二维码功能Sep 05, 2023 pm 05:45 PM

如何使用PHP实现动态生成二维码功能二维码(QRCode)被广泛应用于各个领域,它可以存储大量信息且易于扫描。在网页应用中,我们经常需要动态生成二维码,以便为用户提供便捷的操作方式。本文将介绍如何使用PHP实现动态生成二维码的功能。一、安装和配置PHPQRCode库为了方便生成二维码,我们可以使用PHPQRCode库。首先,我们需要

如何在 iPhone 上扫描二维码如何在 iPhone 上扫描二维码May 04, 2023 am 11:46 AM

先决条件:在您的iPhone上启用二维码扫描默认情况下,所有运行iOS11的iPhone都启用了扫描QR码的功能。因此,您需要确保您的iPhone已更新到最新的可用版本,至少iOS11才能能够原生扫描QR码。在继续执行以下任何方法之前,您必须确保在iPhone上启用了该功能。您可以通过打开“设置”应用并点击“相机”部分在iPhone上启用QR码扫描。在下一个屏幕上,启用“扫描QR码”切换。这应该会打开该功能,以便您可以使用以下任何方法扫描并从QR码中提取

如何使用PHP生成批量的二维码?如何使用PHP生成批量的二维码?Aug 25, 2023 pm 04:33 PM

如何使用PHP生成批量的二维码?随着互联网技术的不断发展,二维码已经成为了一种非常普遍的信息传递工具。二维码可以存储大量的信息,并且可以快速扫描识别,因此在各行各业中得到了广泛的应用。在很多情况下,我们需要批量生成大量的二维码,比如用于商品标签、活动门票等。PHP是一种广泛应用于web开发的脚本语言,具有灵活、简单易用的特点。下面,我们将介绍如何使用PHP生

如何使用PHP生成带有时间限制的二维码?如何使用PHP生成带有时间限制的二维码?Aug 26, 2023 pm 04:34 PM

如何使用PHP生成带有时间限制的二维码?随着移动支付和电子门票的普及,二维码成为了一种常见的技术。在很多场景中,我们可能需要生成一种带有时间限制的二维码,即使在一定时间后,该二维码也将失效。本文将介绍如何使用PHP生成带有时间限制的二维码,并提供代码示例供参考。安装PHPQRCode库要使用PHP生成二维码,我们需要先安装PHPQRCode库。这个库

如何通过PHP编写一个简单的二维码生成器如何通过PHP编写一个简单的二维码生成器Sep 24, 2023 am 08:49 AM

如何通过PHP编写一个简单的二维码生成器二维码在现代社会中已经变得非常常见,它能够快速传递信息,提升用户体验。在本文中,我将向大家介绍如何使用PHP编写一个简单的二维码生成器。一、安装必要的工具和库在开始之前,我们需要确保已经安装以下工具和库:PHP:确保已经安装了PHP的最新版本,可以通过运行php-v命令来查看当前PHP的版本。Composer:C

如何使用PHP开发公众号的二维码生成功能如何使用PHP开发公众号的二维码生成功能Sep 19, 2023 am 10:03 AM

如何使用PHP开发公众号的二维码生成功能当今社交媒体的盛行使得公众号成为企业与用户互动的重要渠道之一。为了吸引更多用户关注公众号,企业常常会使用二维码来方便用户扫码关注。本文将介绍如何使用PHP开发公众号的二维码生成功能,并提供具体的代码示例。获取二维码生成地址在使用PHP开发公众号的二维码生成功能之前,我们首先需要获取二维码生成地址。可以通过微信公众平台提

使用Slim框架中间件实现二维码生成和扫描的功能使用Slim框架中间件实现二维码生成和扫描的功能Jul 28, 2023 pm 05:33 PM

使用Slim框架中间件实现二维码生成和扫描的功能简介:在现代社会,二维码已经成为广泛应用的一种信息传递方式。许多应用程序和网站都提供了二维码的生成和扫描功能。本文将介绍如何使用Slim框架的中间件来实现二维码的生成和扫描功能。安装Slim框架:首先,我们需要安装Slim框架。在终端中执行以下命令:composerrequireslim/slim生成二维码

如何使用Vue实现二维码生成如何使用Vue实现二维码生成Nov 07, 2023 am 09:57 AM

二维码是现代社会中广泛使用的一种信息编码方式,Vue是一款前端框架,如何使用Vue实现二维码生成呢?一、了解二维码生成的原理二维码的生成原理是将一段文本或一段URL地址转换成一张图片,在这张图片中编码了文本或URL地址的信息。二维码生成可以使用第三方库,本文介绍如何使用Qrcode.js库来生成二维码。Qrcode.js是一款轻量级、无依赖的二维码生成库。二

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft