在Windows手机上模糊图像

时间:2014-06-13 13:29:58

标签: c# windows-phone-8 windows-phone writeablebitmapex

我正在使用WriteableBitmapEX库试图模糊Windows Phone 8上的Bitmap图像我试过这段代码

Uri uri = new Uri(AnyUri, UriKind.RelativeOrAbsolute);
BitmapImage img1 = new BitmapImage();
img1.UriSource = uri;
img1.CreateOptions = BitmapCreateOptions.None;
WriteableBitmap wbm = new WriteableBitmap(img1);
var wbm2 = WriteableBitmapExtensions.Convolute(wbm, WriteableBitmapExtensions.KernelGaussianBlur5x5);
PanoramaBackground.ImageSource = wbm2;

但是有一个例外"对象引用未设置为对象的实例" 在WriteableBitmap wbm = new WriteableBitmap(img1);

为什么这不起作用?

1 个答案:

答案 0 :(得分:2)

这应该有效:

WriteableBitmap bitmap = BitmapFactory.New(0, 0).FromContent("RelativePathHere.png");
var blurredBitmap = WriteableBitmapExtensions.Convolute(bitmap, WriteableBitmapExtensions.KernelGaussianBlur5x5);
ImageControl.Source = blurredBitmap;

修改

好的,所以看起来你正在从网络上获取图像并且你有一个例外,因为图像尚未加载。您需要订阅ImageOpened事件处理程序。这是代码:

Uri uri = new Uri("AbsoluteUriPath.png", UriKind.Absolute);
BitmapImage img1 = new BitmapImage(uri);
img1.CreateOptions = BitmapCreateOptions.None;
img1.ImageOpened += (s, e) =>
{
      WriteableBitmap bitmap = new WriteableBitmap(img1);
      var blurredBitmap = WriteableBitmapExtensions.Convolute(bitmap,
                WriteableBitmapExtensions.KernelGaussianBlur5x5);
            ImageControl.Source = blurredBitmap;
};