C# 微信生成二维码

1、下载ThoughtWorks.QRCode.dll 

             zxing.dll  添加引用命名空间

 

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 ThoughtWorks.QRCode.Codec;
using ZXing;

namespace WindowsForm
{
    public partial class WeChatCode : Form
    {
        public WeChatCode()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请输入要生成的二维码!");
                return;
            }
            GetGenerationCode(textBox1.Text);
        }
        /// 
        /// 生成二维码
        /// 
        /// 二维码信息
        /// 图片
        public Bitmap GetGenerationCode(string msg)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//编码问题
            writer.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
            const int codeSizeInPixels = 250;   //设置图片长宽
            writer.Options.Height = writer.Options.Width = codeSizeInPixels;
            writer.Options.Margin = 0;//设置边框
            ZXing.Common.BitMatrix bm = writer.Encode(msg);
            Bitmap img = writer.Write(bm);
            pictureBox1.Image = img;
            return img;
        }
    }
}


控件如图

 

C# 微信生成二维码_第1张图片

测试如下

C# 微信生成二维码_第2张图片

你可能感兴趣的:(WinFom,c#)