WPF字节数组的BitmapImage [英] WPF Byte array to BitmapImage

查看:164
本文介绍了WPF字节数组的BitmapImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读字节数组BitmapImage的所有第一页的结果,我发现那位
字节数组BitmapImage的WP
在Silverlight 字节[]到的BitmapImage
问题是,我code这么想的工作对我来说,我得到这个错误:


  

System.Windows.Media.Imaging.BitmapImage'不包含一
  定义的SetSource',没有扩展方法'的SetSource
  接受类型的第一个参数
  System.Windows.Media.Imaging.BitmapImage'可以找到(你
  缺少using指令或程序集引用?)


我的主要code是:

  INT跨距= CoverPhotoBitmap.PixelWidth * 4;
INT大小= CoverPhotoBitmap.PixelHeight *步幅;
字节[] = CoverPhotoPixels新的字节[大小]
CoverPhotoBitmap.CopyPixels(CoverPhotoPixels,步幅,0);字节[] = HiddenPhotoPixels新的字节[大小]
HiddenPhotoBitmap.CopyPixels(HiddenPhotoPixels,步幅,0);
ResultPhotoBitmap = ByteArraytoBitmap(HiddenPhotoPixels);

和我的方法是:

 公共静态的BitmapImage ByteArraytoBitmap(字节[]的字节数组)
{
    MemoryStream的流=新的MemoryStream(字节阵列);
    的BitmapImage BitmapImage的=新的BitmapImage();
    bitmapImage.SetSource(流);
    返回BitmapImage的;
}


解决方案

您发现似乎已具体到Silverlight的例子。例外说明了在所谓的(的SetSource)的方法并不存在。你需要做的是设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.streamsource(v=vs.100).aspx\"相对=nofollow> StreamSource的

 公共静态的BitmapImage ByteArraytoBitmap(字节[]的字节数组)
{
    MemoryStream的流=新的MemoryStream(字节阵列);    的BitmapImage BitmapImage的=新的BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.StreamSource =流;
    bitmapImage.EndInit();    返回BitmapImage的;
}

I read all first page results of "Byte array to BitmapImage" and I fount Byte array to BitmapImage WP byte[] to BitmapImage in silverlight the problem is that I code dosen't work for me and I get this error:

'System.Windows.Media.Imaging.BitmapImage' does not contain a definition for 'SetSource' and no extension method 'SetSource' accepting a first argument of type 'System.Windows.Media.Imaging.BitmapImage' could be found (are you missing a using directive or an assembly reference?)

My main code is:

int stride = CoverPhotoBitmap.PixelWidth * 4;
int size = CoverPhotoBitmap.PixelHeight * stride;
byte[] CoverPhotoPixels = new byte[size];
CoverPhotoBitmap.CopyPixels(CoverPhotoPixels, stride, 0);

byte[] HiddenPhotoPixels = new byte[size];
HiddenPhotoBitmap.CopyPixels(HiddenPhotoPixels, stride, 0);
ResultPhotoBitmap = ByteArraytoBitmap(HiddenPhotoPixels);

and my method is:

public static BitmapImage ByteArraytoBitmap(Byte[] byteArray)
{
    MemoryStream stream = new MemoryStream(byteArray);
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.SetSource(stream);
    return bitmapImage;
}

解决方案

The example you found appears to have been specific to Silverlight. The exception explains that the method you called(SetSource) does not exist. What you need to do is set the StreamSource.

public static BitmapImage ByteArraytoBitmap(Byte[] byteArray)
{
    MemoryStream stream = new MemoryStream(byteArray);

    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.StreamSource = stream;
    bitmapImage.EndInit();

    return bitmapImage;
}

这篇关于WPF字节数组的BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆