0

i am beginning in develop winphone and nokia imaging sdk. i have two function.

firstly, i call the function below to change image to gray color

 private async void PickImageCallback(object sender, PhotoResult e)
    {
        if (e.TaskResult != TaskResult.OK || e.ChosenPhoto == null)
        {
            return;
        }
            using (var source = new StreamImageSource(e.ChosenPhoto))
            {
                using (var filters = new FilterEffect(source))
                {
                    var sampleFilter = new GrayscaleFilter();
                    filters.Filters = new IFilter[] { sampleFilter };
                    var target = new WriteableBitmap((int)CartoonImage.ActualWidth, (int)CartoonImage.ActualHeight);
                    var renderer = new WriteableBitmapRenderer(filters, target);
                    {
                        await renderer.RenderAsync();  
                        _thumbnailImageBitmap = target;
                        CartoonImage.Source = target;
                    }
                }
            }
        SaveButton.IsEnabled = true;
    }

then i call function to change image to binary color

private async void Binary(WriteableBitmap bm_image)
    {
        var target = new WriteableBitmap((int)CartoonImage.ActualWidth, (int)CartoonImage.ActualHeight);
        MemoryStream stream= new MemoryStream();
        bm_image.SaveJpeg(stream, bm_image.PixelWidth, bm_image.PixelHeight, 0, 100);

        using (var source = new StreamImageSource(stream))
        {
            using (var filters = new FilterEffect(source))
            {
                var sampleFilter = new StampFilter(5, 0.7);
                filters.Filters = new IFilter[] { sampleFilter };

                var renderer1 =new WriteableBitmapRenderer(filters, target);
                {
                    await renderer1.RenderAsync();
                    CartoonImage.Source = target;
                }
            }
        }
    }

but when it run to " await renderer1.RenderAsync();" in the second function, it doesn't work. How can i solve it. And you can explain for me about how "await" and "async" work ?

thank you very much!

David Božjak
  • 16,887
  • 18
  • 67
  • 98
Dat Lieu
  • 63
  • 9

1 Answers1

2

I'm mostly guessing here since I do not know what error you get, but I'm pretty sure your problem lies in setting up the source. Have you made sure the memory stream position is set to the beginning (0) before creating an StreamImageSource?

Try adding:

stream.Position = 0;

before creating the StreamImageSource.

Instead of trying to create a memory stream from the writeable bitmap I suggest doing:

using Nokia.InteropServices.WindowsRuntime;

...

using (var source = new BitmapImageSource(bm_image.AsBitmap())
{
   ...
}
David Božjak
  • 16,887
  • 18
  • 67
  • 98