We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

BarCodeReader problem when reading image stream

Hi all,

using the latest Aspose.BarCode components I have generated a bar code image and placed it into the attached excel file.

Using the code below I try to read the image from the excel but I get an exception "Parameter is not valid" (using the same technigue, I'm able to read a value from the same image placed in Word).

Pls. advice what I'm missing, the test Excel file is attached.

Thanks.

Mirek

        [Test, Ignore]
        public void ReadValueFromExcel()
        {
            const string documentPathName =
                @"C:\Temp\ExcelWithBarCodeImage.xls";

            var workbook = new Workbook();
        workbook.Open(documentPathName);

            var picture = workbook.Worksheets[0].Pictures[0];

            Assert.AreEqual(ImageFormat.Jpeg, picture.ImageFormat);

            var memoryStream = new MemoryStream();
        picture.ToImage(memoryStream, <span style="color: rgb(43, 145, 175);">ImageFormat</span>.Jpeg);


var barCodeText = string.Empty;
        <span style="color: blue;">try</span>
        {
            <span style="color: blue;">var</span> reader = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">BarCodeReader</span>(memoryStream, <span style="color: rgb(43, 145, 175);">BarCodeReadType</span>.DataMatrix);

            <span style="color: blue;">while</span> (reader.Read())
            {
                barCodeText = reader.GetCodeText();
            }
        }
        <span style="color: blue;">catch</span> (<span style="color: rgb(43, 145, 175);">Exception</span>)
        {
            barCodeText = <span style="color: blue;">string</span>.Empty;
        }

            Assert.IsNotEmpty(barCodeText);
}

Hi Mirek,


Thank you for your inquiry.

Please use the Workbook constructor to pass the Excel file path instead of Workbook.Open method. Also please make sure that your Aspose.Cells assembly is of latest version (7.0.3). I have changed your source code lines like below and was able to read the barcode text from Excel file.

[C#]

const string documentPathName = @“C:\Temp\ExcelWithBarCodeImage.xls”;

var workbook = new Aspose.Cells.Workbook(documentPathName);

var picture = workbook.Worksheets[0].Pictures[0];

var memoryStream = new MemoryStream();

picture.ToImage(memoryStream, ImageFormat.Jpeg);

var barCodeText = string.Empty;

try
{
var reader = new BarCodeReader(memoryStream, BarCodeReadType.DataMatrix);

while (reader.Read())
{
barCodeText = reader.GetCodeText();
}

}
catch (Exception)
{
barCodeText = string.Empty;
}