繁体   English   中英

WPF字节数组到BitmapImage

[英]WPF Byte array to BitmapImage

我读取了“字节数组到BitmapImage”的所有首页结果,并在Silverlight中将字节数组找到了BitmapImage WP 字节[]到BitmapImage,问题是我的代码对我不起作用,并且出现此错误:

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

我的主要代码是:

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);

我的方法是:

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

您发现的示例似乎特定于Silverlight。 该异常说明您调用的方法(SetSource)不存在。 您需要做的是设置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