Documentation - News: On Air. Online. On Demand.



Get Started with Barcodes & QRs in C# & VB.Net Applications

Reading and writing barcodes in C# and all other .NET languages is easy using the Iron Barcode software library from Iron Software.

Install IronBarcode

The first step in the journey will be installing Iron Barcode, which may be achieved by using our NuGet package or by downloading the DLL from this website. of the Iron Barcode classes can be found in the IronBarcode namespace.

The easiest way to install IronBarcode is using the NuGet Package Manager for Visual-Studio: The package name is “Barcode”.

 PM > Install-Package Barcode

Reading a Barcode or QR Code

Reading a barcode with Iron Barcode takes simply takes a few lines of code.

  1. BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("QuickStart.jpg");
  2. if(Result != null){
  3. Console.WriteLine(Result.Text)
  4. }

To improve speed and accuracy of reading barcodes, we can specify a specific barcode format (or formats) so that Iron Barcode doesn’t have to try for every single type of barcode known. We may also pick a specific crop region or area of an image to look in, if that is known.

  1. ReadArea = new System.Drawing.Rectangle(100,200,300,400);
  2. BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("QuickStart.jpg", ReadArea, BarcodeEncoding.QRCode | BarcodeEncoding.Code128);
  3. if(Result != null){
  4. Console.WriteLine(Result.Text)
  5. }

We may also choose to scan for multiple barcodes in a simple scan.

  1. BarcodeResult[] Results = BarcodeReader.QuiclyReadAllBarcodes("MultipleBarcodes.png");
  2. // Work with the results
  3. foreach (BarcodeResult Result in Results)
  4. {
  5. string Value = Result.Value;
  6. Bitmap Img = Result.BarcodeImage;
  7. BarcodeEncoding BarcodeType = Result.BarcodeType;
  8. byte[] Binary = Result.BinaryValue;
  9. Console.WriteLine(Result.Value);
  10. }

We may also use Iron Barcode to read barcodes from one or multiple pages of PDF documents or multipage TIFFs.

  1. PagedBarcodeResult[] PagedResults = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf");
  2. // Work with the results
  3. foreach (BarcodeResult Result in PagedResults)
  4. {
  5. int PageNumb = Result.PageNumber;
  6. string Value = Result.Value;
  7. Bitmap Img = Result.BarcodeImage;
  8. BarcodeEncoding BarcodeType = Result.BarcodeType;
  9. byte[] Binary = Result.BinaryValue;
  10. Console.WriteLine(Result.Value);
  11. }
  12. /// or from a multi-page TIFF scan with image correction:
  13. BarcodeResult[] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.None);

Writing Barcodes

To create or write barcodes using Iron Barcode, we use the BarcodeWriter class. With BarcodeWriter, writing barcodes is extremely easy. We simply specify the barcode format and the value we wish to express, and it will create an image which may be exported as a System.Drawing.Image object or saved to a file.

  1. var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("", BarcodeEncoding.Code128);
  2. MyBarCode.SaveAsImage("MyBarCode.png");

Styling Barcodes

The visual depiction of a barcode can be manipulated very easily using Iron Barcode. The output of every BarcodeWriter operation is a generated barcode object. This generated barcode object has a Fluent API, allowing the graphical settings of the barcode to be set in a single line of code, similar to Linq.

Popular features for styling barcodes include resizing barcodes, setting margins, changing background colors, changing barcode colors, and verifying that the output barcode is still readable.

In a similar fashion, we may also add annotations such as text to a barcode in any typeface we want, or place the value of the barcode very easily under or above the barcode.

  1. var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("", BarcodeEncoding.Code128);
  2. MyBarCode.AddAnnotationTextAboveBarcode("Product URL:");
  3. MyBarCode.AddBarcodeValueTextBelowBarcode();
  4. MyBarCode.SaveAsImage("MyBarCode.png");

Exporting Barcodes as HTML

The generated barcode object also has a convenient feature to export HTML from a generated barcode. It can export a barcode as a standalone HTML document with no external assets, as a standalone HTML tag, or as a data URI.

  1. var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("", BarcodeEncoding.Code128);
  2. MyBarCode.AddAnnotationTextAboveBarcode("Product URL:");
  3. MyBarCode.AddBarcodeValueTextBelowBarcode();
  4. MyBarCode.SetMargins(100);
  5. MyBarCode.ChangeBarCodeColor(Color.Purple);
  6. MyBarCode.SaveAsPng("MyBarCode.png"); // All major image formats supported as well as PDF and HTML

Generating QR Codes

When generating QR codes using Iron Barcode, we may opt to use the QR Code Writer class instead of the BarcodeWriter class. This class gives some new and interesting features for QR code writing. It allows us to set the QR error correction level, allowing you to decide on a balance between the size of your QR code and its ease of readability.

  1. QRCodeWriter.CreateQrCode("", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf");xw

Iron Barcode also allows for QR codes to be stylized, such as having a logo graphic placed and snapped to grid in the exact center of the image. It may also be colorized so that it matches a certain brand or graphic identity.

  1. var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("", "2 Create a QR Code\\visual-studio-logo.png", 500);
  2. MyQRWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf");

Supported Barcode Formats

Iron Barcode supports a large number of commonly used barcode formats:

  • QR codes, including decorated and branded QR codes with logos and colors
  • Multi-format barcodes, such as Aztec, Data Matrix, CODE 93, and CODE 128
  • RSS Expanded Databar, UPS MaxiCode, and USPS IMb OneCode barcodes
  • Stacked linear barcodes such as RSS-14 and PDF-417
  • Conventional numerical barcode formats such as UPCA, UPCE, EAN-8, EAN-13, Codabar, ITF, MSI, and Plessey
  1. var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("", BarcodeEncoding.Code128);
  2. MyBarCode.SaveAsImage("MyBarCode.png");
  3. MyBarCode.SaveAsGif("MyBarCode.gif");
  4. MyBarCode.SaveAsHtmlFile("MyBarCode.html");
  5. MyBarCode.SaveAsJpeg("MyBarCode.jpg");
  6. MyBarCode.SaveAsPdf("MyBarCode.Pdf");
  7. MyBarCode.SaveAsPng("MyBarCode.png");
  8. MyBarCode.SaveAsTiff("MyBarCode.tiff");
  9. MyBarCode.SaveAsWindowsBitmap("MyBarCode.bmp");
  10. Image MyBarCodeImage = MyBarCode.Image;
  11. Bitmap MyBarCodeBitmap = MyBarCode.ToBitmap();
  12. string DataURL = MyBarCode.ToDataUrl();
  13. string ImgTagForHTML = MyBarCode.ToHtmlTag();
  14. byte[] PngBytes = MyBarCode.ToPngBinaryData();
  15. // Binary barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
  16. using (System.IO.Stream PdfStream = MyBarCode.ToPdfStream()){
  17. // Stream barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
  18. }
  19. MyBarCode.StampToExistingPdfPage("ExistingPDF.pdf", 1, 200, 50);

Why Choose Iron Barcode?

Iron Barcodefeatures a friendly API for developers to read and write barcodes for .NET, which optimizes for acuracy and a low error rate in real world use-cases.

The BarcodeWriter class, for example, will automatically validate and correct ‘checksums’ on UPCA and UPCE barcodes. It will also ‘zero-pad’ numbers which are too short for a specific numeric format. If your data is inappropriate for the specified data format, Iron Barcode will instruct the developer of a more appropriate barcode format which they might use.

Iron Barcode shines in the area of reading barcodes when the barcode has been scanned or has been read from a photographic image. In other words, the image is not graphically perfect and is not just simply a machine-generated screenshot.

Iron Barcode includes automatic rotation and perspective correction, correction for digital noise, and can automatically detect the type of barcode which is encoded in an image.

Moving Forward

To get more out of Iron Barcode, we encourage you to read the tutorials within this documentation section, to visit us on GitHub, and to read the .NET Object Reference in an MSDN format.

Powered by Frankly