Generate and Display Barcode Image in ASP.NET MVC

A barcode visually represents data in a machine-readable form. Usually, it contains data or information about a product or a company, encoded in the form of numbers and/or a pattern of parallel lines. In ASP.NET MVC, we can generate various types of barcodes such as Data Matrix, Aztec, Code 128, etc. In this article, we will learn how to dynamically generate and display barcode image in ASP .NET MVC application. After following the mentioned steps, we will have our own ASP.NET MVC Barcode Generator. So let’s begin.

The article shall cover the following topics:

  1. Features of ASP.NET MVC Barcode Generator
  2. C# API to Generate Barcode in ASP.NET MVC
  3. Steps to Generate and Display Barcode Image
  4. Demo ASP.NET MVC Barcode Generator
  5. Download ASP.NET MVC Barcode Generator Source Code

Features of ASP.NET MVC Barcode Generator

Our ASP.NET MVC barcode generator will have the following features.

  1. Generate the following types of barcode symbologies:
    • Code128
    • Code11
    • Code39
    • QR
    • Datamatrix
    • EAN13
    • EAN8
    • ITF14
    • PDF417
  2. Save the generated barcode image in the following formats:
    • PNG
    • JPEG
    • BMP
    • EMF
    • SVG
  3. Preview the generated barcode image.
  4. Download the generated barcode image to your local disk.

C# API to Generate Barcode in ASP.NET MVC

For generating barcode images and displaying them in the ASP.NET MVC application, we will be using the Aspose.BarCode for .NET API. It allows us to generate and recognize a wide range of 1D & 2D barcode types. Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.BarCode

How to Dynamically Generate and Display Barcode Image in ASP.NET MVC

We can generate and display a barcode image in the ASP.NET MVC application by following the steps given below:

  • Firstly, create a new project and select the ASP.NET Web Application (.NET Framework) project template.
Select the project template.

Select the project template.

  • Next, in Create a new ASP.NET Web Application dialog, choose MVC and then choose to create.
Select MVC

Select MVC

Install Aspose.BarCode for .NET

Install Aspose.BarCode for .NET

  • Next, create a new folder “Images” to save the generated barcode images.
Create Images folder.

Create Images folder.

  • Now, create a model in the “Models” folder with the name “Barcode” to store the barcode information.
  • Also, add enumeration to list the supported barcode symbologies. It can be added in a separate class under the “Models” folder, or we can add it in the Barcode.cs class file.
  • Similarly, add enumeration to list the supported image formats.
  • Next, open Views/Home/index.cshtml and replace its content with the following script.
  • Then, open the “HomeController” class and add a new action result to handle the post request.
  • After that, add a new action result to handle the image download request in the “HomeController”.
  • Finally, run the application.

Demo ASP.NET MVC Barcode Generator

The following is the demonstration of the ASP.NET MVC Barcode Generator application we have just created.

Demo ASP.NET MVC Barcode Generator

Demo ASP.NET MVC Barcode Generator

Download ASP.NET MVC Barcode Generator Source Code

You can download the complete source code of the ASP.NET MVC Barcode Generator application from GitHub.

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to generate and display a barcode image in an ASP.NET MVC application. We have also seen how to download the generated barcode image programmatically. Besides, you can learn more about Aspose.BarCode for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also