c# - 将 BitmapImage 转换为 Image.Source

标签 c# image xaml url bitmapimage

我目前卡在 Visual Studio 2017 中的一个项目中。以下代码用于在 .xaml 页面上设置图像组件的源。

private void setImage()
    {
        var image = new Image();
        var imageUrl = @"http://ddragon.leagueoflegends.com/cdn/5.2.1/img/" +
        currentChamp.Image.Group + "/" + currentChamp.Image.Full;
        Debug.Write(imageUrl);
        BitmapImage bmi = new BitmapImage();
        bmi.BeginInit();
        bmi.UriSource = new Uri(imageUrl, UriKind.Absolute);
        bmi.EndInit();

        image.Source = bmi;
    }

这里是要引用的代码。 ^^^

在所有其他问题中,我发现与我要问的问题相似,这一直是我正在尝试做的事情的正确/回答/批准的方式。但是,我收到一个错误,在任何问题中没有其他人收到

无法将 system.windows.media.imaging.BitmapImage 隐式转换为 Windows.UI.XAML.media.ImageSource

我的导入、引用和类型都已经过检查并且是正确的。我缺少框架吗?是否有可能导致此功能不再有效的更新?

我正在使用 VS2017 在 Microsoft.NETCore.UniversalWindowsPlatform 中工作

请注意,这不是 this 的副本,因为我没有引用资源文件夹并且我的路径是绝对路径。

最佳答案

Windows.Media.Imaging.BitmapImage 是一个 WPF 类。 Windows.UI.XAML.media.ImageSource 是 UWP。您找到了 WPF 的解决方案,但您的 UWP Image 类需要一个 UWP 位图对象。您需要使用 Windows.UI.Xaml.Media.Imaging.BitmapImage 的东西.

目前我无法测试 UWP,但通过阅读文档,我认为您想要这样的东西:

image.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri(imageUrl, UriKind.Absolute));

您的 C# 文件中似乎有 using Windows.Media.Imaging;。将其替换为 using Windows.UI.Xaml.Media.Imaging;

是明智的

那么你可以使用这个:

image.Source = new BitmapImage(new Uri(imageUrl, UriKind.Absolute));

关于c# - 将 BitmapImage 转换为 Image.Source,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44833830/

相关文章:

c# - 如何使用数据绑定(bind)更新 C# 图表控件

image - Action 图像 MVC3 Razor

c# - 如何在 C# 代码中重新启用 EntranceThemeTransition?

c# - WPF 子控件,仅样式内容部分

c# - 在 WPF/XAML 中实现带有图标的工具栏

c# - 如何在 autofac 中注册通用接口(interface)的所有实现?

c# - 混淆 Winform 及其外部依赖项 (dll)

c# - 更有效的写入文件的方法

html - 如何在 CSS flex 或 grid 中使单个图像响应并具有图像文本列?

css - 图像不会拉伸(stretch)容器宽度