This project has moved and is read-only. For the latest updates, please go here.

"Index was outside the bounds of the array" in Decode method

Oct 19, 2014 at 7:40 AM
Edited Oct 19, 2014 at 7:43 AM

I'm new in Windows Phone 8.1 development.
I'm doing a simple app to scan barcode using ZXing.Net and Windows Phone 8.1 SDK.

When I call the Decode method I got an exception with text of "Index was outside the bounds of the array"

Here is a portion of code:
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
                    await Camera.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);


                    var properties = Camera.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
                    var videoEncodingProperties = properties as VideoEncodingProperties;

                    WriteableBitmap writableImg = new WriteableBitmap((int)videoEncodingProperties.Width, (int)videoEncodingProperties.Height);

                    await writableImg.SetSourceAsync(stream);

                    result = barcode.Decode(writableImg);

                    if (result != null)
                        debug.Text = result.Text;
                        debug.Text = "No results";
I think the problem is with the size of the WritableImage, because when I run the app on the emulator (and definitely there is no barcode in it), the decoder decodes and returns no value (and that's ok), but when I run it on my WP8.1 device, I got an exception with a text of:
"Index was outside the bounds of the array"

I tried to resize the writable image with no results! any help with that?
Oct 20, 2014 at 8:33 PM
Edited Oct 20, 2014 at 8:56 PM
I just looks to me like your confused between your photo image and your video instead of setting the .Width and .Height on your videoEncodingProperties why don't you try writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight
instead of
WriteableBitmap writableImg = new WriteableBitmap((int)videoEncodingProperties.Width, (int)videoEncodingProperties.Height);
You also need to pull the image twice from the stream once initialized as (1,1) and once as writeableBmp.PixelWidth, writeableBmp.PixelHeight
Marked as answer by Rifaisoft on 10/21/2014 at 3:07 PM
Jun 9, 2015 at 3:40 PM
I'm having the same problem and I done as deanforu44 suggested. The problem occurs only when reading from a file on the device. If I read directly from the camera, using MediaCapture or read from a file on the simulator I get no error. Here is my code:
        public async void ContinueFilePickerOperation(Windows.ApplicationModel.Activation.FileOpenPickerContinuationEventArgs e)
            StorageFile file = e.Files[0];

            var stream = await file.OpenReadAsync();
            // initialize with 1,1 to get the current size of the image
            var writeableBmp = new WriteableBitmap(1, 1);
            await writeableBmp.SetSourceAsync(stream);
            // and create it again because otherwise the WB isn't fully initialized and decoding
            // results in a IndexOutOfRange
            writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight);
            await writeableBmp.SetSourceAsync(stream);

            string result = null;
                result = barCondeReaderService.ScanBitmap(writeableBmp);
            catch (Exception ex)
                dialogService.Show("Ocorreu um erro \n" + ex.Message, "Erro");
                BarCodeText = result != null ? result : "Não identificamos um codigo válido";