DEV Community

Mehr Muhammad Hamza
Mehr Muhammad Hamza

Posted on • Updated on

How to generate Barcodes in C#: Full tutorial

Why Use a Barcode Generator?

Barcodes are used in various settings, from logistics to retail, and are invaluable for helping companies keep track of inventory, streamline delivery systems, and more. Barcodes improve data accuracy and speed, reduce errors, and boost operational efficiency, highlighting their importance in organizing and managing information across diverse fields.

Today, we will be taking a dip into the topic of barcode generation, exploring various use cases in which a barcode generator is needed, and exploring the IronBarcode library. IronBarcode is a robust tool that simplifies generating barcodes in C# projects, making it easy and efficient to create, customize, and read barcodes. By creating barcodes programmatically, you can streamline the barcode generation process, therefore raising the efficiency of your workplace.

By the end of our time together today, you will have been introduced to IronBarcode, explored some of its powerful features, and be armed with the skills you need to start using it in your workspace like a pro.

Getting Started Quickly

Create a Visual Studio Project

  1. Start by creating a new project in Visual Studio.
    1. Open Visual Studio.
    2. Choose a project template (e.g., Console Application).
    3. Configure project settings (name, location).
    4. Select the target framework (e.g., .NET 7).

Visual Studio Project

Install the IronBarcode Library

Using the NuGet package manager console, enter the following command to install the barcode library:



Install-Package Barcode


Enter fullscreen mode Exit fullscreen mode

To install the library via the NuGet Package Manager window go to tools>NuGet Package Manager>Manage NuGet Packages for Solution and search for IronBarcode.
NuGet Package Manager For Solution
After installation, add the necessary namespace to your project:



using Ironbarcode;


Enter fullscreen mode Exit fullscreen mode

Generate a Barcode from a URL

Generating barcodes from a URL can be helpful in many different work environments, from inventory management such as linking an item's barcode to that product's information, or in event management when a ticket's barcode could link to a page containing all of the event information.

This example will take you through the simple process of generating a barcode from a URL and then saving it as a PNG image.



using IronarCode;

//Generates the barcode using a URL
GeneratedBarcode barcode =   BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128);

//Saves the generated barcode image as a PNG
barcode.SaveAsPng("barcode.png");


Enter fullscreen mode Exit fullscreen mode

This code uses the BarcodeWriter.CreateBarcode method to generate barcode images with the specified data and encoding type then saves the barcode image as "barcode.png".
Output - Create Barcodes

Customization and Styling

Add Annotation Text and Barcode Value to Your Barcode

You can add annotation text and barcode value to the barcode image to provide additional context.



//Generate a barcode using a URL
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128);

//Add some annotation text above the barcode image
barcode.AddAnnotationTextAboveBarcode("This is Sample Barcode");

//Add the value text below the barcode
barcode.AddBarcodeValueTextBelowBarcode();

//Save the barcode as a PNG
barcode.SaveAsPng("annotatedBarcode.png");


Enter fullscreen mode Exit fullscreen mode

The above code adds annotation text above the barcode and the barcode value below it, making the barcode more informative.
Barcode Image with Annotaion

Set Dimensions

To style the barcode, specify its width and height.



//Generate the barcode and set it's width and height
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128,600,150);
barcode.SaveAsPng("resizedBarcode.png");


Enter fullscreen mode Exit fullscreen mode

This code generates a barcode with a width of 600 and a height of 150 pixels.
Styling Barcode

Change Colors

You can also customize the background and barcode colors. This can be especially helpful when you want to improve accessibility, changing the colors of the barcode can greatly improve the contrast and make it easier to see. Another reason to change the barcode colors would be to make it more visually appealing and stand out.



using IronBarCode;

//Generate the barcode and set it's dimensions
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128,600,150);

//Change the background color
barcode.ChangeBackgroundColor(IronSoftware.Drawing.Color.DarkKhaki);
//Change the Barcode color
barcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkOrange, true);

//save the barcode
barcode.SaveAsPng("coloredBarcode.png");


Enter fullscreen mode Exit fullscreen mode

This code changes the background color to DarkKhaki and the barcode color to DarkOrange, enhancing the visual appeal.
Colored Barcode

Reading Barcodes

To read a barcode from an image, use the following code:



var resultFromFile = BarcodeReader.Read(@"barcode.png");
            foreach (var barcodeValue in resultFromFile)
            {
                Console.WriteLine(barcodeValue);
            }


Enter fullscreen mode Exit fullscreen mode

This code reads the barcode data from the image file "barcode.png" and prints the detected barcode values to the console.
Read Barcode

IronBarcode: A Closer Look

IronBarcode is a powerful .NET barcode generator library that you can harness to gain more control over barcode generation and manipulation. With this tool by your side, you will be able to create barcodes easily with just a few lines of code, have full control over the customization of the barcodes you create, and read multiple forms of barcodes.

With these strong features under your control, you will find carrying out barcode-related tasks a breeze. With this in mind, let's now take a closer look at the various aspects of this barcode C# library and how it will benefit you.
Iron Barcode

Features

As we have already discussed, IronBarcode offers a strong set of features that will help simplify barcode generation, here we will take a closer look at some of these features:

  • Various barcode generation: IronBarcode allows developers flexibility in generating multiple types of barcodes; whether you need to generate modern linear barcodes such as Code 39, older linear barcodes such as UPC-A, or 2D barcodes such as Data Matrix, the library has you covered.
  • Barcode reading: IronBarcode also excels at reading barcodes. It supports many data formats, including images, streams, and PDFs.
  • Image correction filters: When a barcode image is unreadable, you can leverage IronBarcode's image correction filters to enhance the image and make the barcode easier to read. These filters include sharpening, contrast, brightness, binary threshold, adaptive threshold, and invert.
  • Barcode customization: When you use IronBarcode, you have full control over the customization of the dimensions for the barcodes you create and can fully style barcodes.

Advanced Features

IronBarcode also supports:

  • Create QR Codes: IronBarcode supports QR code generation
  • Barcode format support: Multiple barcode formats (e.g., Data Matrix, PDF417)
  • Machine learning: Machine learning is used for improved accuracy in IronBarcode

Real-World Applications

What about using IronBarcode in real-world situations outside of just talking about it? IronBarcode is beneficial in a variety of real-world applications which we will take a closer look at below.

  • Inventory management: Use IronBarcode to streamline your inventory management systems, track inventory items, monitor items you have shipped off, and manage stock levels with ease.
  • Product labeling: Barcodes are used in any retail environment to track inventory, process sales and monitor stock levels.
  • Healthcare: Healthcare is an essential industry, and with IronBarcode, you can streamline the process of printing barcodes for patient ID bands, making it easier to keep track of patients and access their files more efficiently, manage medications, and keep track of medical equipment.
  • Ticketing systems: In ticketing systems, such as event ticketing, barcodes can streamline the entire process. If we use event tickets as an example here, you could apply a barcode to people's tickets in order to make it easy to track attendance numbers and ensure a speedy process of checking people into the event.

Speed and Efficiency

IronBarcode is optimized for speed and performance, making it a top choice for barcode generation and reading. It demonstrates superior efficiency and high performance, capable of handling demanding applications with ease. Tests have shown that IronBarcode can generate and read barcodes rapidly while maintaining low memory consumption.

All of this means that when you begin using IronBarcode in your workspace, you will see a real difference in the speed and efficiency of your barcode generation process, making it particularly helpful if you work in an industry that relies on barcodes in any capacity.

Error Handling and Troubleshooting

Errors and issues are common in the programming space, and while sometimes these errors can be more complex and require more research to fix, sometimes it's as simple as a missing reference or closing bracket. Here are some common errors and their fixes:

  • Missing DLL Reference: Ensure IronBarcode.Dll is correctly referenced.
  • Incorrect Barcode Encoding: Verify the encoding type matches your data.
  • Read Errors: Ensure the barcode image is clear and properly scanned.

Cross-Platform Compatibility

When it comes to working across different platforms and systems, IronBarcode boasts strong cross-platform compatibility features. No matter what environment you work in, there's a good chance that IronBarcode has the support for it.

Let's take a look at an overview of this support:

  • IronBarcode offers strong compatibility within various .NET environments such as .Net 8,7, .NET Core 2x & 3x, and .NET framework 4.6.2+.
  • It also works with various operating systems including Linux, MacOS, and Windows.
  • .NET languages such as C#, VB.NET, F#

Licensing

IronBarcode requires a License key in order to work. If you want to try IronBarcode out for yourself before committing to buying a license, then you'll be sorted with the free trial offered by IronBarcode.

If you are ready to begin using IronBarcode to elevate your barcode tasks today, then it offers a range of license pricing tiers and extras, starting from $749. These tiers all come with their own benefits and features, but every tier comes with support from our expert developers and the full use of the extensive features within IronBarcode.

License & Pricing of Iron Barcode

To get started with your license key, you simply need to add it to your project as seen in the following line:



//Replace the license key variable with your own license key
IronBarCode.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";

Enter fullscreen mode Exit fullscreen mode




Conclusion

Now that we've come to the end of our deep dive into barcode generation and IronBarcode, you can take what we have discussed today to elevate your barcode creation tasks to the next level! Whether you already know the topics that we have discussed today or are a complete beginner, you'll be ready to start creating barcodes on the go in your .NET applications.

When you feel ready to dive into more advanced features offered by IronBarcode, we offer plenty of how-to guides and documentation, so we have you covered no matter what you are trying to accomplish.

Top comments (0)