C#生成二维码(简易)

 

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

其中引用的动态库:ThoughtWorks.QRCode.dll(重要),分享的debug目录中有,请自己找。

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
    {
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="barcodeText">文本</param>
        /// <returns>图片</returns>
        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);//保存位图 
        }

        /// <summary>
        /// 解析二维码文本
        /// </summary>
        /// <param name="bitmap">图片对象</param>
        /// <returns></returns>
        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;
        }

    }


}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值