利用ZXing.Net来生成二维码(C#)

抄袭自:https://www.cnblogs.com/hsiang/p/8452984.html

 

步骤:

1 利用NuGet下载ZXing.Net,

地址:https://www.nuget.org/packages/ZXing.Net/0.16.4

利用NuGet下载ZXing.Net可以参考:

NuGet学习笔记(1)——初识NuGet及快速安装使用:https://kb.cnblogs.com/page/143190/
VS使用Nuget教程详解 Visual studio 安装第三方的组件库:https://www.cnblogs.com/dathlin/p/7705014.html
VS2013中Nuget程序包管理器控制台使用入门:http://www.cnblogs.com/wangqiideal/p/4672329.html

 

我下载的版本是0.10.0,不行的话就多试几个版本,如下图:

利用ZXing.Net来生成二维码(C#)
 

2  编写代码:

winform主界面如下图,因为只是测试嘛,所有就弄得简单点。

利用ZXing.Net来生成二维码(C#)

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;                   //添加的命名空间
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;                       //添加的命名空间
using ZXing.QrCode;                //添加的命名空间

namespace 生成二维码
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txtContent.Text)){
                MessageBox.Show("请输入文本");
                return;
            }

            Bitmap bitmap = CreateQRCode(txtContent.Text, 800, 600);
            if(bitmap==null){
                MessageBox.Show("二维码生成失败");
            }
            if (WriteBitmapToPng(bitmap))
            {
                MessageBox.Show("二维码生成成功");
            }
            else {
                MessageBox.Show("二维码生成失败");
            }
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="msg">嵌入二维码中的信息</param>
        /// <param name="width">要生成的二维码图片的宽度</param>
        /// <param name="height">要生成的二维码图片的高度</param>
        /// <returns>返回图片二维码</returns>
        private Bitmap CreateQRCode(string msg,int width,int height) {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            QrCodeEncodingOptions options = new QrCodeEncodingOptions()
            {
                CharacterSet="UTF-8",
                Width=width,
                Height=height,
                Margin=1,
               
               
            };
            writer.Options = options;
            Bitmap map = writer.Write(msg);
            return map;
        }

        /// <summary>
        /// 把Bitmap保存为png图片输出,输出到bin/Debug目录下
        /// </summary>
        /// <param name="bitmap">输入的bitmap</param>
        /// <returns>是否保存成功</returns>
        private bool WriteBitmapToPng(Bitmap bitmap) {
            if(bitmap==null){
                MessageBox.Show("输入的Bitmap无效");
                return false;
            }
            try
            {
                if(!File.Exists("test.png")){
                    File.Create("test.png");
                }
                bitmap.Save("test.png", ImageFormat.Png);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
           
        }
    }
}

3  运行

利用ZXing.Net来生成二维码(C#)

4 到Debug目录打开test.png如下图:

利用ZXing.Net来生成二维码(C#)

好了,就可以打开你的微信进行扫一扫了。