How to create, print barcode image in C# ASP.NET, MVC,
Windows app?


How to generate linear, 2d barcode images in C# asp.net, WPF with C# source code

Create, Print barcode labels in C# ASP.NET Web, WinForms, WPF desktop application with free C# source code project



In this C# tutorial, you learn how to generate barcode images in ASP.NET web and Windows applications using C#.

  • Integrate C# barcode library into ASP.NET Core, ASP.NET Framework, MVC, web applications
  • Integrate C# barcode library into .NET Core Winodws Forms, WPF, console Windows application
  • Creating QR Code, Data Matrix, PDF-417 2d barcodes, also encoding Code 128, Code 39, GS1-128, EAN/UPC linear barcode formats in C# applications
  • Encode digits, alpha-numeric text into barcode, also encoding complex data such as non-printable chars, Unicode text, GS1, byte array data
  • Easy to customize barcode dimension width and height, 1d barcode text labels and advanced image options
  • Generate styled QR Code with detailed C# demo source code
  • Easy to create barcode in memory, graphics object or output to file in BMP, JPG, PNG, TIFF, EPS, SVG format

How to create linear, 2d barcodes in ASP.NET and Windows application using C#

  1. Download .NET Barcode Generator Suite
  2. Install C# library to create barcodes in .NET Projects
  3. Step by Step Tutorial
















Download & install C# Barcode Generator library to .NET projects

Top

Install through NuGet

For .NET Core (Class library, Console, Windows Forms, WPF) apps on Windows
PM > Install-Package OnBarcode.Barcode.Generator


Download DLL

OnBarcode C# Barcode Generator Suite SDK trial version is free to download. You can manually download it here:
Download C# Barcode Generator Suite SDK trial version

Install DLL

For .NET Core apps on Windows
  • Add OnBarcode.Barcode.Common.dll and OnBarcode.Barcode.ASPNETCore.dll (for ASP.NET Core web streaming only) from folder "{downloaded package}/dll/NetStandard2.0/" to .NET project reference.
  • You need manually add System.Drawing.Common from "NuGet Package Manager" to .NET Core project reference.

You can view more detailed install guide here
How to install C# barcode nuget package or dll on your .NET 5+, Framework web and desktop applications?

We also have provided step by step barcode library setup tutorials for .NET projects.




How to create barcodes in C# class using C# Barcode Generator library?

Top
Using C# barcode library, you can easily create, customize the barcode and save to a hard-copy of the image file, or print to memory for further processing.


Print a QR Code to an image file

Here we will see how to quickly generate a simple QR Code image file with 3 lines of C# code using OnBarcode C# Barcode Suite.
  • Firstly, create a new QRCode object, which allows you to create a QR Code barcode image with customized settings.
  • Set the QR Code encoding data as "QR Code"
  • Call method drawBarcode() to create a new QR Code image file using C# code
            QRCode barcode = new QRCode();
            barcode.Data = "QR Code";
            barcode.drawBarcode("C://Projects//Barcode-OnBarcode//Output//qrcode-quick-create.png");

Here is the QR Code generated using the above C# codes.


Using C# Barcode library, you can easily generate and customize barcode and output to raster (PNG, Bitmap, JPEG) and vector (SVG, EPS) images.

View more details here: Create barcode in PNG, BMP raster images and SVG, EPS vector images


Create a Code 128 barcode in Stream object

The following C# source code example shows how to quickly create a simple Code 128 barcode with digits and text content using 4 lines of C# code in your .NET project.
  • Create a new Linear object, which allows you to create any 1d/linear barcode type
  • Set the barcode data as "ABC-123-abc"
  • Set the barcode object's barcode type as "BarcodeType.CODE128"
  • Create a new Stream object, which will store the Code 128 barcode image data
  • Call method drawBarcode() to create a new Code 128 barcode and save the barcode to the Stream object
            Linear barcode = new Linear();
            barcode.Data = "ABC-123-abc";
            barcode.Type = BarcodeType.CODE128;

            Stream barcodeInStream = new MemoryStream();
            barcode.drawBarcode(barcodeInStream);

Here is the Code 128 generated using the above C# codes.


Create a barcode, and draw to a Graphics object

You can also easily create a barcode and paint the barcode to an existing Graphics object.

            Linear barcode = new Linear();
            barcode.Data = "Code 128";
            barcode.Type = BarcodeType.CODE128;
            barcode.BarcodeWidth = 200;
            barcode.BarcodeHeight = 150;

            Bitmap bitmap = new Bitmap(200, 150);
            Graphics barcodeInGraphics = Graphics.FromImage(bitmap);

            barcode.drawBarcode(barcodeInGraphics);

            bitmap.Save("C://Output//barcode-sample.png");


You also use the following API to print barcode on the specified position (float x, float y) on the Graphics object.
public void drawBarcode(Graphics graphics, float x, float y)


Create a barcode, draw barcode in byte array object in C#

You can also quickly create a barcode and encode the barcode data in a byte array object using C#.

            Linear barcode = new Linear();
            barcode.Data = "Code 128";
            barcode.Type = BarcodeType.CODE128;

            byte[] barcodeInBytes = barcode.drawBarcodeAsBytes();




C#.NET Barcode Generation Guides & Tutorials for Each Barcode

Top

Barcode Control for C#.NET - Bar Code Type Generation





How to create barcode with various data formats using C#?

Top
Barcode may need encode data with various data formats, such as
  • Numeric digits (0-9)
  • AlphaNumeric (0-9; Upper case letters, and some special characters)
  • ASCII characters
  • Extended ASCII characters
  • Unicode text
  • Some standards with specific requirements (Such as GS1 system)
  • Byte data to store data in special formats (Such as store an image file into QR Code barcodes)



How to generate barcode with digits, upper, lower case letters using C#?

You can easily use C# code to create barcode with simple data formats (such as digits, upper case, lower case letter, and other printable characters from ASCII, extended ASCII character sets).

In the C# codes below, you can directly provide the printable characters to the barcode object Data property, and the C# barcode generator library will automatically create the barcode from the input numeric text using C#.
            Linear code128 = new Linear();
            code128.Type = BarcodeType.CODE128;
            code128.Data = "ABC-123-abc";
            code128.drawBarcode("C://Projects//Barcode-OnBarcode//Output//code128-simple-data.png");



However you could also need generate QR Code or 1d barcodes from some special characters (such as non-printable characters, Unicode text), the C# barcode generator library has provided C# API to support it.





How to create barcode with complex data message encoded in C#?

Besides simple digits and alphanumeric characters encoding, 2d barcodes and some linear barcode (such as Code 128) supports special data encoding such as binary data, non-printable chars, Unicode, GS1 data messages.

You can view the detailed barcode data encoding here





How to store large data message on barcode using C#?

Sometimes, you need store large data message (such as video) on barcode. And most of the 2d barcode has limit to store the maximum number of characters. However most 2d barcode supports a special data encoding mode Structured Append mode. Using the data encoding mode, you can split the encoding of the data from a message over a number of 2d barcode symbols. QR Code, Data Matrix, PDF-417 (is known as Macro PDF-417) all support this data mode.

You can view the detailed barcode data encoding here
How to encode a large data message over multiple barcodes using C#?




How to create barcode with specified dimension width, height using C#?

Top
You can easily create a QR Code barcode with specified image width and height in C# using barcode library.
  • Set AutoResize to true. When the property is true, the barcode library will automatically draw the whole QR Code barcode on the specified area.
  • Set BarcodeWidth to the value of printed barcode image width
  • Set BarcodeHeight to the value of printed barcode image height
            QRCode barcode = new QRCode();
            barcode.Data = "QR Code";

            barcode.AutoResize = true;
            barcode.BarcodeWidth = 250;
            barcode.BarcodeHeight = 250;

            barcode.drawBarcode("C://Projects//Barcode-OnBarcode//Output//barcode-size.png");



You can view more options and functions to customize barcode size here
How to create and customize barcode width and height in C# ASP.NET, Windows apps?




How to customize 1d barcode text styles using C#?

Top
Using C# barcode generator, you can easily apply the 1d barcode text (HRI) styles with the following property settings:
  • Text font style. Property "TextFont" allows you to customize the text font family, size, and style using object Font.
  • Text color. Property "TextColor" allows you to set the printed text color.
  • Text space with the bar modules. Property "TextMargin" to set the space (in dpi) between barcode modules and the printed text label.
  • Show or hide the text. Property "ShowText" to display or hide the barcode text.
  • Show or hide the check sum character, if any. Property "ShowCheckSumChar" to display or hide the barcode text.

Barcode text font size, style, color settings using C#

The C# code and barcode image below explains how to customize the rendered barcode text using property "TextFont", "TextColor", and "TextMargin".

            Linear barcode = new Linear();
            barcode.Type = BarcodeType.CODE128;
            barcode.Data = "ABC-123-abc";

            barcode.TextFont = new Font("OCR-b", 9f, FontStyle.Regular);
            barcode.TextColor = Color.Red;
            barcode.TextMargin = 10;

            barcode.drawBarcode("C://Projects//Barcode-OnBarcode//Output//barcode-text-styles.png");



Barcode text show or hidden using C#

You can easily hide the linear barcode printed text using property "ShowText" in your C# code.

            Linear barcode = new Linear();
            barcode.Type = BarcodeType.CODE128;
            barcode.Data = "ABC-123-abc";

            barcode.ShowText = false;

            barcode.drawBarcode("C://Projects//Barcode-OnBarcode//Output//barcode-text-hidden.png");



Barcode check sum character show or hidden using C#

You can also choose to hide barcode check sum character in the printed barcode text using C# barcode generator library.

The following C# code will create a Code 39 barcode with check sum digit ('$') enabled and displayed in the barcode text using C# barcode generator.

            Linear barcode = new Linear();
            barcode.Type = BarcodeType.CODE128;
            barcode.Data = "ABC-123-abc";

            barcode.ShowText = false;

            barcode.drawBarcode("C://Projects//Barcode-OnBarcode//Output//barcode-text-hidden.png");



Here we will add one line of code to hide the calculated barcode check sum digit. In the barcode image below the barcode still include checksum digit, but it is not displayed in the barcode text.

            barcode.ShowCheckSumChar = false;



GS1 barcodes text font

The GS1 System recommends to use OCR-B font for digits shown under the Linear barcodes (EAN/UPC, GS1-128, ITF-14, GS1 DataBar). C# Barcode Generator library provide the property TextFont in class OnBarcode.Barcode.Linear to customize the barcode text style (human readable interpretation) using C#.

Here are free font resources you may need
OCR-B font
OCR-A font

The following C# code shows how to generate GS1-128 image with OCR-B font applied.
            Linear barcode = new Linear();
            barcode.Type = BarcodeType.EAN128;
            barcode.Data = "(00)395123451234567895(01)09501101530003";
            barcode.TextFont = new Font("OCR-B", 12f, FontStyle.Regular);
            barcode.drawBarcode("C://Output//barcode-sample-gs1-128-text.png");





How to customize barcode image settings using C#?

Top
Using C# barcode generator library, you can find that it is really easy to customize the barcode image format, settings in the C# application.
  • Image resolution
  • Image colors (support transparent background image)
  • Output to multple raster image formats (JPEG, PNG, GIF, Bitmap, TIFF)
  • Output to vector image formats (SVG & EPS)
Here we will create a simple QR Code with 300 dpi resolution, bar module in red color, space module in blue color using C# codes.

            QRCode barcode = new QRCode();
            barcode.Data = "QR Code";

            barcode.Resolution = 300;
            barcode.ForeColor = Color.Red;
            barcode.BackColor = Color.Blue;

            barcode.drawBarcode("C://Projects//Barcode-OnBarcode//Output//barcode-image-setting.png");



You can find more information here: How to apply barcode image settings in C# using C# barcode generator library?




How to create customed QR Code with logo added using C#?

Top
Sometimes you need add logo to generated QR Code for marketing, you can easily create customed QR Code with logo added using C# barcode generator.

The C# source code here will add a logo image to the center of the created QR Code. You just set the property "Logo" and "LogoActualSize" to specify the logo image location and rendered size.

            QRCode barcode = new QRCode();

            barcode.Data = "https://www.facebook.com";

            barcode.Logo = new System.Drawing.Bitmap("C://Input//facebook_logo_2021.svg.png");
            barcode.LogoActualSize = new System.Drawing.SizeF(600, 600);

            barcode.UOM = UnitOfMeasure.INCH;
            barcode.AutoResize = true;
            barcode.BarcodeWidth = 3;
            barcode.BarcodeHeight = 3;
            barcode.Resolution = 900;
            barcode.AutoResizePaddingTransparent = true;

            barcode.OutputFileFormat = FileFormat.PNG;

            barcode.drawBarcode("C://Output//barcode-styled-qrcode.png");



You can view more details about styled QR Code generation using C# barcode library here: How to create styled QR Code using C#?




Exception: Fail to Create Barcode using C#?

Top
After you have installed the barcode generator library successsfully in the .net project, you may fail to create a barcode with exception thrown.

Handle exception

The barcode software provide two methods to handle exception during barcode generation in C#. You can choose one of them through property ExceptionMode

  • ExceptionMode.ShowErrorImage (default value). The barcode library will produce an empty image with "Invalid Barcode Settings" message printed. The error image is in fixed width and height (273 pixels X 58 pixels).

  • ExceptionMode.ThrowException. The software will throw the exception directly in the C# program, and you can handle it in your code.

Why fail to create barcode

The wrong barcode option settings will fail to generate a barcode. Here are the reasons why you cannot generate a barcode using barcode software

  • Invalid barcode data length. Some barcode format needs fixed barcode data length. If you try to encode barcode with data in wrong length, the barcode generator will throw an exception.

    For example, EAN-13 barcode includes 12 data digits and 1 checksum digit. In the C# code, you can provide 12 digits or 13 digits to the barcode generator library. Otherwise the barcode software will fail to create an EAN-13 barcode and throw an exception.

  • The barcode format usually has its maximum data encoding capacity. If you try to encode a barcode with large data message, the barcode library will throw an exception.

    For example, QR Code with Version 40-L and numeric data mode can encode upto 7,089 characters in a single QR Code symboloy. If you try to encode 7,090 characters into QR Code, the barcode library will throw an exception.

  • If the encoding data message is not supported by the barcode format, the library will also throw an exception.
    The following C# code will throw an exception. Because the Code 128 Set C supports digits encoding only.
    linear.Type = BarcodeType.CODE128C;
    linear.Data = "Code 128";
    


  • In Auto Resize mode, the barcode size settings may produce exception.

    The C# code below, the option TextMargin is greater than the BarcodeHeight, and it will create a negative bar height (Bar height = barcode height - text margin - text height). It is invalid.
    linear.AutoResize = true;
    linear.BarcodeWidth = 500;
    linear.BarcodeHeight = 100;
    linear.TextMargin = 500;
    










OnBarcode is a market-leading provider of barcode imaging generator, reader controls and components for ASP.NET, Windows Forms, WPF, as well Java, Android, iOS (iPhone, iPad) across all major enterprise development platforms. We provides comprehensive tutorials and how-tos for various linear, 2d barcode information, such as C# in ASP.NET, C# .NET, C# Barcode Encoding, C# Barcode Image, VB.NET in ASP.NET, VB.NET Winforms, VB.NET Barcode Encoding. OnBarcode barcode products are supported by RasterEdge ASP.NET Document Viewer, which supports ASP.NET PDF Viewer, ASP.NET MVC Document Viewer, ASP.NET PDF Editor, MVC PDF Viewer. And provide high quality C# Convert PDF to Tiff, C# Convert PDF to Word, C# Convert PDF to HTML, C# Convert PDF to Jpeg images, and their easy and simple documents, like C# PDF SDK, C# extract text from PDF, C# Compress PDF, Print PDF in C# and C# extract image from PDF.
Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.