【问题标题】:Set a ImageSource from FileSystem in Metro app在 Metro 应用程序中从 FileSystem 设置 ImageSource
【发布时间】:2012-09-30 11:28:49
【问题描述】:

在 ApplicationData.Current.RomanigFolder 中,我保存了一个 jpg 文件。是否可以在流或 MemoryStream 中读取此文件的内容并将其设置为 ImageSource?

我在带有 .NET 4.0 的 WPF 应用程序中使用它,代码如下:(img 是 XAML-Image-Control

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
StreamReader sr = new StreamReader(data.message.imageUrl);
bi.StreamSource = sr.BaseStream;
bi.EndInit();
img.Source = bi;
sr.Close();

对于 Metro 应用程序,我认为无法将 StreamSource 设置为 BitmapImage。如何将 Image-File 设置为 Image-Control?

【问题讨论】:

    标签: c# windows-8 windows-runtime winrt-xaml


    【解决方案1】:

    用于编写“Metro Style Apps”或为 Windows 8 构建的应用程序,以在 WPF 项目中设置图像的来源。

    代码如下:

    // Usage
    myImage.Source = ImageFromRelativePath(this, "relative_path_to_file_make_sure_build_set_to_content");
    
    public static BitmapImage ImageFromRelativePath(FrameworkElement parent, string path)
    {
        var uri = new Uri(parent.BaseUri, path);
        BitmapImage result = new BitmapImage();
        result.UriSource = uri;
        return result;
    }
    

    参考:http://www.codingbeta.com/programatically-setting-image-source-in-metro-style-apps-with-wpf/

    【讨论】:

    • 有没有办法控制文件何时加载到内存中? (就像 WPF 中给我们的 BitmapCacheOption 一样)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    相关资源
    最近更新 更多