Saving barcodes

Generating colors

Before we start generating barcodes, we have to decide which colors we want to use to display our barcode.
Generally, we use black and white bars.


var colorFront = new BCGColor(Color.Black);
var colorBack = new BCGColor(Color.White);

Font for Label

We will now create a font for writing a label under the barcode. If you don't wish to have a text label, ignore this step.
The first argument is a System.Drawing.Font. That argument will hold the family and size of your font.


var font = new BCGFont(new Font("Arial", 18));

Creating the barcode

Now, we will create the barcode. There is no parameter for the class constructor; you must call the provided methods to modify the barcode properties (see the manual). At the end of the code, you must call the function Parse() in order to analyze the code you want to draw.


var code = new BarcodeGenerator.BCGcode128();
code.SetScale(2); //Resolution
code.SetThickness(30); // Thickness
code.SetForegroundColor(colorFront); // Color of bars
code.SetBackgroundColor(colorBack); // Color of spaces
code.SetFont(font); // Font
code.Parse("HELLO"); // Text

Saving the barcode to a file

We need to put the barcode on a drawing surface. We will use the BCGDrawing class.
The first argument is the barcode. The last argument is the background color.
To finish, call the FinishAsync method with the argument BCGDrawing.ImageFormat.Png or BCGDrawing.ImageFormat.Jpeg to have a PNG or a JPG file. Then pass in the file name as the second argument.


var drawing = new BCGDrawing(code, colorBack);
await drawing.FinishAsync(BCGDrawing.ImageFormat.Png, "D:\\hello.png");