Generate barcode image in ASP.NET using Bytescout BarCode SDK - ByteScout

Generate barcode image in ASP.NET using Bytescout BarCode SDK

  • Home
  • /
  • Articles
  • /
  • Generate barcode image in ASP.NET using Bytescout BarCode SDK

Bytescout BarCode SDK provides both web image control and non-visual class to generate barcode as images (which can be saved as a stream or a file)

The following sample code below shows how to generate barcode image in PNG image format and output as a file attachment to the visitor from ASP.NET code:

ASP.NET (Visual Basic code)

Imports System.Drawing.Imaging

Imports Bytescout.BarCode

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
‘ Create new barcode
Dim barcode As New Barcode()

‘ Set symbology
barcode.Symbology = SymbologyType.Code39
‘ Set value
barcode.Value = “Sample”

‘ Clear http output
Response.Clear()
‘ Set the content type to PNG
Response.ContentType = “image/png”
‘ Add content type header
Response.AddHeader(“Content-Type”, “image/png”)
‘ Set the content disposition
Response.AddHeader(“Content-Disposition”, “inline;filename=result.png”)

‘ Save image to output stream
barcode.SaveImage(Response.OutputStream, ImageFormat.Png)

‘ End response
Response.End()
End Sub
End Class

ASP.NET (C# code)

using System;
using System.Data;
using System.Drawing.Imaging;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Bytescout.BarCode;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Create new barcode
Barcode barcode = new Barcode();

// Set symbology
barcode.Symbology = SymbologyType.Code39;
// Set value
barcode.Value = “Sample”;

// Clear http output
Response.Clear();
// Set the content type to PNG
Response.ContentType = “image/png”;
// Add content type header
Response.AddHeader(“Content-Type”, “image/png”);
// Set the content disposition
Response.AddHeader(“Content-Disposition”, “inline;filename=result.png”);

// Save image to output stream
barcode.SaveImage(Response.OutputStream, ImageFormat.Png);

// End response
Response.End();

}
}

Tutorials:

prev
next