首先安装这个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();}#endregionprivate 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();}//法1private 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);}}}

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

转载于:https://www.cnblogs.com/aijiao/p/10830988.html

C# winform Qrcoder二维码相关推荐

  1. C# Winform 使用二维码

    关于C# Winform 程序中使用二维码的使用记录: 1.使用 Nuget 安装 ZXing.Net 程序包: 2.调用代码: private void button1_Click(object s ...

  2. C# 生成二维码, winform生成二维码,二维码编号自动增加

    首先在设计器画出来对应的UI展示样式: 效果: 设计代码: namespace LongDeTools {partial class FrmLedPreview{/// <summary> ...

  3. C#WinForm二维码编码解码器

    使用ThoughtWorks.QRCode组件,该组件是一个免费开源的二维码操作动态链接库,在进行程序编写实现该系统的过程中,应先将ThoughtWorks.QRCode.dll文件通过"添 ...

  4. 使用QRCoder生成二维码【.NET 6.0】

    nuget 包 QRCoder 二维码 install-package QRCoder -1.4.3 粘贴板 install-package TextCopy -6.2.1 using QRCoder ...

  5. Winform中实现Excel导入、表格展示、多选获取值、生成二维码、打印流程(附代码下载)

    场景 整体流程需求 1.导入Excel并获取Excel的数.. 2.将Excel的数据复制给DataGridView中进行显示并能实现多选. 3.根据选中的内容生成二维码. 4.将二维码打印. 整体效 ...

  6. Winform中使用printDocument控件打印pictureBox中的二维码照片

    场景 Winform中使用zxing和Graphics实现自定义绘制二维码布局: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  7. Winform中使用zxing实现二维码生成(附dll下载)

    场景 zxing.dll下载 https://download.csdn.net/download/badao_liumang_qizhi/11623214 效果 实现 新建Winform程序,将上面 ...

  8. C# 代码生成二维码方法及代码示例(QRCoder)

    背景 二维码是越来越流行了,很多地方都有可能是使用到.如果是静态的二维码还是比较好处理的,通过在线工具就可以直接生成一张二维码图片,比如:草料二维码.但有的时候是需要动态生成的(根据动态数据生成),这 ...

  9. 二维码图片生成工具C#winform源码

    二维码图片生成工具C#winform源码 源码描述: 一.源码特点 采用winform进行开发,生成二维码并保存,欢迎下载 二.功能介绍 本源码是一个可以自动生成二维码图片的小模块,可以添加自己的lo ...

最新文章

  1. Chrome 浏览器跨域和安全访问问题 使用 chrome的命令行标记:disable-web-security 参数联调线上数据...
  2. 在python中构造时间戳参数的方法
  3. JAVA基础:Hibernate外键关联与HQL语法
  4. python图形编程复选按钮和单选按钮详细说明_Python_tkinter_单选框(Radiobutton)与复选框(Checkbutton)...
  5. 给ubuntuGedit安装gedit-source-code-browser
  6. 一个程序员送给大学生的一些话
  7. Android OnLowMemory和OnTrimMemory
  8. php 修复excel 乱码,php下载excel乱码的解决方法
  9. 2019微型计算机年度显示器,「MC 2019年度评选获奖产品展播」华硕/ROG DIY精品斩获多个大奖...
  10. xv6 System Call
  11. 如何做好系统分析与设计
  12. VBA word 文件类型html,使用vba进行Word文档的数学格式设置
  13. 旺旺的计算机无法打开,电脑任务栏中点击阿里旺旺图标无法打开界面的解决方法...
  14. TTL电路和CMOS电路的区别和…
  15. 二维码应用——门禁系统
  16. 文件正在使用,操作无法完成,因为文件已在...中打开
  17. java小红球下载_小红球闯关
  18. 输入商品显示商品名称和价格
  19. 微服务 杜家豪_搞好“微建设微服务”也是大业绩
  20. Blender图解教程:手把手教你做马里奥问号箱 一 强迫症慎用版(附模型下载)

热门文章

  1. 关于Android Studio单元测试中“Method d in android.util.Log not mocked.”问题的解决。
  2. 《想做视频号必须要懂的知识——音质篇》
  3. java entries_Enumerationlt;? extends ZipEntrygt; entries()_学习Java Zip|WIKI教程
  4. SpringBoot 默认数据库连接池 HikariCP
  5. 特种浓缩分离:生物农药超滤分离提纯过滤技术
  6. The Derivation About CNN and Antoencoder
  7. python123 第四次作业答案_Python第四次作业——黄亦杨
  8. day 9/16 css三大特性和盒子模型
  9. ADAS自动驾驶辅助系统通讯协议「ADASIS Protocol」接口定义解析(二)详细
  10. 【MyBatis】多表查询