C # method of generating two-dimensional code (QR code) using qrcode.net _c# Tutorial

Source: Internet
Author: User

Now many applications on the web are using two-dimensional code to share the Web site or other information. Especially in the mobile field, two-dimensional code is a very big application scene. Because of the needs of the project, need to add a two-dimensional code analysis site in the website function, in the case of Google a large cramp in the circumstances of the helpless use of Baidu. Baidu N Many, find some items, but not strong usability. (one project is developed with VS2005, and debugging is not open in 2010.) Finally found a "artifact" on CodePlex, the "artifact" can be very convenient to generate two-dimensional code, speed that is quite fast, and can support Chinese, follow the MIT agreement.

Qrcode.net is a class library that uses C # to generate two-dimensional code pictures, which makes it easy to provide two-dimensional coded output for WinForm, WebForm, WPF, Silverlight, and Windows Phone 7 applications. You can export a two-dimensional code file to an EPS format.

The project address is: http://qrcodenet.codeplex.com

Qrcode.net no longer uses the http://code.google.com/p/zxing/ZXing port, the new version will have better performance.

The test results are as follows (microseconds):

Input string Length: 74

EC Performance 1000 tests~ qrcode.net:3929 zxing:5221

At the same time, Qrcode.net can parse the string to determine whether to use UTF-8 encoding. (for example, when using Chinese.) )

QRCode Use Method:

New project adds a reference to the class library, and then introduces the Gma.QrCodeNet.Encoding namespace.

Using Gma.QrCodeNet.Encoding;

To output a two-dimensional code in the console:

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 ();

This code produces the following output:

To draw a two-dimensional code on a 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);
}

To draw a two-dimensional code on a 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 to null if input string is null or empt 
Y. Module wide is version 1 QRCode ' s width. int pixelsize = Qrcode.matrix = null? Renderer. Measure: Renderer.
Measure (QrCode.Matrix.Width);
WriteableBitmap Wbitmap = new WriteableBitmap (pixelsize, Pixelsize, n, pixelformats.gray8, NULL); If Wbitmap is null value.
Renderer'll create Gray8 Bitmap as default. Renderer. Draw (Wbitmap, Qrcode.matrix);
Default offset position is (0, 0); Now, can put wbitmap to image control's Source or use it to create image file. 

If you need to render a two-dimensional code in a WinForm or WPF application, you can drag the class library directly into the Toolbox and drag the control directly onto the form.

Save the two-dimensional code directly to the file:

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

To write a two-dimensional code to the 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);

The above mentioned is small set to introduce C # using Qrcode.net to generate two-dimensional code (QR code), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.