背景

二维码是越来越流行了,很多地方都有可能是使用到。如果是静态的二维码还是比较好处理的,通过在线工具就可以直接生成一张二维码图片,比如:草料二维码。但有的时候是需要动态生成的(根据动态数据生成),这个使用在线就工具就无法实现了。最好是能在代码中直接生成一个二维码图片,这里我就介绍下使用QRCoder类库在代码中生成二维码。

网上生成二维码的组件还是挺多的,但是真正好用且快速的却不多。QRCoder就是我在众多中找到的,它的生成速度快、而且使用也相当方便。

开始编码

1、安装 QRCoder组件。在项目上通过NuGet包管理器来安装,搜索名称:QRCoder

2、在代码中添加引用:using QRCoder;

3、编码生成

  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;}}}}

运行效果

上面代码运行的结果

加个Logo吧

还可以加上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 ImageSaveFileDialog 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)相关推荐

  1. Java实现一行代码生成二维码,可传输到前端展示,可自定义二维码样式,可设置图片格式,可对二维码添加图片,可对二维码添加文字,可以设置二维码大小、字体大小、字体颜色、边框颜色、边框大小等等

    Java实现一行代码生成二维码,可传输到前端展示,可自定义二维码样式,可设置图片格式,可对二维码添加图片,可对二维码添加文字,可以设置二维码大小.字体大小.字体颜色.边框颜色.边框大小等等. 0.准备 ...

  2. 如何用C代码生成二维码

    如何用C代码生成二维码 当下因微信和支付宝等手机应用广泛使用,而基于二维码/一维条码的移动支付,也借助手机移动端席卷全国,使得越来越多的人知道有"二维码"这么一种东西. 对于普通用 ...

  3. 微信小程序转二维码方法分享

    微信小程序转二维码方法分享 需要转码的可以看看 这个东西是看个人需求的,618就要来了,各种活动也将来袭 有些小伙伴不知道怎么生成 为了方便小程序邀请活动没法外发,这里分享下将小程序转二维码的方法 首 ...

  4. 获取任意微信公众号二维码方法

    [获取任意微信公众号二维码方法] 复制下面链接在浏览器里打开,iOS微信打开会报错. https://open.weixin.qq.com/qr/code?username=PlayerYK 将以上链 ...

  5. Java代码生成二维码

    Java代码生成二维码: 背景:在项目的需求中,大部分会需要你写一个生成二维码的功能,二维码里面存储特定的信息 示例: pom.xml依赖: <!-- Zxing--><depend ...

  6. python生成二维码代码_python 一行代码生成 二维码

    效果图如上:动态二维码,彩色二维码,黑白二维码: 看到别人做的炫酷二维码,瞬间心动,想动手做个 自己的炫酷二维码. github上有大神 做的框架,直接拿来用,安装工具 myqr: pip insta ...

  7. python二维码生成识别代码_Python3+qrcode+zxing生成和识别二维码教程

    一.安装依赖库 pip install qrcode pillow image zxing pillow是python3中PIL的代替库,image是生成图版需要用到的库 安装image时报错&quo ...

  8. 用Python制作动态二维码,一行代码就做到了

    如何做到用一行代码实现动态二维码的制作? 用法比较简单,直接通过pip安装即可 pip3 install myqr 安装完成之后,就可以生成你想要的二维码了.对!就是这么简单- 普通的二维码 直接执行 ...

  9. Android开发之google Zxing实现二维码扫描的代码分析

    1.技术简介    在Android中实现二维码的扫描主要是通过第三方框架来实现的,主要框架是google的Zxing.现在就用该框架来实现二维码及条形码的扫描及识别,同时对于手机中存储的图片也进行识 ...

最新文章

  1. web前端知识点太多_初学web前端,学习方法容易走偏,这是为什么?
  2. 数论-扩展中国剩余定理
  3. python实现50行代码_50行代码实现python计算器主要功能
  4. sql 查出一张表中重复的所有记录数据
  5. 09-CNN手写数字识别
  6. Flash和margue字幕滚动效果
  7. Juniper 防火墙session拥堵案例解决
  8. Hive中元数据表的含义
  9. hdu 敌兵布阵(线段树之单点更新)
  10. MOQL-复杂事件处理(CEP)
  11. Sass:@error
  12. 简单的springBoot集成jedis
  13. 2021年美赛成绩公布与美赛查询!美赛官网已更新
  14. sqlmap安装和使用
  15. 几大技术体系极其应用
  16. c语言课程设计报告书模板,C语言课程设计报告模板(最终版).doc
  17. python中水量_Python居然还能用于巨大的工程项目!比如三峡发电量估算系统!
  18. 三角函数常用公式总结
  19. Android项目实战--【谁是歌手-布局篇】
  20. idea打开后不显示界面,win+D快捷键解决问题

热门文章

  1. Excel VBA - Workbook对象
  2. 你的网页加载太慢了怎么办?
  3. Atlas Unknown Error
  4. C# ?. 判斷Null值
  5. Lie to me不错
  6. wampServer配置WWW根目录遇到的坑
  7. linux系统下nginx安装目录和nginx.conf配置文件目录
  8. Server.MapPath()的用法
  9. mysql按月进行表分区
  10. Linux操作系统下Sudo命令的使用方法说明