C# 微信生成二维码

来源:互联网 发布:mac vim保存退出命令 编辑:程序博客网 时间:2024/05/16 10:12

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);        }        /// <summary>        /// 生成二维码        /// </summary>        /// <param name="msg">二维码信息</param>        /// <returns>图片</returns>        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;        }    }}

控件如图


测试如下


原创粉丝点击