Http://www.cnblogs.com/Soar1991/archive/2012/03/30/2426115.html
Many applications on the web now use QR codes to share URLs or other information. Especially in the mobile domain, the QR code is a very large application scenario. Because of the needs of the project, need to add a Web site to generate two-dimensional code analysis of the function of the URL, in the case of large cramps in Google's frustration using Baidu. Baidu N More, find some projects, but the availability is not strong. (There is a project developed with VS2005, debugging in 2010 does not open.) Finally found a "artifact" on the CodePlex, this "artifact" can be very convenient to generate two-dimensional code, speed that is quite fast, and can support Chinese, adhere to the MIT Protocol.
Qrcode.net is a class library written in C # to generate two-dimensional code images, which makes it easy to provide QR code output functionality for WinForm, WebForm, WPF, Silverlight, and Windows Phone 7 applications. You can export two-dimensional code files to EPS format.
The project address is: http://qrcodenet.codeplex.com
The qrcode.net no longer uses the http://code.google.com/p/zxing/ZXing port, and the new version will have better performance.
The test results are as follows (in microseconds):
Input string Length: 74 x
EC Performance tests~ qrcode.net:3929 zxing:5221
At the same time, Qrcode.net can parse the string and decide whether to use UTF-8 encoding. (for example, when using Chinese.) )
QRCode How to use:
New project adds a reference to the class library and then introduces the Gma.QrCodeNet.Encoding namespace.
?
using Gma.QrCodeNet.Encoding; |
Output the QR 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 QR code on the 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);
}
|
Draw the QR code on the 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.
|
If you need to present the QR code in a WinForm or WPF application, you can drag the class library directly into the Toolbox and then drag the control directly onto the form.
Save QR Code directly to 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); |
Write the QR 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); |
C # generates two-dimensional code (QR code) using Qrcode.net