Free Support Forum - aspose.com

Barcode throws InvalidCastException with certain string values - URGENT!

We are using Aspose.Barcode to generate data matrix barcodes and we have run into an issue where certain string values that we attempt to generate barcodes from are causing the component to throw an invalid cast exception. We had this issue once before, and when we submitted the issue the Aspose.Barcode technical team identified it as a bug and sent us a patch that addressed the problem. The patch worked for quite a while, but now we are starting to see the same problem with certain string values (relatively rarely, but since we generate these on legal documents, even one such occurrence is a serious problem). Here is the code used to generate the barcode:

Bitmap imgBarcode = null;

License license = new License();

license.SetLicense("Aspose.BarCode.lic");

try

{

BarCodeBuilder objBarCodeBuilder = new BarCodeBuilder();

objBarCodeBuilder.GraphicsUnit = GraphicsUnit.Millimeter;

objBarCodeBuilder.SymbologyType = Aspose.BarCode.Symbology.DataMatrix;

objBarCodeBuilder.BarHeight = 0.2f;

objBarCodeBuilder.Resolution = new Resolution(75.0f, 75.0f, ResolutionMode.Printer);

objBarCodeBuilder.BackColor = Color.White;

objBarCodeBuilder.ForeColor = Color.Black;

objBarCodeBuilder.Code128CodeSet = Code128CodeSet.Auto;

objBarCodeBuilder.CodeTextAlignment = StringAlignment.Center;

objBarCodeBuilder.xDimension = 0.5f;

objBarCodeBuilder.yDimension = 0.5f;

objBarCodeBuilder.AutoSize = true;

objBarCodeBuilder.ImageHeight = 20f;

objBarCodeBuilder.CaptionAbove.Visible = false;

objBarCodeBuilder.CaptionBelow.Visible = false;

objBarCodeBuilder.CodeLocation = CodeLocation.None;

objBarCodeBuilder.AztectErrorLevel = 15;

objBarCodeBuilder.CodeText = "8318AE1B-043D-4BA5-A5D5-12A73976671C . PlotImport . LMD09 . 27-SEP-05 . منحة . LMD09 . 27-SEP-05";

try

{

imgBarcode = objBarCodeBuilder.GenerateBarCodeImage();

}

catch (Exception)

{

objBarCodeBuilder.CodeText = codeValue.Replace("-", "");

imgBarcode = objBarCodeBuilder.GenerateBarCodeImage();

}

}

catch (Exception)

{

throw;

}

Below is the stack trace for the InvalidCaseException that is thrown:

at Aspose.BarCode.x43b47ea53db7fad9.x09ce0515f4236488(ArrayList& xcab25b727b2248a2)

at Aspose.BarCode.x43b47ea53db7fad9.x7348e718f33706c8(String x8555928af50182dd, ArrayList& x6c2d9947e99d3c64)

at Aspose.BarCode.x43b47ea53db7fad9._x568621beab670842(String x8555928af50182dd)

at Aspose.BarCode.x43b47ea53db7fad9.encode(String str)

at Aspose.BarCode.BarCodeBuilder.x568621beab670842()

at Aspose.BarCode.BarCodeBuilder.x479874c8b70eab1e()

at Aspose.BarCode.BarCodeBuilder.x3f05e8a79ad985e2()

at Aspose.BarCode.BarCodeBuilder.xd6b4fff0126b3dcb()

at Aspose.BarCode.BarCodeBuilder.GenerateBarCodeImage()

at eLMS.Common.Barcode.BarcodeImage.GetBarCodeImage(String codeValue) in C:\Source\eLMS\Solution\eLMS.Common\Barcode\BarcodeImage.cs:line 135

at eLMS.Test.BarcodeImageTest.GetBarCodeImageTest() in C:\Source\eLMS\Solution\eLMS.Test\BarcodeImageTest.cs:line 92

Hi,

I am unable to reproduce the above exception. Are you using the latest release of Aspose.BarCode? What I have done is simple copied your code in my application. There was 1 variable error which was not declared. I commented out the line.

objBarCodeBuilder.CodeText = codeValue.Replace("-", "");

When I tried to save the file, the VS 2005 gave message that the file contains Unicode characters, so save it in Unicode encoding format. I saved it. And I could see the arabic characters in the .cs file.

The code compiled and executed. I added the following line at the end to see the barcode image.

imgBarcode.Save("c:\\barcodes.png");

I tried to recognize it using the Aspose Recognition demo. It failed. The size of the image was too small, so I commented the following lines to make the image size default.

objBarCodeBuilder.Resolution = new Resolution(75.0f, 75.0f, ResolutionMode.Printer);
objBarCodeBuilder.xDimension = 0.5f;
objBarCodeBuilder.yDimension = 0.5f;

Ok, I will try the new version…but from your screenshot showing the component reading the generated barcode it looks like the Arabic text wasn’t encoded correctly.