C# winform Qrcoder二维码

首先安装这个Qrcoder,nuget中搜Qrcoder,然后直接点击安装

界面设计

namespace QRCoder
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.pb_qrcode = new System.Windows.Forms.PictureBox();
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.tb_msg = new System.Windows.Forms.TextBox();
            this.cb_version = new System.Windows.Forms.ComboBox();
            this.cb_icon_border = new System.Windows.Forms.ComboBox();
            this.cb_pixel = new System.Windows.Forms.ComboBox();
            this.cb_icon_size = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.rb_we_y = new System.Windows.Forms.RadioButton();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.button2 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.pb_qrcode)).BeginInit();
            this.SuspendLayout();
            // 
            // pb_qrcode
            // 
            this.pb_qrcode.Location = new System.Drawing.Point(418, 12);
            this.pb_qrcode.Name = "pb_qrcode";
            this.pb_qrcode.Size = new System.Drawing.Size(359, 291);
            this.pb_qrcode.TabIndex = 0;
            this.pb_qrcode.TabStop = false;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(85, 443);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(139, 32);
            this.button1.TabIndex = 1;
            this.button1.Text = "生成二维码法1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(31, 400);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(113, 12);
            this.label1.TabIndex = 2;
            this.label1.Text = "二维码包含的数据:";
            // 
            // tb_msg
            // 
            this.tb_msg.Location = new System.Drawing.Point(151, 400);
            this.tb_msg.Name = "tb_msg";
            this.tb_msg.Size = new System.Drawing.Size(695, 21);
            this.tb_msg.TabIndex = 3;
            // 
            // cb_version
            // 
            this.cb_version.FormattingEnabled = true;
            this.cb_version.Items.AddRange(new object[] {
            "15",
            "16",
            "3",
            "2",
            "1",
            "0"});
            this.cb_version.Location = new System.Drawing.Point(85, 27);
            this.cb_version.Name = "cb_version";
            this.cb_version.Size = new System.Drawing.Size(121, 20);
            this.cb_version.TabIndex = 4;
            this.cb_version.Text = "5";
            // 
            // cb_icon_border
            // 
            this.cb_icon_border.FormattingEnabled = true;
            this.cb_icon_border.Items.AddRange(new object[] {
            "5",
            "4",
            "3",
            "2",
            "1"});
            this.cb_icon_border.Location = new System.Drawing.Point(280, 95);
            this.cb_icon_border.Name = "cb_icon_border";
            this.cb_icon_border.Size = new System.Drawing.Size(121, 20);
            this.cb_icon_border.TabIndex = 5;
            this.cb_icon_border.Text = "5";
            // 
            // cb_pixel
            // 
            this.cb_pixel.DisplayMember = "7";
            this.cb_pixel.FormattingEnabled = true;
            this.cb_pixel.Items.AddRange(new object[] {
            "7",
            "6",
            "5",
            "4",
            "3"});
            this.cb_pixel.Location = new System.Drawing.Point(280, 27);
            this.cb_pixel.Name = "cb_pixel";
            this.cb_pixel.Size = new System.Drawing.Size(121, 20);
            this.cb_pixel.TabIndex = 6;
            this.cb_pixel.Text = "7";
            this.cb_pixel.ValueMember = "7";
            // 
            // cb_icon_size
            // 
            this.cb_icon_size.FormattingEnabled = true;
            this.cb_icon_size.Items.AddRange(new object[] {
            "20",
            "19",
            "18",
            "17",
            "16"});
            this.cb_icon_size.Location = new System.Drawing.Point(87, 95);
            this.cb_icon_size.Name = "cb_icon_size";
            this.cb_icon_size.Size = new System.Drawing.Size(121, 20);
            this.cb_icon_size.TabIndex = 7;
            this.cb_icon_size.Text = "20";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(13, 34);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(41, 12);
            this.label2.TabIndex = 8;
            this.label2.Text = "版本:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(13, 103);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(65, 12);
            this.label3.TabIndex = 9;
            this.label3.Text = "图标尺寸:";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(212, 27);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(29, 12);
            this.label4.TabIndex = 10;
            this.label4.Text = "像素";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(214, 95);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(65, 12);
            this.label5.TabIndex = 11;
            this.label5.Text = "图标边线:";
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(15, 163);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(29, 12);
            this.label6.TabIndex = 12;
            this.label6.Text = "白边";
            // 
            // rb_we_y
            // 
            this.rb_we_y.AutoSize = true;
            this.rb_we_y.Location = new System.Drawing.Point(87, 163);
            this.rb_we_y.Name = "rb_we_y";
            this.rb_we_y.Size = new System.Drawing.Size(35, 16);
            this.rb_we_y.TabIndex = 13;
            this.rb_we_y.TabStop = true;
            this.rb_we_y.Text = "";
            this.rb_we_y.UseVisualStyleBackColor = true;
            // 
            // radioButton2
            // 
            this.radioButton2.AutoSize = true;
            this.radioButton2.Location = new System.Drawing.Point(151, 163);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(35, 16);
            this.radioButton2.TabIndex = 14;
            this.radioButton2.TabStop = true;
            this.radioButton2.Text = "";
            this.radioButton2.UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(262, 443);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(139, 32);
            this.button2.TabIndex = 15;
            this.button2.Text = "生成二维码法2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(885, 579);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.radioButton2);
            this.Controls.Add(this.rb_we_y);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.cb_icon_size);
            this.Controls.Add(this.cb_pixel);
            this.Controls.Add(this.cb_icon_border);
            this.Controls.Add(this.cb_version);
            this.Controls.Add(this.tb_msg);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.pb_qrcode);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pb_qrcode)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.PictureBox pb_qrcode;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox tb_msg;
        private System.Windows.Forms.ComboBox cb_version;
        private System.Windows.Forms.ComboBox cb_icon_border;
        private System.Windows.Forms.ComboBox cb_pixel;
        private System.Windows.Forms.ComboBox cb_icon_size;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.RadioButton rb_we_y;
        private System.Windows.Forms.RadioButton radioButton2;
        private System.Windows.Forms.Button button2;
    }
}

后台代码

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

namespace QRCoder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //法1
        private void button1_Click(object sender, EventArgs e)
        {

            //Image image;
            //try
            //{
            //    QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            //    qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            //    qrCodeEncoder.QRCodeScale = Convert.ToInt32(cb_icon_size.Text);//尺寸
            //    qrCodeEncoder.QRCodeVersion = Convert.ToInt32(cb_version.Text);//版本
            //    qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            //    image = qrCodeEncoder.Encode(tb_msg.Text);
            //}
            //catch (Exception ex)
            //{
            //    image = null;
            //}
            //if (image != null)
            //{
            //    pb_qrcode.Image = image;
            //}

            //create_two(this.tb_msg.Text);
            string strUrl = "https://www.baidu.com/";// "https://eco.blockchainlock.io/keyvox";
            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;
            string curtime = currentTime.ToString("yyyy-MM-dd");//zhj-2019-05-08操作时间    
            string strContent = strUrl;// +"/16211580/" + curtime; //Data:/ getapp /< 设备ID >/< UTC时间 >
            try
            {
                Bitmap bt = CreateQRCode(strContent, 291, 5);
                bt.Save(".\\image.Png", System.Drawing.Imaging.ImageFormat.Png);
                pb_qrcode.Image = Image.FromFile(@".\\image.Png");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            //System.Web.HttpUtility.UrlEncode(strContent, System.Text.Encoding.Unicode);
            //create_two(strContent);

            //int version = Convert.ToInt16(cb_version.Text);
            //int pixel = Convert.ToInt16(cb_pixel.Text);
            //string str_msg = tb_msg.Text;
            //int int_icon_size = Convert.ToInt16(cb_icon_size.Text);
            //int int_icon_border = Convert.ToInt16(cb_icon_border.Text);
            //bool b_we = rb_we_y.Checked ? true : false;
            //Bitmap bmp = QRCoder.Encoder.code(str_msg, version, pixel, "E:/seaconch/git/1.jpg", int_icon_size, int_icon_border, b_we);
            //pb_qrcode.Image = bmp;
        }
        private void create_two(string nr)
        {
            Bitmap bt;
            string enCodeString = nr;
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);
            //string filename = string.Format(DateTime.Now.ToString(), "yyyymmddhhmmss")
            //+ ".jpg";
            //filename = filename.Replace(" ", "");
            //filename = filename.Replace(":", "");
            //filename = filename.Replace("-", "");
            //filename = filename.Replace(".", "");
            bt.Save(".\\image.Png", System.Drawing.Imaging.ImageFormat.Png);
            pb_qrcode.Image = Image.FromFile(@".\\image.Png");

        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="Content">内容文本</param>
        /// <param name="size">图片尺寸(像素)</param>
        /// <param name="margin">图片白边(像素)</param>
        /// <returns></returns>
        public static Bitmap CreateQRCode(string Content, int size, int margin = 5)
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
           
            qrCodeEncoder.QRCodeScale = 10;
            qrCodeEncoder.QRCodeVersion = 6;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            try
            {
                System.Drawing.Image image = qrCodeEncoder.Encode(Content);
                int resWidth = size + 2 * margin;
                int resHeight = size + 2 * margin;
                // 核心就是这里新建一个bitmap对象然后将image在这里渲染
                Bitmap newBit = new Bitmap(resWidth, resHeight, PixelFormat.Format32bppRgb);
                Graphics gg = Graphics.FromImage(newBit);

                // 设置背景白色
                for (int y = 0; y < resWidth; y++)
                {
                    for (int x = 0; x < resHeight; x++)
                    {
                        newBit.SetPixel(x, y, Color.White);
                    }
                }

                // 设置黑色边框
                for (int i = 0; i < resWidth; i++)
                {
                    newBit.SetPixel(i, 0, Color.Black);
                    newBit.SetPixel(i, resWidth - 1, Color.Black);

                }

                for (int j = 0; j < resHeight; j++)
                {
                    newBit.SetPixel(0, j, Color.Black);
                    newBit.SetPixel(resHeight - 1, j, Color.Black);

                }
                gg.DrawImage(image, margin, margin, size, size);
                //newBit.Save(size.ToString() + ".png");
                return newBit;
            }
            catch (Exception)
            {
                return null;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 法2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string strUrl = "https://www.baidu.com/";// "https://eco.blockchainlock.io/keyvox";
            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;
            string curtime = currentTime.ToString("yyyy-MM-dd");//zhj-2019-05-08操作时间    
            string strContent = strUrl;// +"/16211580/" + curtime; //Data:/ getapp /< 设备ID >/< UTC时间 >
            create_two(strContent);
        }
    }

}

如果二维码是用网址生成的,网址正确的情况下,扫描二维码会打开该网址 。

猜你喜欢

转载自www.cnblogs.com/aijiao/p/10830988.html
今日推荐