How to convert the barcode to image in the ASP.NET Core platform?
Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create a barcode and convert the barcode to an image on the ASP.NET Core platform.
Steps to convert the barcode to an image on the ASP.NET Core platform:
- Create a new ASP.NET Core Web Application project.
- Select the Web Application pattern (Model-View-Controller) for the project.
- Install the Syncfusion.Pdf.Imaging.Net.Core NuGet package as a reference to your .NET Core application from nuget.org.
- Add a new button in the Index.cshtml as shown below.
CSHTML
@{
Html.BeginForm("CreateDocument", "Home", FormMethod.Get);
{
<div>
<input type="submit" value="Create Document" style="width:150px;height:27px" />
</div>
}
Html.EndForm();
}
- A default controller with the name HomeController.cs is added to the ASP.NET Core project. Include the following namespaces in that HomeController.cs file.
C#
using Syncfusion.Pdf.Barcode; using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
VB.NET
Imports Syncfusion.Pdf.Barcode Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Drawing
- Add a new action method CreateDocument in the HomeController.cs and include the following code snippet to convert the barcode to an image and download it.
C#
// Drawing QR Barcode
PdfQRBarcode barcode = new PdfQRBarcode();
// Set an error correction level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.Medium;
// Set XDimension
barcode.XDimension = 3;
// Set text
barcode.Text = "http://www.syncfusion.com";
// Convert to image
Stream barcodeImage = barcode.ToImage(new SizeF(300, 300));
// Download the PDF document in the browser
FileStreamResult fileStreamResult = new FileStreamResult(barcodeImage, "application/jpg");
fileStreamResult.FileDownloadName = "Barcode.jpg";
return fileStreamResult;
VB.NET
' Drawing QR Barcode
Dim barcode As PdfQRBarcode = New PdfQRBarcode()
' Set an error correction level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.Medium
' Set XDimension
barcode.XDimension = 3
' Set text
barcode.Text = "http://www.syncfusion.com"
' Convert to image
Dim barcodeImage As Stream = barcode.ToImage(New SizeF(300, 300))
' Download the PDF document in the browser
Dim fileStreamResult As FileStreamResult = New FileStreamResult(barcodeImage, "application/jpg")
fileStreamResult.FileDownloadName = "Barcode.jpg"
Return fileStreamResult
A complete working sample can be downloaded from BarcodeToImage.zip.
By executing the program, you will get the image as follows.
Take a moment to peruse the documentation where you can find other options like adding one-dimensional and two-dimensional barcodes, set location, and size to the barcode, export barcode as an image, and customizing the barcode appearance.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.
See Also: