Barcode in C#
Here we will show how to create barcode images in GIF/BMP/JPG formats and how to create a printable report-like page that contains a barcode with StrokeScribe barcode generator in a C# application.
How to Proceed
- Download and install the barcode generator;
- Create a C# console application;
- Add a reference to the StrokeScribeClass into your project;
- Paste one of the following code samples into the project.
How to Add the Barcode Generator into a C# Project
1. Execute Project->Add Reference from the Visual Studio menu:
2. Go to the COM tab and select the StrokeScribe Class.
The console application, by default, doesn't have a reference to the System.Drawing namespace - you need to add it manually. This reference is required to test the barcode printing example.
How to Save a Barcode Image into a File in C#
The application creates a barcode with the width:height proportion of 2:1.
The BitmapW property returns the minimum picture width in pixels that fits the barcode.
For best looking barcode images images, always set the image width as a multiple of the value reported by the BitmapW.
1. Paste the following code into your project:
using STROKESCRIBECLSLib;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StrokeScribeClass ss = new StrokeScribeClass();
ss.Alphabet = enumAlphabet.CODE128; // Or enumAlphabet.EAN13 or enumAlphabet.CODE39
ss.Text = "123ABCDEF"; // Any text to encode in the barcode
int width = ss.BitmapW; // The minimum barcode width in pixels
int rc = ss.SavePicture("code128.bmp", enumFormats.BMP, width*2, width); // The barcode W:H ratio of 2:1. Each bar has the width of 2px.
if (rc != 0)
System.Console.Write(ss.ErrorDescription);
}
}
}
2. Build and run your application.
3. Look for the code128.bmp in the project folder. The barcode will look as shown on the picture:
Making Picture Files of 2D Barcodes
For 2D barcodes (QR COde, Data Matrix, PDF417), use the following code:
ss.Alphabet = enumAlphabet.QRCODE;
int rc = ss.SavePicture("qrcode.jpg", enumFormats.JPG, ss.BitmapW, ss.BitmapH); //A QR CODE image with 1x1 pixel modules
Or
ss.Alphabet = enumAlphabet.PDF417;
int rc = ss.SavePicture("pdf417.gif", enumFormats.GIF, ss.BitmapW, ss.BitmapH);
Making Vector-based drawing of Barcodes
For vector based barcode pictures in EMF/WMF formats, you may specify any values for width and height.
WMF format uses TWIPs (1440 per inch):
ss.Alphabet = enumAlphabet.QRCODE;
int rc = ss.SavePicture("qrcode.wmf", enumFormats.WMF, 1440, 1440); //1x1 inch QR CODE
EMF accepts millimeters:
ss.Alphabet = enumAlphabet.DATAMATRIX;
int rc = ss.SavePicture("datamatrix.emf", enumFormats.EMF, 10, 10); //10x10mm. DATA MATRIX
How to Create a Report-like Printable Page with a Barcode in C#
This C# application prints a page with a barcode on the system's default printer. Barcodes are created as raster pictures.
This method is useful when printing barcode labels on a thermal transfer printer.
using System;
using System.Drawing;
using System.Drawing.Printing;
using STROKESCRIBECLSLib;
namespace ConsoleApplication1
{
class Program
{
static private void PrintPage(object sender, PrintPageEventArgs ev)
{
ev.HasMorePages = false;
StrokeScribeClass ss = new StrokeScribeClass();
ss.Alphabet = enumAlphabet.CODE128;
ss.Text = "ABCDEF123";
stdole.StdPicture olepicture;
olepicture = ss.GetPictureHandle(enumFormats.BMP, ss.BitmapW*3, ss.BitmapW); //A barcode with width:height ratio of 3:1
if (ss.Error!=0)
{
Console.Write(ss.ErrorDescription);
return;
}
Image img;
img = Image.FromHbitmap((IntPtr)olepicture.Handle);
ev.Graphics.DrawImage(img, ev.MarginBounds.Left, ev.MarginBounds.Top);
}
static void Main(string[] args)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(PrintPage);
pd.Print();
}
}
}
Vector-based Barcode Printing in C#
We modified the previous example to print barcodes as vector graphics.
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using STROKESCRIBECLSLib;
namespace ConsoleApplication1
{
class Program
{
static private void PrintPage(object sender, PrintPageEventArgs ev)
{
ev.HasMorePages = false;
StrokeScribeClass ss = new StrokeScribeClass();
ss.Alphabet = enumAlphabet.QRCODE;
ss.Text = "ABC";
dynamic arr = ss.GetPictureArray(enumFormats.WMF, 1440, 1440); //1440 twips = 1 inch
if (ss.Error != 0)
{
Console.Write(ss.ErrorDescription);
return;
}
MemoryStream ms = new MemoryStream(arr);
Image img;
img = Image.FromStream(ms);
ev.Graphics.DrawImage(img, ev.MarginBounds.Left, ev.MarginBounds.Top);
}
static void Main(string[] args)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(PrintPage);
pd.Print();
}
}
}
Related Tutorials
© 2026 StrokeScribe. All rights reserved. Use of any portion of this site constitutes acceptance of our Terms of Use and Privacy Policy. The website material may not be reproduced, except with the prior written permission.