【问题标题】:BitmapImage sourceStream is null in WPFBitmapImage sourceStream 在 WPF 中为空
【发布时间】:2013-07-24 15:09:07
【问题描述】:

我正在使用以下函数来加载位图图像

private BitmapImage fileNameBitMap(string filePath)
{
    if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
    {
        try
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.CacheOption = BitmapCacheOption.OnLoad;


            bitmap.DecodePixelWidth = 200;
            bitmap.UriSource = new Uri(filePath);
            bitmap.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;

            bitmap.EndInit();
            image = bitmap;
            return bitmap;
        }
        catch
        {
            return null;
        }
    }
    return null;
}

当我调试 bitmap.SourceStream 时,我发现它等于 null,我还发现抛出了 FormatNotSupportedException。 在将 BitmapImage 转换为 byte[] 的过程中,我需要将 BitmapImage 存储在流中。

【问题讨论】:

  • 我猜你的意思是BitmapImage.StreamSource。当然是null,因为你还没有设置。你已经设置了UriSource。你为什么不简单地从图像文件中读取byte[](例如File.ReadAllBytes)
  • 我做到了 :) 这是一个很好的解决方案,非常感谢。

标签: c# wpf bitmap stream


【解决方案1】:

您应该简单地将图像文件的内容读入byte[]

var bytes = File.ReadAllBytes(filePath);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多