C#利用QrCode.Net生成二维码(Qr码)的方法

作者:Soar、毅 时间:2023-11-30 17:37:38 

现在网上很多应用都是用二维码来分享网址或者其它的信息。尤其在移动领域,二维码更是有很大的应用场景。因为项目的需要,需要在网站中增加一个生成二维码分析网址的功能,在谷歌大幅度抽筋的情况下无奈使用百度。百度N多,找到一些项目,但是可用性不强。(有一个项目是用VS2005开发的,在2010中调试不开。)终于在codeplex上找到一个“神器”,这个“神器”可以很方便的生成二维码,速度那是相当的快,并且可支持中文,遵从MIT协议。

QrCode.Net是一个使用C#编写的用于生成二维码图片的类库,使用它可以非常方便的为WinForm、WebForm、WPF、Silverlight和Windows Phone 7应用程序提供二维码编码输出功能。可以将二维码文件导出为eps格式。

项目地址为:http://qrcodenet.codeplex.com

QrCode.Net不再采用http://code.google.com/p/zxing/ ZXing的端口,新的版本将有更好的性能。

测试结果如下(微秒):

输入字符串长度:74个

EC performance 1000 Tests~ QrCode.Net: 3929 ZXing: 5221

同时,QrCode.Net可以对字符串进行分析,决定是否使用UTF-8编码。(比如使用中文的时候。)

QrCode使用方法:

新建项目添加对类库的引用,然后引入Gma.QrCodeNet.Encoding命名空间。

using Gma.QrCodeNet.Encoding;

在控制台中输出二维码:


Console.Write(@"Type some text to QR code: ");
string sampleText = Console.ReadLine();
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
QrCode qrCode = qrEncoder.Encode(sampleText);
for (int j = 0; j < qrCode.Matrix.Width; j++)
{
for (int i = 0; i < qrCode.Matrix.Width; i++)
{
char charToPrint = qrCode.Matrix[i, j] ? '█' : ' ';
Console.Write(charToPrint);
}
Console.WriteLine();
}
Console.WriteLine(@"Press any key to quit.");
Console.ReadKey();

此代码将产生以下输出:

C#利用QrCode.Net生成二维码(Qr码)的方法

在Graphics上绘制二维码:


const string helloWorld = "Hello World!";
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = qrEncoder.Encode(helloWorld);
const int moduleSizeInPixels = 5;
Renderer renderer = new Renderer(moduleSizeInPixels, Brushes.Black, Brushes.White);
Panel panel = new Panel();
Point padding = new Point(10,16);
Size qrCodeSize = renderer.Measure(qrCode.Matrix.Width);
panel.AutoSize = false;
panel.Size = qrCodeSize + new Size(2 * padding.X, 2 * padding.Y);
using (Graphics graphics = panel.CreateGraphics())
{
renderer.Draw(graphics, qrCode.Matrix, padding);
}

在WriteableBitmap上绘制二维码:


const string helloWorld = "Hello World!";
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode(helloWorld, out qrCode);
const int moduleSizeInPixels = 5;
Renderer renderer = new Renderer(moduleSizeInPixels); //Black&White is default colour for drawing QrCode
//Matrix under qrCode might be null if input string is null or empty. 21 module wide is version 1 QrCode's width.
int pixelSize = qrCode.Matrix == null ? renderer.Measure(21) : renderer.Measure(qrCode.Matrix.Width);
WriteableBitmap wBitmap = new WriteableBitmap(pixelSize, pixelSize, 96, 96, PixelFormats.Gray8, null);
//If wBitmap is null value. renderer will create Gray8 Bitmap as default.
renderer.Draw(wBitmap, qrCode.Matrix); //Default offset position is (0, 0);
//Now you can put wBitmap to Image control's Source or use it to create image file.

如果需要把二维码呈现在WinForm或者WPF应用程序中,可以直接把类库拖入工具箱,然后直接在窗体上拖出控件。

C#利用QrCode.Net生成二维码(Qr码)的方法

直接把二维码保存到文件:


QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode(helloWorld, out qrCode);
Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);
renderer.CreateImageFile(qrCode.Matrix, @"c:\temp\HelloWorld.png", ImageFormat.Png);

将二维码写入Stream:


QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode(helloWorld, out qrCode);
Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);
MemoryStream ms = new MemoryStream();
renderer.WriteToStream(qrCode.Matrix, ms, ImageFormat.png);

以上所述是小编给大家介绍的C#使用QrCode.Net生成二维码(Qr码)网站的支持!

来源:http://www.cnblogs.com/Soar1991/archive/2012/03/30/2426115.html

标签:qrcode,二维码
0
投稿

猜你喜欢

  • C#对XML文件的各种操作实现方法

    2023-01-21 06:14:40
  • SpringBoot整合Mybatis与thymleft实现增删改查功能详解

    2023-01-06 00:22:59
  • springboot中的pom文件 project报错问题

    2022-01-24 00:41:55
  • java中的Io(input与output)操作总结(四)

    2021-10-11 03:14:19
  • Unity2019-2020 个人版官方免费激活详细方法

    2023-12-08 21:57:39
  • C# WebApi CORS跨域问题解决方案

    2022-12-21 05:01:33
  • 解决Android popupWindow设置背景透明度无效的问题

    2022-12-05 10:39:42
  • 基于jdk1.8的Java源码详解 Integer

    2023-05-08 11:32:22
  • Spring Boot 项目发布到 Tomcat 服务器的操作步骤

    2023-10-28 09:39:05
  • 设计模式系列之组合模式及其在JDK和MyBatis源码中的运用详解

    2022-12-27 12:56:57
  • Java @Accessors注解图文详解

    2023-10-10 06:06:51
  • Java与Scala创建List与Map的实现方式

    2021-07-19 23:53:07
  • 详解Spring Boot 事务的使用

    2022-08-09 01:59:07
  • Java中自动装箱、拆箱引起的耗时详解

    2023-01-11 11:42:42
  • SpringBoot+WebSocket实现消息推送功能

    2021-11-15 12:16:18
  • Android 后台发送邮件示例 (收集应用异常信息+Demo代码)

    2022-06-24 16:31:06
  • 基于Java回顾之JDBC的使用详解

    2021-12-24 16:36:03
  • 详解Java动态字节码技术

    2022-06-20 03:20:20
  • 详解Java的Hibernate框架中的List映射表与Bag映射

    2021-09-16 05:35:05
  • Android Monkey压力测试详细介绍

    2021-10-24 08:02:37
  • asp之家 软件编程 m.aspxhome.com