C# 生成二维码方法(QRCoder)
前言
二维码很多地方都有使用到。如果是静态的二维码还是比较好处理的,通过在线工具就可以直接生成一张二维码图片,比如:草料二维码。
但有的时候是需要动态生成的(根据动态数据生成),这个使用在线就工具就无法实现了。最好是能在代码中直接生成一个二维码图片,介绍下使用QRCoder类库在代码中生成二维码。
网上生成二维码的组件还是挺多的,但是真正好用且快速的却不多。QRCoder就是我在众多中找到的,它的生成速度快、而且使用也相当方便。
开始编码
1、安装 QRCoder组件。在项目上通过NuGet包管理器来安装,搜索名称:QRCoder
2、在代码中添加引用:using QRCoder;
3、编码生成
private void RenderQrCode()
{
string level = "Q";// comboBoxECC.SelectedItem.ToString();
QRCodeGenerator.ECCLevel eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3);
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
{
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode("9f3274ec-1481-4988-a3c8-698bbafbf14b", eccLevel))
{
using (QRCode qrCode = new QRCode(qrCodeData))
{
pictureBoxQRCode.BackgroundImage = qrCode.GetGraphic(20, Color.Black, Color.White,
null, 50);
this.pictureBoxQRCode.Size = new System.Drawing.Size(pictureBoxQRCode.Width, pictureBoxQRCode.Height);
//Set the SizeMode to center the image.
this.pictureBoxQRCode.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBoxQRCode.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
}
}
上面代码运行的结果

还可以加上logo
private Bitmap GetIconBitmap()
{
Bitmap img = null;
if (iconPath.Text.Length > 0)
{
try
{
img = new Bitmap(iconPath.Text);
}
catch (Exception)
{
}
}
return img;
}
完整代码
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 QRCoder;
using System.Drawing.Imaging;
using System.IO;
namespace QRCoderDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBoxECC.SelectedIndex = 0; //Pre-select ECC level "L"
RenderQrCode();
}
private void buttonGenerate_Click(object sender, EventArgs e)
{
RenderQrCode();
}
private void RenderQrCode()
{
string level = comboBoxECC.SelectedItem.ToString();
QRCodeGenerator.ECCLevel eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3);
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
{
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(textBoxQRCode.Text, eccLevel))
{
using (QRCode qrCode = new QRCode(qrCodeData))
{
pictureBoxQRCode.BackgroundImage = qrCode.GetGraphic(20, Color.Black, Color.White,
GetIconBitmap(), (int) iconSize.Value);
this.pictureBoxQRCode.Size = new System.Drawing.Size(pictureBoxQRCode.Width, pictureBoxQRCode.Height);
//Set the SizeMode to center the image.
this.pictureBoxQRCode.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBoxQRCode.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
}
}
private Bitmap GetIconBitmap()
{
Bitmap img = null;
if (iconPath.Text.Length > 0)
{
try
{
img = new Bitmap(iconPath.Text);
}
catch (Exception)
{
}
}
return img;
}
private void selectIconBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDlg = new OpenFileDialog();
openFileDlg.Title = "Select icon";
openFileDlg.Multiselect = false;
openFileDlg.CheckFileExists = true;
if (openFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
iconPath.Text = openFileDlg.FileName;
if (iconSize.Value == 0)
{
iconSize.Value = 15;
}
}
else
{
iconPath.Text = "";
}
}
private void btn_save_Click(object sender, EventArgs e)
{
// Displays a SaveFileDialog so the user can save the Image
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Bitmap Image|*.bmp|PNG Image|*.png|JPeg Image|*.jpg|Gif Image|*.gif";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if (saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
using (FileStream fs = (System.IO.FileStream) saveFileDialog1.OpenFile())
{
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
ImageFormat imageFormat = null;
switch (saveFileDialog1.FilterIndex)
{
case 1:
imageFormat = ImageFormat.Bmp;
break;
case 2:
imageFormat = ImageFormat.Png;
break;
case 3:
imageFormat = ImageFormat.Jpeg;
break;
case 4:
imageFormat = ImageFormat.Gif;
break;
default:
throw new NotSupportedException("File extension is not supported");
}
pictureBoxQRCode.BackgroundImage.Save(fs, imageFormat);
fs.Close();
}
}
}
public void ExportToBmp(string path)
{
}
private void textBoxQRCode_TextChanged(object sender, EventArgs e)
{
RenderQrCode();
}
private void comboBoxECC_SelectedIndexChanged(object sender, EventArgs e)
{
RenderQrCode();
}
}
}
C# 生成二维码方法(QRCoder)的更多相关文章
- php+qrcode类+生成二维码方法
//生成二维码 public function qrcode() { $data = input(); if(!$data['param']){ return json(['code ' => ...
- PHP生成二维码方法
<?php //先下载一份phpqrcode类,下载地址http://down.51cto.com/data/780947require_once("phpqrcode/phpqrco ...
- C#通过第三方组件生成二维码(QR Code)和条形码(Bar Code)
用C#如何生成二维码,我们可以通过现有的第三方dll直接来实现,下面列出几种不同的生成方法: 1):通过QrCodeNet(Gma.QrCodeNet.Encoding.dll)来实现 1.1):首先 ...
- zxing生成二维码和条码
/*** * 生成二维码方法 * @param str 生成内容 * @param widthHeight 宽度和高度 * @return * @throws WriterException */ p ...
- C# 根据字符串生成二维码
1.先下载NuGet包(ZXing.Net) 2.新建控制器及编写后台代码 using System; using System.Collections.Generic; using System.D ...
- C# 生成二维码(QR Code)
参考: C#通过ThoughtWorks.QRCode生成二维码(QR Code) 通过ThoughtWorks.QRCode(ThoughtWorks.QRCode.dll)来实现 1) ...
- 基于Asp.Net Core,利用ZXing来生成二维码的一般流程
本文主要介绍如何在.net环境下,基于Asp.Net Core,利用ZXing来生成二维码的一般操作.对二维码工作原理了解,详情见:https://blog.csdn.net/weixin_36191 ...
- php(tp5) 生成二维码
phpqrcode类库官网下载地址:https://sourceforge.net/projects/phpqrcode/ 1.我们先看看php是怎么生成二维码的 1.首先我们先下载一下 phpqr ...
- php 使用phpqrcode生成二维码并上传到OSS
一般情况调用phpqrcode第三方插件 会把生成的二维码图片保存到服务器,不保存服务器也会以header头的形式输出到浏览器,(我们不允许把图片文件保存的liunx服务器,只能保存到阿里云OSS存储 ...
- 4种方法生成二维码 (js 控制canvas 画出 二维码)
随着网络的迅速发展 发展 发展,二维码的应用将会越来越多.同时很多只是很平凡的二维码,请拿起你的手 把这个二维码 设计起来吧.下面分享了几个非常好的二维码设计. 二维码原理: 二维条码/二维码可以分 ...
随机推荐
- Python装饰器实例讲解(一)
Python装饰器实例讲解(一) 多种角度讲述这个知识,这是个系列文章 但前后未必有一定的顺承关系 部分参考网络 本文以一个小案例引出装饰器的一些特点,不涉及理论,后面再谈 案例 写一个代码来求一个数 ...
- Nginx 安装perl
1 安装包下载 https://www.cpan.org/src获取最新偶数版本下载链接并替换(偶数版本为稳定版) 2 上传到服务器解压 tar -zxvf perl-5.36.0.tar.gz 3 ...
- mysql18-Show Profile和全局日志
1.是什么 分析SQL执行带来的开销是优化SQL的重要手段.在MySQL数据库中,可以通过配置profiling参数来启用SQL剖析.该参数开启后,后续执行的SQL语句都将记录其资源开销,诸如IO,上 ...
- window系统增强优化工具
计算机系统优化的作用很多,它可以清理WINDOWS临时文件夹中的临时文件,释放硬盘空间:可以清理注册表里的垃圾文件,减少系统错误的产生:它还能加快开机速度,阻止一些程序开机自动执行:还可以加快上网和关 ...
- 学习Java Day25
今天学习了类路径和如何设置类路径 其中需要创建JAR后面才会介绍如何创建JAR,类路径可以节省空间并改善性能,设置类路径可以用-classpath选项指定路径
- JS逆向实战11——某金属集团动态cookie加密
本文来自:来自: https://www.cnblogs.com/zichliang/ 目标网站 aHR0cDovL3d3dy50bm1nLmNvbS5jbi9pbmZvcm1hdGlvbi9pbmZ ...
- Idea External Libraries 没有导入依赖
Maven 下面是有依赖的,但是 Idea 的 External Libraries 没有导入进来,就非常奇怪,这个现象我在 Android Studio 也遇到过,要么找到 Maven 仓库,手动把 ...
- Infinity 和 -Infinity
认识 Infinity 在 JavaScript 中超出 1.797693134862315E+308 的数值即为 Infinity,小于 -1.797693134862316E+308 的数值为无穷 ...
- 【django-vue】前端取消默认样式 main.js配置 后端主页模块接口 跨域问题详解 项目自定义配置 git介绍和安装
目录 回顾 上节课回顾 今日内容 1 前端全局样式和js配置 1.1 global.css 1.2 settings.js 1.3 main.js 2 后端主页模块接口 三种开发模式 模型父类Base ...
- PULPINO仿真建立
PULPINO仿真建立 PULPINO主要特点: systemverilog实现 有常规外设,GPIO.SPI.I2C.UART等 有调试接口 总线有AXI,外设总线为APB,扩展性好 脚本为cmak ...