Barcode Styling & Annotation

C# + VB.Net: Barcode Styling & Annotation Barcode Styling & Annotation
using System;
using IronBarCode;
using System.Drawing;  // Include a reference to System.Drawing.Dll


/*** STYING GENERATED BARCODES  ***/

// BarcodeWriter.CreateBarcode creates a GeneratedBarcode object which allows the barcode to be styled and annotated.
GeneratedBarcode MyBarCode = BarcodeWriter.CreateBarcode("Iron Software", BarcodeWriterEncoding.QRCode); 

// Any text (or commonly, the value of the barcode) can be added to the image in a default or specified font.
// Text positions are automatically centered, above or below.  Fonts that are too large for a given image are automatically scaled down.
MyBarCode.AddBarcodeValueTextBelowBarcode();
MyBarCode.AddAnnotationTextAboveBarcode("This is My Barcode", new Font(new FontFamily("Arial"), 12, FontStyle.Regular, GraphicsUnit.Pixel), Color.DarkSlateBlue);

// Resize, add Margins and Check final Image Dimensions
MyBarCode.ResizeTo(300, 300); // pixels
MyBarCode.SetMargins(0, 20, 0, 20);

int FinalWidth = MyBarCode.Width;
int FinalHeight = MyBarCode.Height;

//Recolor the barcode and its background
MyBarCode.ChangeBackgroundColor(Color.LightGray);
MyBarCode.ChangeBarCodeColor(Color.DarkSlateBlue);
if (!MyBarCode.Verify())
{
    Console.WriteLine("Color contrast should be at least 50% or a barcode may become unreadable.  Test using GeneratedBarcode.Verify()");
}

// Finally save the result
MyBarCode.SaveAsHtmlFile("StyledBarcode.html");


/*** STYING BARCODES IN A SINGLE LINQ STYLE EXPRESSION ***/

// Fluent API
BarcodeWriter.CreateBarcode("https://ironsoftware.com", BarcodeWriterEncoding.Aztec).ResizeTo(250, 250).SetMargins(10).AddBarcodeValueTextAboveBarcode().SaveAsImage("StyledBarcode.png");


/*** STYING QR CODES WITH LOGO IMAGES OR BRANDING ***/

// Use the QRCodeWriter.CreateQrCodeWithLogo Method instead of BarcodeWriter.CreateBarcode
// Logo will automatically be sized appropriately and snapped to the QR grid.

GeneratedBarcode QRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://visualstudio.microsoft.com/", "visual-studio-logo.png");
QRWithLogo.ResizeTo(500, 500).SetMargins(10).ChangeBarCodeColor(Color.DarkGreen);
QRWithLogo.SaveAsPng("QRWithLogo.Png").SaveAsPdf("MyVerifiedQR.html"); // save as 2 formats 
Imports System
Imports IronBarCode
Imports System.Drawing ' Include a reference to System.Drawing.Dll


'''* STYING GENERATED BARCODES  **

' BarcodeWriter.CreateBarcode creates a GeneratedBarcode object which allows the barcode to be styled and annotated.
Private MyBarCode As GeneratedBarcode = BarcodeWriter.CreateBarcode("Iron Software", BarcodeWriterEncoding.QRCode)

' Any text (or commonly, the value of the barcode) can be added to the image in a default or specified font.
' Text positions are automatically centered, above or below.  Fonts that are too large for a given image are automatically scaled down.
MyBarCode.AddBarcodeValueTextBelowBarcode()
MyBarCode.AddAnnotationTextAboveBarcode("This is My Barcode", New Font(New FontFamily("Arial"), 12, FontStyle.Regular, GraphicsUnit.Pixel), Color.DarkSlateBlue)

' Resize, add Margins and Check final Image Dimensions
MyBarCode.ResizeTo(300, 300) ' pixels
MyBarCode.SetMargins(0, 20, 0, 20)

Dim FinalWidth As Integer = MyBarCode.Width
Dim FinalHeight As Integer = MyBarCode.Height

'Recolor the barcode and its background
MyBarCode.ChangeBackgroundColor(Color.LightGray)
MyBarCode.ChangeBarCodeColor(Color.DarkSlateBlue)
If Not MyBarCode.Verify() Then
	Console.WriteLine("Color contrast should be at least 50% or a barcode may become unreadable.  Test using GeneratedBarcode.Verify()")
End If

' Finally save the result
MyBarCode.SaveAsHtmlFile("StyledBarcode.html")


'''* STYING BARCODES IN A SINGLE LINQ STYLE EXPRESSION **

' Fluent API
BarcodeWriter.CreateBarcode("https://ironsoftware.com", BarcodeWriterEncoding.Aztec).ResizeTo(250, 250).SetMargins(10).AddBarcodeValueTextAboveBarcode().SaveAsImage("StyledBarcode.png")


'''* STYING QR CODES WITH LOGO IMAGES OR BRANDING **

' Use the QRCodeWriter.CreateQrCodeWithLogo Method instead of BarcodeWriter.CreateBarcode
' Logo will automatically be sized appropriately and snapped to the QR grid.

Dim QRWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://visualstudio.microsoft.com/", "visual-studio-logo.png")
QRWithLogo.ResizeTo(500, 500).SetMargins(10).ChangeBarCodeColor(Color.DarkGreen)
QRWithLogo.SaveAsPng("QRWithLogo.Png").SaveAsPdf("MyVerifiedQR.html") ' save as 2 formats

In this sample, we see that barcodes may be annotated with text of your choosing or the barcode’s own value using any typeface which is installed on the target machine. If that typeface is not available, an appropriate similar typeface will be chosen. Barcodes may be resized, have margins added, and both the barcode and the background may be recolored. They may then be saved as an appropriate format.

In the final few lines of code, you can see that using our fluent style operators, it’s possible to create and style a barcode in only a few lines of code, similar to System.Linq.