RSS

QR Code Generator in C#

07 May

By Silverlancer

QrCode Wiki:

Index:

 Developer Documentation for Contributors

Usage generating QR code

Add a reference to project or assemblyGma.QrCodeNet.Encoding and import the namespace.

using Gma.QrCodeNet.Encoding;

Encode text to QR code bit matrix :

Encoder.Encode(“”) will throw exception for empty string or when string is huge where QrCode doesn’t have enough space.

            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 will generate following output:

ConsoleOutput.png

Render QR code to graphics, let’s say to panel control with segment size of 5 pixel, blue on yellow and 10 pixel padding:

using Gma.QrCodeNet.Encoding.Windows.Controls;
            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);
            }

Render QR code to DrawingContext, let’s say to DrawingVisual with segment size of 5 pixel, blue on yellow and 10 pixel padding:

using Gma.QrCodeNet.Encoding.Windows.WPF;
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, Colors.Blue, Colors.Yellow);

DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();

renderer.Draw(drawingContext, qrCode.Matrix, new Point(0, 20));   //Offset for Matrix position => new Point(0, 20)

drawingContext.Close();

Render QR code to WriteableBitmap, with segment size of 5 pixel, black on white and 2 pixel padding:

Currently, Renderer under WPF section only support drawing QrCode at Pbgra32 or Gray8 pixel format. Gray8 is memory efficient pixel format where Pbgra32 enables colour stream with alpha value.

using Gma.QrCodeNet.Encoding.Windows.WPF;
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.

Using out-of-the-box WinForms/WPF control. Just drag & drop it to your form an set Text and AutoSize property:

WinForms/WPF control can now change module size, color, errorcorrection level at run time
QrCodeImgControl is alternative solution to QrCodeControl. Where it has stretch function for QrCode to fit on specific size.
WPF version of ImgControl can also choose ColorMode. non color mode gives better memory performance

UsingControlScreenshot.JPG

Saving QR code 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);

Saving QR code to 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);
Advertisements
 
2 Comments

Posted by on May 7, 2012 in C#

 

2 responses to “QR Code Generator in C#

  1. avapose

    April 2, 2013 at 3:38 PM

     
  2. radek

    November 29, 2013 at 8:42 PM

    hi, from where did u take the renderer class? Do I have to import somehow?

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: