【问题标题】:'System.Windows.Controls.Image' does not contain a definition for 'FromFile'“System.Windows.Controls.Image”不包含“FromFile”的定义
【发布时间】:2014-09-04 21:00:14
【问题描述】:

我想制作一个 WPF 应用程序来显示我硬盘上的一些图像。 这是我的尝试:

using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;

namespace Photo
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Image myimage = Image.FromFile(@"C:\images\myimage.jpg");
        }
    }
}

这是我得到的错误:

'System.Windows.Controls.Image' 不包含对 '来自文件'

我怎样才能摆脱这个错误?谢谢..

【问题讨论】:

  • 您使用哪个 .NET 框架?它只属于 .NET 4 或更高版本。
  • 是.NET Framework 4.5
  • 您真正想使用哪个Image 类,System.Windows.Controls.ImageSystem.Drawing.Image?后者具有 FromFile 方法,但基于 GDI,因此您可能不想在 WPF 应用程序中使用它。
  • @Dirk 我想我应该选择第一个,但你能告诉我如何使用第一个获取图像吗?谢谢。

标签: c# wpf


【解决方案1】:

Image.FromFile 可用于System.Drawing.Image。它不适用于System.Windows.Controls.Image。对于 WPF,您不应该使用 System.Drawing.Image

所以问题是如何从文件中加载 WPF 中的Image 对象。使用:

System.Windows.Controls.Image myImage = new System.Windows.Controls.Image();
myImage.Source = new BitmapImage(new Uri(@"C:\images\myimage.jpg"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多