Products Technologies Demo Docs Blog Support Company

Rendering Barcode Objects in ASP.NET

TX Barcode .NET can be used in ASP.NET web applications to render barcode images. This sample shows how to use a WebForms Image control to render a barcode image that is created code-behind. The ASPX page is very simple and consists of a WebForms Image control, a label to show the raw text and a refresh button: In the Page_Load event, the current date and time measured in ticks is passed as a QueryString to the ImageUrl property of the Image control. The URL is another ASPX page that uses TX…

Rendering Barcode Objects in ASP.NET

TX Barcode .NET can be used in ASP.NET web applications to render barcode images. This sample shows how to use a WebForms Image control to render a barcode image that is created code-behind.

Rendering barcode objects in ASP.NET

The ASPX page is very simple and consists of a WebForms Image control, a label to show the raw text and a refresh button:

<div>
    <asp:Image ID="Barcode1" runat="server" />
    <asp:Label ID="lblBarcodeText" runat="server" Text=""></asp:Label>
    <asp:Button ID="btnRefresh" runat="server" Text="Refresh" />
</div>

In the Page_Load event, the current date and time measured in ticks is passed as a QueryString to the ImageUrl property of the Image control. The URL is another ASPX page that uses TX Barcode .NET to create the barcode image.

protected void Page_Load(object sender, EventArgs e)
{
    // set the barcode text
    string sBarcodeText = DateTime.Now.Ticks.ToString();

    // set the URL to the ASPX page that returns the image data
    Barcode1.ImageUrl = "CreateBarcode.aspx?text=" + sBarcodeText;
    lblBarcodeText.Text = sBarcodeText;
}

In the Page_Load event of CreateBarcode.aspx, TX Barcode .NET is used to save the encoded string as a barcode image into a MemoryStream. This stream is returned using Response.BinaryWrite.

protected void Page_Load(object sender, EventArgs e)
{
    // create a new TXBarcodeControl
    TXTextControl.Barcode.TXBarcodeControl barcode =
      new TXTextControl.Barcode.TXBarcodeControl();
    barcode.CreateControl();

    // set the barcode type and size
    barcode.BarcodeType = TXTextControl.Barcode.BarcodeType.MicroPDF;
    barcode.UpperTextLength = 40;
    barcode.Width = 500;

    // set the text from the QueryString
    barcode.Text = Request.QueryString["text"];

    // save the barcode to an image MemoryStream
    MemoryStream stream = new MemoryStream();
    barcode.SaveImage(stream, System.Drawing.Imaging.ImageFormat.Png);

    // send back the image
    Response.Clear();
    Response.ContentType = "image/png";
    Response.BinaryWrite(stream.ToArray());
    Response.Flush();
    Response.Close();
    Response.End();
}

The HTML image element loads the image data dynamically from the CreateBarcode.aspx page. Every time, the Refresh button is clicked, the barcode is updated with the current date and time.

Download the sample from GitHub and test it on your own.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2012 or better
  • TX Text Control .NET Server (trial sufficient)

Related Posts

ASP.NETDockerGitHub

Creating an ASP.NET Core Web App with Docker Support and GitHub Packages

This article shows how to create an ASP.NET Core Web App with Docker support. The TX Text Control NuGet packages are hosted on GitHub Packages to get restored during the build process.


ASP.NETWindows FormsWPF

Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub

This article gives a quick overview of the new repositories, their structure and our plans for the future.


ASP.NETCIGitHub

Preparing Projects for GitHub Actions

Software workflow automation allows an ongoing and continuous monitoring throughout the development, code review, branch management and deployment of software projects. This article shows how to…


ASP.NETReportingGitHub

ASP.NET MVC: Implementing a Simplistic, Custom Button Bar

For some applications, the fully-featured ribbon bar might be too overloaded with features or the ribbon concept is not required in a project. Programmatically, all ribbon tabs, groups and buttons…


ASP.NETReportingGitHub

ASP.NET MVC: Adding Protected Sections to Documents

A SubTextPart object represents a user-defined range of text in a TX Text Control document. A SubTextPart is basically a range of text with a Name and an ID property to store additional…