2

我正在使用Nokia Imaging SDK开发WP8应用程序。 我正在尝试将滤镜效果添加到图像并将其渲染为WriteableBitmapWriteableBitmapRenderer.RenderAsync()ArgumentException“值不在预期的范围内”

这里是我的代码:

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

一切是怎么回事,但是当这条线是处理:

await renderer.RenderAsync(); 

ArgumentException抛出:

Value does not fall within the expected range 

我想我创建了IImageProvider effectsWriteableBitmap wb

有没有人有这个问题,并发现一个问题? 谢谢:)

回答

4

您需要设置流的位置,然后将其设置为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); 

您需要这样做,因为您已拨打bitmap.SetSource(photoResult.ChosenPhoto)。这意味着该流已被读取一次,因此它的位置在流的最后。当StreamImageSource尝试读取它时,它已经在最后,因此“值不在预期范围内”。

+0

它解决了这个问题! TY非常! :d – McSIME

相关问题