The package QRCode contains the classes you need to create QRCode (mode 2) barcodes within your .NET applications (windows and web applications).
QR Code is a matrix symbology which includes a finder pattern located at three corners of the symbol used to locate the symbol and figure out its size and orientation.
The main features of QRCode symbols are:
J4L-QRCode supports:
In order to run the sample application you must execute QRCodeDemo11.exe or QRCodeDemo10.exe.
In the sample application you can set all properties of the QRCode symbology.
You can execute the following commands:
- Refresh: repaint the symbol using the new properties.
- Print: send image to printer.
- Save: save the symbol in gif format.
- Exit: terminate application.
QRCode class
The main class for creating QRCode barcodes is J4L.QRCode.QRCode. It is a subclass of System.Windows.Forms.Control and can therefore be used for placing a barcode on a windows form.
The following properties allows you to configurate the barcode:
- ApplicationIndicator: see Fnc1Mode property.
- AutoConfigurate: if true the preferredVersion can be ignored if the data does not fit in the selected version.
- BarBackColor: background color.
- BarForeColor: foreground color (color of the bars).
- BinaryCode: sets the value to be encoded as an array of integers.
- Code: string to be encoded.
- Fnc1Mode: Selects FNC1 mode. Valid values are:
- FNC1_MODE_NO: disable FNC1 (default).
- FNC1_MODE_FIRST: This Mode Indicator identifies symbols encoding data formatted according to the UCC/EAN Application Identifiers standard.
- FNC1_MODE_SECOND: This Mode Indicator identifies symbols formatted in accordance with specific industry or application specifications previously agreed with AIM International. You must then set a value for the ApplicationIndicator property.
- ECI: Extended Channel interpretation to be used (-1 to use the default).
- Encoding: encoding mode (default is AUTO). Valid values are:
- AUTO: Automatic selecting of the encoding method.
- ALPHA: encode alpahnumeric characters only (upper case letter plus 9 additional characters).
- NUMERIC: encode alpahnumeric digits only.
- BYTE: use this mode to encode binary data.
- KANJI: encodes Kanji characters only.
- ErrorCorrectionLevel: Valid values are:
- CORRECTION_LEVEL_L (default). About 7% recovery capacity.
- CORRECTION_LEVEL_M: About 15% recovery capacity.
- CORRECTION_LEVEL_Q: About 25% recovery capacity.
- CORRECTION_LEVEL_H: About 30% recovery capacity.
- getCurrentX (read only): final position of the cursor. Use this properry to find out the size of the image in pixels.
- getCurrentY (read only): final position of the cursor. Use this properry to find out the size of the image in pixels.
- Margin: left and top margin in pixels (default is 30).
- ModuleSize: number of pixels which make a module (square) in the barcode (default is 4).
- PreferredVersion: preferred format. Another version will be automatically selected if AutoConfigurate=true and the amount of data and the selected error correction level does not fit in the preferred version. Valid values are 1 to 40.
- ProcessTilde: if true (default) the tilde character (~) will be processed like this:
- ~~: will be replaced with ~
- ~dxxx: will be replaced by the character whose ascii code is xxx. For example ~d065 will be replaced with A.
- Redraw: set this property to true if you need to rebuild the barcode.
Structured Append properties
- StructuredAppend: if true, the structured append mode is enabled (default is false).
- StructuredAppendCounter: number of symbols which make the sequence.
- StructuredAppendIndex: position of current symbol within the secuence (starting at 1).
The following method can be used for painting the barcode on an external graphics object:
- public void paint(Graphics g): paints the qrcode symbol.
If you need to created a image file you can do it like this:
[C#]
using J4L.QRCode;
...
// define variable
QRCode bc;
// create instance of the objact
bc = new QRCode();
// set barcode properties
bc.Code="12345678";
...
// set size and write to file
bc.Size = new System.Drawing.Size(368, 176);
bc.saveToFile("qrcode.gif","GIF");
[VBNET]
Imports J4L.QRCode
......
' define variable
Dim bc as QRCode
'create instance of the object
bc = new QRCode()
' set barcode properties
bc.Code="12345678"
...
' set size and write to file
bc.Size = new System.Drawing.Size(368, 176)
bc.SaveToFile("qrcode.gif","GIF")
You can also use the paint() method for rendering the barcode onto an external Graphics object:
[C#]
using J4L.QRCode;
using System.Drawing;...
// create image and graphics
Bitmap inMemoryImage =new Bitmap(300,300) ;
Graphics g= Graphics.FromImage(inMemoryImage) ;
// create barcode
QRCode bc=new QRCode();// set barcode properties
bc.Size=new Size(300,300);
bcCode="12345678";
...
// render barcode on "g"
bc.paint(g);[VBNET]
Imports J4L.QRCode
Imports System.Drawing..............
' create image and graphics
dim inMemoryImage as new Bitmap(300,300)
dim g as Graphics = Graphics.FromImage(inMemoryImage)
'create barcode
dim bc as QRCode =new QRCode()' set barcode properties
bc.Size=new Size(300,300);
bc.Code="12345678"
...
'render barcode on "g"
bc.paint(g)The windows forms control can be placed on a form with your IDE by just adding our controls to the toolbox. You can also programatically add controls to your form with the following code:
[C#]
using J4L.QRCode;
...
// define variable
QRCode bc;
// create instance of the objact
bc = new QRCode();
// define position and size of the object on the form
bc.Location = new System.Drawing.Point(8, 8);
bc.Size = new System.Drawing.Size(368, 176);// set barcode properties
bc.Code="12345678";
....
// add it to the form "this" is the current form.
this.Controls.Add(bc);[VBNET]
Imports J4L.QRCode
.....
' define variable
dim bc as QRCode
'create instance of the objact
bc = new QRCode()
'define position and size of the object on the form
bc.Location = new System.Drawing.Point(8, 8)
bc.Size = new System.Drawing.Size(368, 176)' set barcode properties
bc.Code="12345678";
...
'add it to the form "me" is the current form.
me.Controls.Add(bc)You can print the barcode by rendering in on the Graphics objects of the PrintDocument:
[C#]
void btnPrintClick(object sender, System.EventArgs e)
{
// create PrintDocument and set handler
PrintDocument printDocument1=new PrintDocument();
printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
printDocument1.Print();
}private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// print barcode here
bc.paint(e.Graphics);
}QRCodeWeb class
The class J4L.QRCode.QRCodeWeb is a subclass of WebControl and can be used in your web applications to create barcodes.
The Web Control can be used in 2 ways:
- Include the control in a asp.net page so that it creates a barcode and the HTML code for it. This approach will create a physical image file on your server. In order to use this approach you must only add the qrcode11.dll ( or qrcode10.dll if you use .NET 1.0) assembly to your favorite IDE and drop our components on your form. The resulting code will look like this.
<%@ Page Language="C#" %>
<%@ Register TagPrefix="n0" Namespace="J4L.QRCode" Assembly="qrcode10" %>
<script runat="server"></script>
<html>
<head>
</head>
<body>
<form runat="server">
<n0:QRCodeWeb id="QRCodeWeb1" runat="server" Code="12345" LocalImageDirectory="c:\dotnet\webmatrix\test" NumberOfCodes="1" ImageRetainTime="30" Resolution="200" Height="131px" PositionOfCode="1" Width="333px"></n0:QRCodeWeb>
<!-- Insert content here -->
</form>
</body>
</html>
The web control can be configured with the following properties:
- ImageRetainTime: (in minutes). Temporary barcode image files will be deleted after this time.
- LocalImageDirectory: directory where the barcode images will be created.
-ImageHTMLPrefix: directory where the barcode images will be created, from the browser's point of view.
- ImageFile: name of the created image file.
- Use it in a aspx page to create a image on the fly. Your html page should contain a tag like this:
<img SRC=barcode.aspx ALT=Barcode BORDER=0>
which defines a image that must be read from barcode.aspx. The barcode.aspx page must then generate the barcode image in the following way:
[C#]
<%@ Page language="c#" AutoEventWireup="false" Trace="false" Debug="false" %>
<%@Import Namespace="System.Drawing" %>
<%@Import Namespace="System.IO" %>
<%@Import Namespace="System.Drawing.Imaging" %>
<%@Import Namespace="J4L.QRCode" %>
<%@ OutputCache Duration="100" VaryByParam="none" %>
<%
// define variable
QRCodeWeb bc;
// create instance of the object
bc = new QRCodeWeb();// set barcode properties
bc.Code="12345678";
...// create in memory image
Bitmap inMemoryImage = new Bitmap( 200,200);
Graphics g = Graphics.FromImage(inMemoryImage);
// paint barcode
bc.getQRCode().Size=new Size(300,300);
bc.getQRCode().paint(g);MemoryStream tempStream = new MemoryStream();
// output image to the memory stream in gif format
inMemoryImage.Save(tempStream,ImageFormat.Gif);Response.ClearContent();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/gif";
Response.BinaryWrite(tempStream.ToArray());
Response.End();
%>[VBNET]
<%@ Page language="VB" AutoEventWireup="false" Trace="false" Debug="false" %>
<%@Import Namespace="System.Drawing" %>
<%@Import Namespace="System.IO" %>
<%@Import Namespace="System.Drawing.Imaging" %>
<%@Import Namespace="J4L.QRCode" %>
<%@ OutputCache Duration="100" VaryByParam="none" %>
<%
' define variable
dim bc as QRCodeWeb = new QRCodeWeb()' set barcode properties
bc.Code="12345678"
...' create in memory image
dim inMemoryImage as Bitmap= new Bitmap( 200,200)
dim g as Graphics = Graphics.FromImage(inMemoryImage)
' paint barcode
bc.getQRCode().Size=new Size(300,300);
bc.getQRCode().paint(g)dim tempStream as MemoryStream = new MemoryStream()
' output image to the memory stream in gif format
inMemoryImage.Save(tempStream,ImageFormat.Gif)Response.ClearContent()
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "image/gif"
Response.BinaryWrite(tempStream.ToArray())
Response.End()
%>
Important Note:
In order to properly print a QRCode symbol embedded in a HTML page you must use the <IMG> tag. Note that you will need to use the attibutes height and width in order to achieve the correct size of the symbol on the printed page.
This is a simple HTML page that contains a QRCode symbol:
<HTML>
<HEAD><TITLE>Servlet Example META http-equiv=Content-Type content="text/html; charset=windows-1252">
</HEAD>
<BODY bgColor=#ffffff>This is your QRCode:
<IMG height=100 width=100 src="qrcode.aspx" ></BODY>
</HTML>
Annex A
Table — Data capacity for QRCode versions
Version Error Correction LevelNumeric
Alphanumeric
Byte
Kanji
1
L
M
Q
H41
34
27
1725
20
16
1017
14
11
710
8
7
42
L
M
Q
H77
63
48
3447
38
29
2032
26
20
1420
16
12
83
L
M
Q
H127
101
77
5877
61
47
3553
42
32
2432
26
20
154
L
M
Q
H187
149
111
82114
90
67
5078
62
46
3448
38
28
215
L
M
Q
H255
202
144
106154
122
87
64106
84
60
4465
52
37
276
L
M
Q
H322
255
178
139195
154
108
84134
106
74
5882
65
45
367
L
M
Q
H370
293
207
154224
178
125
93154
122
86
6495
75
53
398
L
M
Q
H461
365
259
202279
221
157
122192
152
108
84118
93
66
529
L
M
Q
H552
432
312
235335
262
189
143230
180
130
98141
111
80
6010
L
M
Q
H652
513
364
288395
311
221
174271
213
151
119167
131
93
7411
L
M
Q
H772
604
427
331468
366
259
200321
251
177
137198
155
109
8512
L
M
Q
H883
691
489
374535
419
296
227367
287
203
155226
177
125
9613
L
M
Q
H1022
796
580
427619
483
352
259425
331
241
177262
204
149
10914
L
M
Q
H1101
871
621
468667
528
376
283458
362
258
194282
223
159
12015
L
M
Q
H1250
991
703
530758
600
426
321520
412
292
220320
254
180
13616
L
M
Q
H1408
1082
775
602854
656
470
365586
450
322
250361
277
198
15417
L
M
Q
H1548
1212
876
674938
734
531
408644
504
364
280397
310
224
17318
L
M
Q
H1725
1346
948
7461046
816
574
452718
560
394
310442
345
243
19119
L
M
Q
H1903
1500
1063
8131153
909
644
493792
624
442
338488
384
272
20820
L
M
Q
H2061
1600
1159
9191249
970
702
557858
666
482
382528
410
297
23521
L
M
Q
H2232
1708
1224
9691352
1035
742
587929
711
509
403572
438
314
24822
L
M
Q
H2409
1872
1358
10561460
1134
823
6401003
779
565
439618
480
348
27023
L
M
Q
H2620
2059
1468
11081588
1248
890
6721091
857
611
461672
528
376
28424
L
M
Q
H2812
2188
1588
12281704
1326
963
7441171
911
661
511721
561
407
31525
L
M
Q
H3057
2395
1718
12861853
1451
1041
7791273
997
715
535784
614
440
33026
L
M
Q
H3283
2544
1804
14251990
1542
1094
8641367
1059
751
593842
652
462
36527
L
M
Q
H3517
2701
1933
15012132
1637
1172
9101465
1125
805
625902
692
496
38528
L
M
Q
H3669
2857
2085
15812223
1732
1263
9581528
1190
868
658940
732
534
40529
L
M
Q
H3909
3035
2181
16772369
1839
1322
10161628
1264
908
6981002
778
559
43030
L
M
Q
H4158
3289
2358
17822520
1994
1429
10801732
1370
982
7421066
843
604
45731
L
M
Q
H4417
3486
2473
18972677
2113
1499
11501840
1452
1030
7901132
894
634
48632
L
M
Q
H4686
3693
2670
20222840
2238
1618
12261952
1538
1112
8421201
947
684
51833
L
M
Q
H4965
3909
2805
21573009
2369
1700
13072068
1628
1168
8981273
1002
719
55334
L
M
Q
H5253
4134
2949
23013183
2506
1787
13942188
1722
1228
9581347
1060
756
59035
L
M
Q
H5529
4343
3081
23613351
2632
1867
14312303
1809
1283
9831417
1113
790
60536
L
M
Q
H5836
4588
3244
25243537
2780
1966
15302431
1911
1351
10511496
1176
832
64737
L
M
Q
H6153
4775
3417
26253729
2894
2071
15912563
1989
1423
10931577
1224
876
67338
L
M
Q
H6479
5039
3599
27353927
3054
2181
16582699
2099
1499
11391661
1292
923
70139
L
M
Q
H6743
5313
3791
29274087
3220
2298
17742809
2213
1579
12191729
1362
972
75040
L
M
Q
H7089
5596
3993
30574296
3391
2420
18522953
2331
1663
12731817
1435
1024
784