Search code examples
windows-runtimewindows-phone-8.1lumia-imaging-sdk

Exception When Rendering an Image Using Lumia Imaging SDK


In my WP8.1 app, I'm trying to crop an image using the Lumia (formerly Nokia) Imaging SDK. the image is retrieved using FileOpenPicker:

public async void ContinueFileOpenPicker(Windows.ApplicationModel.Activation.FileOpenPickerContinuationEventArgs args) {
    if (args.Files.Count > 0) {
        _stream = await args.Files[0].OpenAsync(Windows.Storage.FileAccessMode.Read);
        _bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
        await _bitmapImage.SetSourceAsync(_stream);
        SelectedImage.Source = _bitmapImage;
    }
    else {
        Debug.WriteLine("Operation cancelled.");
    }
}

Then the filter applied in a button handler (after the user selected a cropping area; dimensions just for testing purposes):

private async void GetImageAcceptButton_Click(object sender, RoutedEventArgs e) {
    await GetCroppedBitmapAsync();
}

async public Task GetCroppedBitmapAsync() {
    using (var source = new RandomAccessStreamImageSource(_stream)) {
        using (var filterEffect = new FilterEffect(source)) {
            var filter = new CropFilter(new Windows.Foundation.Rect(0, 0, 100, 100));
            filterEffect.Filters = new IFilter[] { filter };
            var target = new WriteableBitmap(50, 50);
            using (var renderer = new WriteableBitmapRenderer(filterEffect, target)) {
                await renderer.RenderAsync();
                SelectedImage.Source = target;
            }
        }
    }
}

The RenderAsync() call throws an exception:

System.Runtime.InteropServices.COMException occurred
HResult=-2147467259
Message=Error HRESULT E_FAIL has been returned from a call to a COM component.
Source=mscorlib
ErrorCode=-2147467259

Applying the filters seems rather straightforward. Why does it fail here?


Solution

  • You should enable native debugging and look at the Output window. You're currently missing the real exception message (which tries to be more specific). Exception message strings are "smuggled" across the WinRT call border, only an HRESULT is officially passed (here, E_FAIL).

    Is this Silverlight 8.1 or a Universal App btw?

    My guess at an answer might be that you need to seek/rewind the stream back. It could be that the position is at the end.