前言

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

但有的时候是需要动态生成的(根据动态数据生成),这个使用在线就工具就无法实现了。最好是能在代码中直接生成一个二维码图片,介绍下使用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# 生成二维码方法(QRCoder)的更多相关文章

  1. php+qrcode类+生成二维码方法

    //生成二维码 public function qrcode() { $data = input(); if(!$data['param']){ return json(['code ' => ...

  2. PHP生成二维码方法

    <?php //先下载一份phpqrcode类,下载地址http://down.51cto.com/data/780947require_once("phpqrcode/phpqrco ...

  3. C#通过第三方组件生成二维码(QR Code)和条形码(Bar Code)

    用C#如何生成二维码,我们可以通过现有的第三方dll直接来实现,下面列出几种不同的生成方法: 1):通过QrCodeNet(Gma.QrCodeNet.Encoding.dll)来实现 1.1):首先 ...

  4. zxing生成二维码和条码

    /*** * 生成二维码方法 * @param str 生成内容 * @param widthHeight 宽度和高度 * @return * @throws WriterException */ p ...

  5. C# 根据字符串生成二维码

    1.先下载NuGet包(ZXing.Net) 2.新建控制器及编写后台代码 using System; using System.Collections.Generic; using System.D ...

  6. C# 生成二维码(QR Code)

    参考:   C#通过ThoughtWorks.QRCode生成二维码(QR Code)   通过ThoughtWorks.QRCode(ThoughtWorks.QRCode.dll)来实现 1)   ...

  7. 基于Asp.Net Core,利用ZXing来生成二维码的一般流程

    本文主要介绍如何在.net环境下,基于Asp.Net Core,利用ZXing来生成二维码的一般操作.对二维码工作原理了解,详情见:https://blog.csdn.net/weixin_36191 ...

  8. php(tp5) 生成二维码

    phpqrcode类库官网下载地址:https://sourceforge.net/projects/phpqrcode/ 1.我们先看看php是怎么生成二维码的 1.首先我们先下载一下  phpqr ...

  9. php 使用phpqrcode生成二维码并上传到OSS

    一般情况调用phpqrcode第三方插件 会把生成的二维码图片保存到服务器,不保存服务器也会以header头的形式输出到浏览器,(我们不允许把图片文件保存的liunx服务器,只能保存到阿里云OSS存储 ...

  10. 4种方法生成二维码 (js 控制canvas 画出 二维码)

    随着网络的迅速发展 发展 发展,二维码的应用将会越来越多.同时很多只是很平凡的二维码,请拿起你的手 把这个二维码 设计起来吧.下面分享了几个非常好的二维码设计.  二维码原理: 二维条码/二维码可以分 ...

随机推荐

  1. 12月23日内容总结——csrf跨站请求伪造、校验策略、相关装饰器,auth认证模块及相关操作,拓展auth_user表

    目录 一.csrf跨站请求伪造 概念引入 概念讲解 二.csrf校验策略 概念讲解 form表单操作csrf策略 ajax请求csrf策略 三.csrf相关装饰器 四.auth认证模块 五.auth认 ...

  2. (一) MdbCluster分布式内存数据库——基础架构介绍

    (一) MdbCluster分布式内存数据库--基础架构介绍   这个项目是怎么开始的我已经有些记不清楚了,大概是原来的内存数据库很不好用,一次次地让我们踩坑,我又自以为是地觉得可以做一个更好的出来. ...

  3. html 引入 js 代码的几种方式

    一.普通加载 <script src="./abc.js"></script> 二.内嵌 <script> JavaScript 代码 < ...

  4. WPF跨平台方案?

    Avalonia XPF 通过我们的跨平台UI框架,释放现有WPF应用程序的全部潜力,使WPF应用程序能够在macOS和Linux上运行,而不需要昂贵和有风险的重写. 工作原理 我们使用 Fork o ...

  5. 软件教程 | Jupyter&stata之stata_kernel攻略

    ![](http://mdimg.yxj1010.top/xlbxs_ydt2.png) 目录: 目录 一.什么是stata_kernel 1. stata_kernel简介 2. 为什么要使用sta ...

  6. 爬取精美壁纸5w张,爱了爱了

    近日接到一个需求--爬取某应用商店所有在线销售壁纸,这个任务起初让我惊呆了.因为上级没有给我解决风控问题,若爬取在售资源被人家厂商追责怎么办?若造成人家服务器出问题怎么办?问的时候上级含糊其辞,唉!其 ...

  7. JZOJ 3737. 【NOI2014模拟7.11】挖宝藏

    \(\text{Solution}\) 当 \(h=1\) 时显然是斯坦纳树板子,最方案必然是树形的 \(h > 1\) 时,考虑在每一层新建一个状态表示上一层宝藏全部挖完到这层某个点的答案 同 ...

  8. 2020.11.30【NOIP提高A组】模拟

    总结与反思 很不幸,估分 \(170\),可惜 \(T2\) 暴力 \(50pts\) 全掉了 \(T1\) 结论题,如果想到了,\(O(n)\) 过,只有十几行代码 感觉不好想,不过还是 \(A\) ...

  9. 题解 [ZJOI2007]棋盘制作

    把悬线法这个坑填了,还是很简单的 qwq. 悬线法一般解决有一定约束条件的最大矩形问题.悬线的定义是从一个点一直往上走直到边界或者不符合条件结束. 炒个例子,在这题里面比如说有这样一个矩形 0 1 0 ...

  10. 微信小程序与微信公众号之间支付问题解决方案

    前言 大家好,我是一名对编程有兴趣的小伙子,IT届称我为xiager,工作中叫我jake 就好了,如果此文对你有帮助希望多多关注哦. 准备 微信公众平台 微信支付平台 微信开放平台 一. 小程序    ...