[英]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.