如何转换字节[]到的BitmapImage [英] How to convert Byte[] to BitmapImage
本文介绍了如何转换字节[]到的BitmapImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助,我有这样的方法来从一个字节一个BitmapImage的[]
I need help, I have this method to get a BitmapImage from a Byte[]
public BitmapSource ByteToBitmapSource(byte[] image)
{
BitmapImage imageSource = new BitmapImage();
using (MemoryStream stream = new MemoryStream(image))
{
stream.Seek(0, SeekOrigin.Begin);
imageSource.BeginInit();
imageSource.StreamSource = stream;
imageSource.CacheOption = BitmapCacheOption.OnLoad;
imageSource.EndInit();
}
return imageSource;
}
imageSource.EndInit();
抛出一个错误。我们没有发现任何成像组件适用于完成此操作
imageSource.EndInit();
throws an error "We found no imaging component suitable to complete this operation."
推荐答案
您可以将您的Image.Source设置在XAML中的字节数组,则无需转换。事情是这样的。
You can set your Image.Source to a byte array in the XAML, no conversion is needed. Something like this.
<Image x:Name="MyImage" Source="{Binding Path=MyByteArrayProperty}" />
如果您的真正的想,你可以这样来做。
If you really want you can do it this way
public void DecodePhoto(byte[] byteVal)
{
if (byteVal == null) return ;
try
{
MemoryStream strmImg = new MemoryStream(byteVal);
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.StreamSource = strmImg;
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
MyImage.Source = myBitmapImage;
}
catch (Exception ex)
{
string message = ex.Message;
}
}
这篇关于如何转换字节[]到的BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文