1

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?

  • On what thread are you when you call that? – Igor Ralic Mar 10 '15 at 21:33
  • Hmm, both GetImageAcceptButton_Click() and GetCroppedBitmapAsync() are asynchronous calls, so they'd each run on their own (worker) thread. –  Mar 11 '15 at 06:39
  • That assumption is wrong I'm afraid. You're likely on the UI/dispatcher thread. Async doesn't mean concurrent, it just means the completion of the method might occur later. To actually post work on the thread pool, the called method needs to do so explicitly (e.g. by using Task.Run). So basically there is always a synchronous part beginning an async method. Typically it then becomes asynchronous at the first "await". Something to keep in mind. – CÅdahl Mar 11 '15 at 07:02
  • Thank you. That's good to know. I also had a look at the Threads window. –  Mar 11 '15 at 07:54

1 Answers1

0

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.

CÅdahl
  • 442
  • 3
  • 13
  • To elaborate, you begin by passing the stream to an Image element. That probably read the stream to the end already. – CÅdahl Mar 11 '15 at 07:05
  • It is a universal app. Thanks guys - this seems to be the key. I added a _stream.Seek(0), and that got rid of the exception. Now the image does not show but that might be due the wrong (dummy) measures I used to get this running. Need to take a closer look at it in the afternoon. But this seems to have fixed it. –  Mar 11 '15 at 07:49