C#生成二维码(简易)

C#生成二维码(简易)_第1张图片C#生成二维码(简易)_第2张图片

 

项目源码:链接:https://pan.baidu.com/s/1XsZya-W6J_QZrMmapiI19g 
提取码:9hmi 
 

其中引用的动态库:ThoughtWorks.QRCode.dll(重要),分享的debug目录中有,请自己找。C#生成二维码(简易)_第3张图片

Form1.cs的代码

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;

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

        private void button1_Click(object sender, EventArgs e)
        {
            MyQR d = new MyQR();
            Bitmap b=  d.BarcodeImage(textBox1.Text.ToString());
            MessageBox.Show("ok!");
            pictureBox1.Image = b;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MyQR d = new MyQR();
            MessageBox.Show(d.QRCodeDecoderUtil((Bitmap)this.pictureBox1.Image));
        }
    }
}

MyQR.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using ThoughtWorks.QRCode.Codec;
using System.Drawing;
using ThoughtWorks.QRCode.Codec.Data;

namespace ProductQR
{
    public class MyQR
    {
        /// 
        /// 生成二维码
        /// 
        /// 文本
        /// 图片
        public Bitmap BarcodeImage(String barcodeText)
        {

            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeVersion = 0;
            Bitmap img = qrCodeEncoder.Encode(barcodeText, Encoding.UTF8);//指定utf-8编码, 支持中文
            return img;

            //string httpFileName = System.Environment.CurrentDirectory;
            //string fileName = "/a.jpg";
            //img.Save(httpFileName + fileName);//保存位图 
        }

        /// 
        /// 解析二维码文本
        /// 
        /// 图片对象
        /// 
        public string QRCodeDecoderUtil(Bitmap bitmap)
        {
            QRCodeDecoder decoder = new QRCodeDecoder();
            string decodedString = "";
            try
            {
                decodedString = decoder.decode(new QRCodeBitmapImage(bitmap), Encoding.UTF8);//指定utf-8编码, 支持中文  
            }
            catch (Exception e)
            {
                return "请先生成二维码!";
            }
            return decodedString;
        }

    }


}

 

你可能感兴趣的:(C#)