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>
view raw index.aspx hosted with ❤ by GitHub

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;
}
view raw index.aspx.cs hosted with ❤ by GitHub

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.