How to use ThermalLabel SDK in ASP.NET or Web Services projects (MTA threading model)

You can use the ThermalLabel SDK dll in ASP.NET or Web Services to generate or print barcode labels. Suppose you want to generate barcode labels in PDF or some raster image format to save it on server disk or to display them in an ASP.NET webpage. You could do that with the following code inside the Load event of a Page:

VB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

	'Create the thermal label object
	Dim tLabel As New ThermalLabel(Neodynamic.SDK.Printing.UnitType.Inch, 4, 4)

	Dim txtItem As New TextItem(0.2, 0.2, 1, 0.5, "Thermal Label Test")
	
	Dim bcItem As New BarcodeItem(0.2, 1, 2, 1, BarcodeSymbology.Code128, "1234567890")
	'Set bars height to 1 inch 
	bcItem.BarHeight = 1
	'Set bars width to 0.01 inch 
	bcItem.BarWidth = 0.01
	
	 'Add items to the label
	tLabel.Items.Add(txtItem)
	tLabel.Items.Add(bcItem)
	
	'Create a PrintJob object and export the label to PDF
	Using pj As New PrintJob()
	    pj.ExportToPdf(tLabel, "C:\BarcodeLabel.pdf", 300)
	End Using

End Sub

C#

protected void Page_Load(object sender, EventArgs e)
{
        //Create the thermal label object
	ThermalLabel tLabel = new ThermalLabel(Neodynamic.SDK.Printing.UnitType.Inch, 4, 4);

	TextItem txtItem = new TextItem(0.2, 0.2, 1, 0.5, "Thermal Label Test");
	
	BarcodeItem bcItem = new BarcodeItem(0.2, 1, 2, 1, BarcodeSymbology.Code128, "1234567890");
	//Set bars height to 1 inch 
	bcItem.BarHeight = 1;
	//Set bars width to 0.01 inch 
	bcItem.BarWidth = 0.01;
	
	//Add items to the label
	tLabel.Items.Add(txtItem);
	tLabel.Items.Add(bcItem);
	
	//Create a PrintJob object and export the label to PDF
	using (PrintJob pj = new PrintJob())
        {
	    pj.ExportToPdf(tLabel, @"C:\BarcodeLabel.pdf", 300);
	}
}

As soon you test this code, it is likely you get this error:

“The calling thread must be STA, because many UI components require this”

The error is because ThermalLabel SDK is based on the .NET client framework classes which require an STA (Single-Threaded Apartments) Thread and ASP.NET uses a multithreaded apartment (MTA) threading model instead.

So, to use ThermalLabel SDK in MTA environments, you have two choices:

  1. The simplest solution is to set up the AspCompat attribute to “true” of the ASP.NET webpage where you are using ThermalLabel SDK.
  2. The other solution (mainly if you are not using ThermalLabel SDK inside an ASPX page but, for instance, in an ASHX HTTP Handler) is you use ThermalLabel SDK classes inside a STA Thread as shown in the following code:

    VB

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    	Dim worker As New Threading.Thread(New Threading.ThreadStart(AddressOf Me.ThermalLabelWorker))
    	worker.SetApartmentState(Threading.ApartmentState.STA)
    	worker.Name = "ThermalLabelWorker"
    	worker.Start()
    	worker.Join()
    
    End Sub
    
    Private Sub ThermalLabelWorker()
    
    	'Create the thermal label object
    	Dim tLabel As New ThermalLabel(Neodynamic.SDK.Printing.UnitType.Inch, 4, 4)
    	Dim txtItem As New TextItem(0.2, 0.2, 1, 0.5, "Thermal Label Test")
    	
    	Dim bcItem As New BarcodeItem(0.2, 1, 2, 1, BarcodeSymbology.Code128, "1234567890")
    	'Set bars height to 1 inch 
    	bcItem.BarHeight = 1
    	'Set bars width to 0.01 inch 
    	bcItem.BarWidth = 0.01
    	
    	 'Add items to the label
    	tLabel.Items.Add(txtItem)
    	tLabel.Items.Add(bcItem)
    	
    	'Create a PrintJob object and export the label to PDF
    	Using pj As New PrintJob()
    	    pj.ExportToPdf(tLabel, "C:\BarcodeLabel.pdf", 300)
    	End Using
    
    End Sub
    

    C#

    protected void Page_Load(object sender, EventArgs e)
    {
            Thread worker = new Thread(new ThreadStart(this.ThermalLabelWorker));
            worker.SetApartmentState(ApartmentState.STA);
            worker.Name = "ThermalLabelWorker";
            worker.Start();
            worker.Join();
    }
    
    private void ThermalLabelWorker()
    {
            //Create the thermal label object
    	ThermalLabel tLabel = new ThermalLabel(Neodynamic.SDK.Printing.UnitType.Inch, 4, 4);
    
    	TextItem txtItem = new TextItem(0.2, 0.2, 1, 0.5, "Thermal Label Test");
    	
    	BarcodeItem bcItem = new BarcodeItem(0.2, 1, 2, 1, BarcodeSymbology.Code128, "1234567890");
    	//Set bars height to 1 inch 
    	bcItem.BarHeight = 1;
    	//Set bars width to 0.01 inch 
    	bcItem.BarWidth = 0.01;
    	
    	//Add items to the label
    	tLabel.Items.Add(txtItem);
    	tLabel.Items.Add(bcItem);
    	
    	//Create a PrintJob object and export the label to PDF
    	using (PrintJob pj = new PrintJob())
            {
    	    pj.ExportToPdf(tLabel, @"C:\BarcodeLabel.pdf", 300);
    	}
    }
    

ThermalLabel SDK 4.0 Updated – Landscape Print Orientation bug fixed

Our team just released a new maintenance release of ThermalLabel SDK 4.0 containing the following bug fixes:

  • Some items like the LineShapeItem object was not correctly printed when using Landscape90 or Landscape270 print orientation options.

The new version numbers of Neodynamic.SDK.ThermalLabel.dll is now as follows:

  • File Version: 4.0.3500.9
  • Assembly Version: 4.0.3500.0 (unchanged)

Update your system now by downloading and installing it from our website or using the link found in the license email you have received.

Barcode Professional for ASP.NET in Shared Hosting Services with Medium-Trust level settings

If you are using Barcode Professional for ASP.NET dll i.e. Neodynamic.WebControls.BarcodeProfessional.dll in your ASP.NET website and you host it in a server which is configured for Medium-Trust level (like the one offered by GoDaddy.com Shared Hosting servers), then the barcode dll you used for development will not work on such scenario. That’s because by default, you add a reference to our barcode dll which was not designed for trust level restriction.

When you install Barcode Professional for ASP.NET in your dev machine, the installer package provides a couple of Neodynamic.WebControls.BarcodeProfessional.dll files and one of them was designed and built to be used in Medium-Trust level websites. By default, such dll can be found at [BarcodeProInstallDir]\Redistributable\For .NET 2.0-3.5-4.0 (Medium-Trust)
So, what you need to do is just to deploy that dll in the BIN folder of your website hosted in the shared server.

Barcode Professional products line Updated – GS1-128 improved, PDF417 Auto-encoding bug fixed

Today we published a maintenance release for most of our Barcode Professional products. The new builds contain the following updates:

  • Improved the GS1-128 (formerly EAN/UCC-128) barcode algorithm when doing AIs concatenation.
  • Fixed a bug in the Auto encoding mode on PDF-417 barcode.

You can download and update the products by using their download links which can be found in the license email or in the Download section of our website.
The “Assembly Version” of the dlls have not changed but the “File Version”. Here is the list of all new “File version” attributes depending on the Barcode Professional edition:

  • Barcode Professional SDK for .NET patched to v3.0.2000.8
  • Barcode Professional for Windows Forms .NET patched to v7.0.2000.11
  • Barcode Professional for ASP.NET patched to v7.0.2000.7
  • Barcode Professional for WPF patched to v4.0.3500.6
  • Barcode Professional for Silverlight patched to v2.0.40.3
  • Barcode Professional for Reporting Services patched to v7.0.2000.7

ThermalLabel SDK 4.0 Updated – new WPF samples, GS1-128 (EAN/UCC-128), PDF-417 bug fixes

Our team just released a new maintenance release of ThermalLabel SDK 4.0 containing the following updates:

  • Added new WPF (Windows Presentation Framework) samples reproducing the same Windows Forms samples.
  • Improved the GS1-128 (formerly EAN/UCC-128) barcode algorithm when doing AIs concatenation.
  • Fixed a bug in the Auto encoding mode on PDF-417 barcode.

The new version numbers of Neodynamic.SDK.ThermalLabel.dll is now as follows:

  • File Version: 4.0.3500.8
  • Assembly Version: 4.0.3500.0 (unchanged)

Update your system now by downloading and installing it from our website or using the link found in the license email you have received.

Draw and print barcodes on high quality images in ASP.NET

Continuing with our last post about How to merge images and barcodes in ASP.NET with VB, our team has just published a new demo code about How to draw and print QR Code barcode on high quality photos or images in ASP.NET where it takes a high quality 300 dpi photo and using C# or VB, a QR Code 2D barcode is dynamically drew onto it by using our Barcode Professional for ASP.NET product. The guide also describes how to correctly size the IMG tag of the output photo image so it can be printed at high quality resolution on the user’s local printer.

How to merge images and barcodes in ASP.NET with VB

In web pages, the barcodes are generally displayed as images which are generated at server side by using technologies like ASP.NET, PHP, etc.

There’re situations where the barcode image you want to display on a web page needs to be “watermarked” or identified with your company or organization logo, so it cannot be used for other scenarios. Our Barcode Professional for ASP.NET can be used in ASP.NET web pages for displaying linear, postal and 2D barcodes; and can be merged with other images by using .NET drawing classes.

For example, recently a tech member from GS1 Mexico asked us to do that while evaluating our product. The support team wrote a .NET code in VB (which can be easily converted to C#) which take the GS1 Mexico logo and merge it with a GS1-128 (formerly EAN/UCC-128) barcode image generated by our product to render one image file only. All was implemented into an ASP.NET “General Handler” *.ashx file. Here is the sample code:




Imports System
Imports System.Web

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Imports Neodynamic.WebControls.BarcodeProfessional


Public Class GS1MexBarcodeGen : Implements IHttpHandler
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        
        'Gets the value to encode from QueryString param
        Dim valToEncode As String = context.Request("code")
        Dim texto As String = context.Request("text")
        
        'gen barcode image using Barcode Professional for ASP.NET
        Dim imgBarcode As Bitmap
        
        If String.IsNullOrEmpty(valToEncode) = False Then
            Using bcp As New BarcodeProfessional
                bcp.BarcodeUnit = BarcodeUnit.Inch
                bcp.Symbology = Symbology.GS1128
                bcp.Code = valToEncode
                bcp.BarHeight = 0.52
                bcp.BarWidth = 0.01042
                bcp.QuietZoneWidth = 0.3
                imgBarcode = bcp.GetBarcodeImage()
            End Using
        End If
        
        'Gen the output image merging the GS1 Mex logo + barcode 
        'IMPORTANT: the gs1mex_logo.jpg image must be located under root's images folder
                
        Dim imgGS1MexLogo As Bitmap = DirectCast(Image.FromFile(context.Server.MapPath("~/images/gs1mex_logo.jpg")), Bitmap)
        
        Dim szGS1MexLogo As Size = imgGS1MexLogo.Size
        
        Dim szFinal As Size = szGS1MexLogo
        
        If Not imgBarcode Is Nothing Then
            
            Dim szBarcode As Size = imgBarcode.Size
            szFinal.Height = Math.Max(szFinal.Height, szBarcode.Height)
            szFinal.Width += szBarcode.Width
            
        End If
        
        Using imgFinal As New Bitmap(szFinal.Width, szFinal.Height)
            Using gfx As Graphics = Graphics.FromImage(imgFinal)
                
                gfx.Clear(Color.White)
                
                'draw GS1 Mex logo
                gfx.DrawImage(imgGS1MexLogo, 0, 0, New Rectangle(0, 0, szGS1MexLogo.Width, szGS1MexLogo.Height), GraphicsUnit.Pixel)
                'draw barcode 
                If Not imgBarcode Is Nothing Then
                    Using imgBarcode
                        gfx.DrawImage(imgBarcode, New Point(szGS1MexLogo.Width, 0))
                    End Using
                End If
            End Using
            
            Using ms As New MemoryStream()
                imgFinal.Save(ms, ImageFormat.Png)
                ms.Position = 0
                context.Response.ContentType = "image/png"
                context.Response.BinaryWrite(ms.ToArray())
            End Using
            
        End Using
        
        
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

You can display the output image generated by this code by using a simple IMG HTML tag with the correct params, for instance:


<img src="GS1MexBarcodeGen.ashx?code=(00)350123451234567890" alt="GS1-128 barcode" style="border: 5px solid #0055ff" />

The GS1 logo merged with a GS1-128 barcode in ASP.NET

kick it on DotNetKicks.com