QR Code erstellen!

FAQ: TBarCode .NET

FAQ TBarCode .NET: Caching Barcode Images in ASP.NET Applications
 

Streaming Performance

Caching Barcode Images in ASP.NET Applications

File Based Caching

The CacheDirectory property only accepts virtual paths from the web server. It is not possible to use a physical path such as c:\temp. Temporary barcode images should be created within the local (virtual) application directory (where you can find the *.aspx files) or in a subdirectory.

Cache="Harddisk" 
CacheDirectory="." or CacheDirectory="barcodefiles"

Memory Based Caching

With the following setting all barcode images will be stored in the web server's cache memory:

Cache="Memory"

In order to use memory caching you must include a custom HTTP handler BarcodeHttpHandler in the <httpHandlers> section of your web.config file (you might need to create this section first, if it does not exist). It should look similar to this:

 <system.web>
  ...
  <httpHandlers>
    ...
    <add verb="GET" path="BarcodeHttpHandler.axd" 
               type="TECIT.TBarCode.Web.BarcodeHttpHandler,
               TECIT.TBarCode, 
               Version=9.0.1.0, Culture=neutral, 
               PublicKeyToken=1B5F4306B234B83D" />
    ...
  </httpHandlers>
  ...
 </system.web>

Based on the example shown above, you probably need to change the version number depending on your package. Under c:\windows\assembly, look for TECIT.TBarCode to find the correct version.

If you need more detailed instructions go to:

> Start Menu > TEC-IT TBarCode 9.0 > Developer Manuals >TBarCode .NET Developer Reference (BarcodeHttpHandler Class)

DP Premiumadress

How to Encode Binary Data (2D Codes)

Even if you use an escape sequence for binary data the encoder will use Codepage Conversion (example: \x88 leads to a different value in Data Matrix as desired).

Solution:

  • Switch off codepage conversion (use Encoding Mode LowByte)
  • Force binary encoding mode if available (see PDF417 Encoding Mode, Data Matrix EnforceBinaryEncoding)
Relevant for DP Premium Address and other applications where you encode binary data in a 2D symbol (Data Matrix etc).

Web Farms

Using TBarCode ASP.NET in Web Farms (IIS 7.0)

If you use TBarCode ASP.NET Web Control in a Web Farm you may encounter the following exception:

Exception Type: System.ApplicationException
Message: The requested image is no longer cached.

The TBarCode .NET Web Control uses the HttpContext.Cache object to store the generated barcode image. Since http requests are load balanced within a web farm it can occur that a specific Web Control receives a request for an image, which has been generated on a different application server (and therefore is not available in its local cache).

To overcome this problem you could

  • Synchronize the HttpContext.Cache object on all servers in the web farm (="distributed caches"). This could be done with extra programming or additional third party software.
  • or simply use Client Affinity in IIS 7.0 (recommended)

Having client affinity enabled all requests are tagged with a cookie so that subsequent requests (from the same client) are routed to the same server in the web farm.

See Application Request Routing (Step 3 – Configure client affinity)

Application Request Routing provides a client affinity feature that maps a client to a content server behind Application Request Routing for the duration of a client session. When this feature is enabled, the load balancing algorithm is applied only for the very first request from the client.

Bitmaps VB .NET

How can I Create a Bitmap Optimized for Thermal Printers?

The following sample code in VB .NET generates a barcode image optimized for thermal printer output (resolutions such as 203 dpi).

Dim bc As New TECIT.TBarCode.Barcode()

bc.BarcodeType = TECIT.TBarCode.BarcodeType.EanUcc128
bc.Data = "1234567890123"

' set font size
bc.Font = New System.Drawing.Font("Arial", 15,
          System.Drawing.FontStyle.Bold, GraphicsUnit.Point)

bc.TextDistance = 1.1

bc.BearerBarType = TECIT.TBarCode.BearerBarType.TopAndBottom
bc.BearerBarWidth = 2.4

' adjust quiet zone in [Modules] (recommended for bitmaps)
bc.QuietZoneUnit = TECIT.TBarCode.QuietZoneUnit.Modules
bc.QuietZoneLeft = 12
bc.QuietZoneRight = 12

' important: set printer resolution of thermal printer
bc.Dpi = 203

' 203 dpi --> Module Width = 0.5005 = 4 Pixel per Module
Dim moduleWidth As New Single
moduleWidth = 0.5004926  'need exact value here!

bc.SizeMode = TECIT.TBarCode.SizeMode.CustomModuleWidth
bc.ModuleWidth = moduleWidth + 0.001
bc.AdjustModuleWidthToPixelRaster = True

Dim width As New Single
Dim height As New Single

' size in [mm]
width = bc.CalculateBarcodeWidth(Nothing)
height = 35

' convert size to [Pixels]
width = width / (25.4 / bc.Dpi)
height = height / (25.4 / bc.Dpi)

' adjust bitmap size
Dim drawBitmapRect As New Rectangle(0, 0, width, height)
bc.BoundingRectangle = drawBitmapRect

' output to file
bc.Draw("barcode.bmp", TECIT.TBarCode.ImageType.Bmp)

PDF417 Bitmap

How can I Create a Readable PDF417 Bitmap with Constant Size?

The following sample code in ASP .NET generates a PDF417 image with constant size.

//PDF417
Barcode barcode = new Barcode();
barcode.Data = strMyData;
barcode.BarcodeType = BarcodeType.Pdf417;
barcode.Pdf417.EncodingMode = PdfEncodingMode.Binary;

// with dpi = 100 we get 1 Pixel = 0.254 mms
barcode.Dpi = 100;

 // we should specify the number of horizontal data columns
 // this inhibits the symbol to change its horizontal size regardless of data
 barcode.Pdf417.NumberOfColumns = 16; // use a value, which fits to your available space !!

 // now calculate optimal bitmap size for the bar code
 barcode.SizeMode = SizeMode.FitToBoundingRectangle;
 Size optimalSize = barcode.CalculateOptimalBitmapSize(null, 1, 1);

 // we already could use this optimal Size for saving the image
 // barcode.BoundingRectangle = new Rectangle(0, 0, optimalSize.Width,  optimalSize.Height);
 // barcode.Draw(filename, ImageType.Jpg);

 // but we want a constant bitmap size,
 // which fits exactly into your predefined space

 Size finalSize = new Size(350, 200); // final bitmap size in Pixel

 // so we have to add empty spaces around the symbol via adding a quiet zone
 barcode.QuietZoneUnit = QuietZoneUnit.Pixel;

 // calculate the required empty quiet zone we have to add

if (finalSize.Width > optimalSize.Width)
  barcode.QuietZoneRight = finalSize.Width - optimalSize.Width;
else
  // should not occur!! Reduce the Pdf417.NumberOfColumns
  finalSize.Width = optimalSize.Width;
            
if (finalSize.Height > optimalSize.Height)
  barcode.QuietZoneBottom = finalSize.Height - optimalSize.Height;
else
  // should not occur!! Increase final bitmap size
  finalSize.Height = optimalSize.Height;

barcode.BoundingRectangle = new Rectangle(0, 0, finalSize.Width, finalSize.Height);
barcode.Draw(filename, ImageType.Jpg);

PDF417 ASP.NET

How can I Optimize PDF417 Symbols in my Web Control?

Option #1

Add the following attributes to your TBarCode .NET Web Control. This will generate an optimized symbol, which fits into a 120 x 120 pixel matrix. Increase both the Width/Height and the NumberOfColumns attribute if required.

<cc2:BarcodeControl id="BarcodeControl1"
  Width="120" Height="120"
  Barcode-Dpi="96" 
  Barcode-SizeMode="MinimalModuleWidth" 
  Barcode-Pdf417-NumberOfColumns="3"                        
  Barcode-MustFit="True" ErrorHandling="ShowMessage" 
  Barcode-BarcodeType="Pdf417" 

Option #2

In the Page Load event or after you have set the bar code data apply the following calculation. This code will use width/height ratio of 1:3 for the graphical modules.

Barcode barcode = BarcodeControl1.Barcode;
barcode.SizeMode = SizeMode.FitToBoundingRectangle;  
System.Drawing.Size optimalSize = barcode.CalculateOptimalBitmapSize(null, 1, 1);

BarcodeControl1.Width = new Unit(optimalSize.Width, UnitType.Pixel);
BarcodeControl1.Height = new Unit(optimalSize.Height, UnitType.Pixel);  

BarcodeControl1.Refresh();

Code 39 Bitmap

How Can I Create a Readable Code 39 Bitmap?

The following sample code in ASP .NET generates a Code 39 image.

//Code 39
Barcode barcode = new Barcode();
barcode.Data = "10030000007611107871900002199908";
barcode.BarcodeType = BarcodeType.Code39;

// with dpi = 100 we get 1 Pixel = 0.254 mms
barcode.Dpi = 100;

// now calculate optimal bitmap size for the bar code
barcode.SizeMode = SizeMode.FitToBoundingRectangle;

// set default size of symbol (define the default height)
barcode.BoundingRectangle = new Rectangle(0, 0, 254, 100 /* = 1 inch */);
                
// now calculate optimal bitmap size for the bar code
Size optimalSize = barcode.CalculateOptimalBitmapSize(null, 1, 1);

barcode.BoundingRectangle  = new Rectangle(0, 0, optimalSize.Width, optimalSize.Height);
barcode.Draw(filename, ImageType.Jpg);

© TEC-IT Datenverarbeitung GmbH, Austria  |  Site Map  |  RSS  |  Impressum  |  Nutzungsbedingungen  |  Kontakt