Question

I'm developing a WP8 app using Nokia Imaging SDK. I'm trying to add filter effect to an image and render it into a WriteableBitmap.

Here is my code:

private async void PhotoChosen(object sender, PhotoResult photoResult)
    {
        if (photoResult != null)
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(photoResult.ChosenPhoto);

            WriteableBitmap wb = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);

            StreamImageSource source = new StreamImageSource(photoResult.ChosenPhoto);

            var effects = new FilterEffect(source);
            effects.Filters = new IFilter[] { new SketchFilter() };
            var renderer = new WriteableBitmapRenderer(effects, wb);

            await renderer.RenderAsync();
        }
    }

All is going fine, but when this line is processing:

await renderer.RenderAsync();

This ArgumentException is thrown:

Value does not fall within the expected range

I think I've made a mistake creating the IImageProvider effects or the WriteableBitmap wb

Does anyone got this problem and found an issue ? Thanks :)

Was it helpful?

Solution

You need to set the stream position before setting it as source for StreamImageSource.

BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(photoResult.ChosenPhoto);

WriteableBitmap wb = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);

photoResult.ChosenPhoto.Position = 0;

StreamImageSource source = new StreamImageSource(photoResult.ChosenPhoto);

You need to do this because you have called bitmap.SetSource(photoResult.ChosenPhoto). That means that the stream has already been read once, therefore it's position is at the very end of the stream. When StreamImageSource tries to read it, it is already at the end, thus "Value does not fall within the expected range."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top