由于对象的当前状态,操作无效

时间:2014-04-06 07:15:20

标签: c# windows-phone-8 nokia-imaging-sdk lumia-imaging-sdk

我开始开发winphone和nokia映像sdk。我有两个功能。

首先,我调用下面的函数将图像更改为灰色

 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;
    }

然后我调用函数将图像更改为二进制颜色

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;
                }
            }
        }
    }

但是当它跑到"等待renderer1.RenderAsync();"在第二个功能中,它不起作用。我该如何解决呢。你可以向我解释一下"等待"和" async"工作?

非常感谢你!

1 个答案:

答案 0 :(得分:2)

我大多猜测这里,因为我不知道你得到了什么错误,但我很确定你的问题在于设置源代码。在创建StreamImageSource之前,您是否确定将内存流位置设置为开头(0)?

尝试添加:

stream.Position = 0;

在创建StreamImageSource之前。

我没有尝试从可写位图创建内存流,而是建议:

using Nokia.InteropServices.WindowsRuntime;

...

using (var source = new BitmapImageSource(bm_image.AsBitmap())
{
   ...
}