'System.Windows.Controls.Image' does not contain a d

本文关键字:contain not Controls System Windows Image does | 更新日期: 2023-09-27 18:20:48

我想制作一个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"不包含的定义"FromFile"

我怎样才能消除这个错误?谢谢

'System.Windows.Controls.Image' does not contain a d

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"));