如何将Byte []转换为BitmapImage

fma*_*ma3 16 c# wpf bitmapimage

我需要帮助,我有这个方法从Byte []获取BitmapImage

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;
}
Run Code Online (Sandbox Code Playgroud)

imageSource.EndInit(); 抛出错误"我们发现没有适合完成此操作的成像组件."

ast*_*ght 26

Image.Source在XAML中设置为字节数组属性.

<Image x:Name="MyImage" Source="{Binding Path=MyByteArrayProperty}" />
Run Code Online (Sandbox Code Playgroud)

如果你真的想要你可以在这样的代码背后做到这一点

  public void DecodePhoto(byte[] byteVal)
        {


                MemoryStream strmImg = new MemoryStream(byteVal);
                BitmapImage myBitmapImage = new BitmapImage();
                myBitmapImage.BeginInit();
                myBitmapImage.StreamSource = strmImg;
                myBitmapImage.DecodePixelWidth = 200;
                myBitmapImage.EndInit();
                MyImage.Source = myBitmapImage;

        }
Run Code Online (Sandbox Code Playgroud)


Shi*_*mmy 0

您应该向我们提供有关您的图片的更多信息。
我可以假设系统不支持它,然后我建议您使用外部工具,例如imageMagik或任何其他特定于您的图像的转换器。