Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'System.Windows.Controls.Image' does not contain a definition for 'FromFile'

Tags:

c#

wpf

I want to make a WPF app to show some images on my hard drive. Here's my attempt :

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

Here is the error I get :

'System.Windows.Controls.Image' does not contain a definition for 'FromFile'

How can I get rid of this error? Thanks..

like image 290
jason Avatar asked Jan 09 '23 21:01

jason


1 Answers

Image.FromFile is available with System.Drawing.Image. It is not available with System.Windows.Controls.Image. For WPF you shouldn't be using System.Drawing.Image.

So the question is how to load Image object in WPF from a file. Use:

System.Windows.Controls.Image myImage = new System.Windows.Controls.Image();
myImage.Source = new BitmapImage(new Uri(@"C:\images\myimage.jpg"));
like image 140
Habib Avatar answered Feb 08 '23 00:02

Habib