简体   繁体   English

WPF字节数组到BitmapImage

[英]WPF Byte array to 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: 我读取了“字节数组到BitmapImage”的所有首页结果,并在Silverlight中将字节数组找到了BitmapImage WP 字节[]到BitmapImage,问题是我的代码对我不起作用,并且出现此错误:

'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?) 'System.Windows.Media.Imaging.BitmapImage'不包含'SetSource'的定义,并且找不到扩展方法'SetSource'接受类型为'System.Windows.Media.Imaging.BitmapImage'的第一个参数(您是缺少using指令或程序集引用?)

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. 您发现的示例似乎特定于Silverlight。 The exception explains that the method you called(SetSource) does not exist. 该异常说明您调用的方法(SetSource)不存在。 What you need to do is set the StreamSource . 您需要做的是设置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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM