WPF程序中有个Image控件,并且显示有图片。我的问题是:
如何取得这个控件中的图片并转成byte。谢谢!

解决方案 »

  1.   

    http://stackoverflow.com/questions/553611/wpf-image-to-byte
      

  2.   

    Image.PropertyItems  获取存储于该 Image 中的所有属性项(元数据片)。  
      

  3.   

    你好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;也不行。报错如上。
      

  4.   

    to colinfang2006:
    这Image是个 wpf控件 ,好像没这属性。在Form下,好像用save到stream的方法就能解决。不知wpf为什么这么复杂。
      

  5.   

                var bmp = img.Source as BitmapSource;
      

  6.   

    To Mgenx ,
    我用 var bmp = img.Source as BitmapSource,运行到bmp.PixelHeight时就会报错:未将对象引用设置到对象的实例。
    后来我又改用WriteableBitmap wb = new WriteableBitmap((BitmapSource)img.Source)成功了。
    谢谢指教!