【问题标题】:loading the source of a BitmapImage in WPF?在 WPF 中加载 BitmapImage 的源?
【发布时间】:2012-08-02 04:49:45
【问题描述】:

在 silverlight 应用程序中,我有一个定义为 System.Windows.Media.Imaging.BitmapImage 的 BitmapImage,它是一个名为“SetSource”的方法,我可以像这样设置源:

BitmapImage bitmap = new BitmapImage();
System.IO.Stream stream = _scene.GetStream();
if (stream == null) return;
bitmap.SetSource(stream);

在 WPF 应用程序中,我也有一个 位图图像定义为System.Windows.Media.Imaging.BitmapImage,但没有 SetSource 方法。如何像在 Silverlight 应用中一样在 WPF 应用中设置源?

另外,它是一个流,而不是一个字符串。它不是 URI。所以“UriSource”方法不起作用。我试过这个:

        System.IO.Stream stream = _scene.GetStream();
        if (stream == null) return;
        BitmapImage bitmap = new BitmapImage();

        bitmap.UriSource = new Uri(stream.ToString());

在运行时,它抛出了一个错误,即无法确定 URI。 URI 是 Intranet 的标识符吗?你确定这不是银光吗?我正在做一个 WPF 应用程序

【问题讨论】:

    标签: c# wpf bitmapimage


    【解决方案1】:

    您必须设置BitmapImage.StreamSource 属性:

    BitmapImage bitmap = new BitmapImage();
    bitmap.BeginInit();
    bitmap.StreamSource = _scene.GetStream();
    bitmap.EndInit();
    

    如果您想在创建位图后立即关闭流,您还必须设置 BitmapCacheOption.OnLoad 选项:

    using (Stream stream = _scene.GetStream())
    {
        bitmap.BeginInit();
        bitmap.CacheOption = BitmapCacheOption.OnLoad;
        bitmap.StreamSource = stream;
        bitmap.EndInit();
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用constructor 指定UriSource

      BitmapImage bmp = new BitmapImage(uriSource: new Uri());
      

      或者你可以使用BitmapImage.StreamSource属性来设置StreamSource

      【讨论】:

        猜你喜欢
        • 2010-09-10
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多