BitmapImage img = new BitmapImage(new Uri("somepath",UriKind.Relative)); WriteableBitmap wbm = new WriteableBitmap(img);
我在上面的行中得到一个运行时错误:“对象引用未设置为对象的实例”.
解决方法
获取空引用异常的原因是
BitmapImage.CreateOptions Property默认值为BitmapCreateOptions.DelayCreation.您可以将其设置为BitmapCreateOptions.None并在加载图像后创建WriteableBitmap:
BitmapImage img = new BitmapImage(new Uri("somepath",UriKind.Relative)); img.CreateOptions = BitmapCreateOptions.None; img.ImageOpened += (s,e) => { WriteableBitmap wbm = new WriteableBitmap((BitmapImage)s); };