你好mgenx! 我参考了那个例子,如下: var bmp = img.Source as BitmapImage;int height = bmp.PixelHeight; int width = bmp.PixelWidth; int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);byte[] bits = new byte[height * stride]; bmp.CopyPixels(bits, stride, 0); 但运行时会错: 未将对象引用设置到对象的实例。我改用BitmapImage bmp = new BitmapImage();bmp = img.Source as BitmapImage;也不行。报错如上。
to colinfang2006: 这Image是个 wpf控件 ,好像没这属性。在Form下,好像用save到stream的方法就能解决。不知wpf为什么这么复杂。
var bmp = img.Source as BitmapSource;
To Mgenx , 我用 var bmp = img.Source as BitmapSource,运行到bmp.PixelHeight时就会报错:未将对象引用设置到对象的实例。 后来我又改用WriteableBitmap wb = new WriteableBitmap((BitmapSource)img.Source)成功了。 谢谢指教!
我参考了那个例子,如下:
var bmp = img.Source as BitmapImage;int height = bmp.PixelHeight;
int width = bmp.PixelWidth;
int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);byte[] bits = new byte[height * stride];
bmp.CopyPixels(bits, stride, 0);
但运行时会错:
未将对象引用设置到对象的实例。我改用BitmapImage bmp = new BitmapImage();bmp = img.Source as BitmapImage;也不行。报错如上。
这Image是个 wpf控件 ,好像没这属性。在Form下,好像用save到stream的方法就能解决。不知wpf为什么这么复杂。
我用 var bmp = img.Source as BitmapSource,运行到bmp.PixelHeight时就会报错:未将对象引用设置到对象的实例。
后来我又改用WriteableBitmap wb = new WriteableBitmap((BitmapSource)img.Source)成功了。
谢谢指教!