Search code examples
c#windows-runtimewindows-phone-8.1winrt-xamllumia-imaging-sdk

Lumia Imaging SDK JpegRenderer.RenderAsync InvalidOperationException


I'm using Lumia Imaging SDK ver 2.0 to crop images in Windows Phone 8.1 RT application. The code works fine, but JpegRenderer.RenderAsync() sometimes throws InvalidOperationException, Operation is not valid due to the current state of the object.

The issue reproduces every time with a some images, and crashes the application. I use the following code for cropping:

using (StorageFileImageSource inputImageSource = new StorageFileImageSource(inputImageFile))
{
    using (FilterEffect filterEffect = new FilterEffect(inputImageSource))
    {
        // Create cropping filter.
        List<IFilter> filters = new List<IFilter>();
        CropFilter cropFilter = new CropFilter(croppedImageSize);
        filters.Add(cropFilter);

        // Add filters to effects.
        filterEffect.Filters = filters;

        // Create renderer with above filters and render new image.
        using (JpegRenderer renderer = new JpegRenderer(filterEffect))
        {
                IBuffer croppedImage = await renderer.RenderAsync();
                return croppedImage.ToArray();
        }
    }
}

I referred to this resource and it says the JpegRenderer.RenderAsync() throws InvalidOperationException when the filter property value changes while the rendering is in progress. I don't change the value of the property once it's set, then why is the exception being thrown?


Solution

  • I figured out the problem, and as David said, I was passing wrong dimensions which were larger than the size of the image. I was using BitmapDecoder.PixelHeight and BitmapDecoder.PixelWidth to calculate dimensions.

    However, in some images having orientation data in EXIF data, BitmapDecoder.PixelHeight gave the width of the image and vice-versa. For this, I had to use BitmapDecoder.OrientedPixelHeight and BitmapDecoder.OrientedPixelWidth to get the actual height and width of the image, accommodating the orientation of the image.