Hello,
I have this C# code but I get the error in the title. on the "FromFile" part. I want to simply publish images in my PC on a WPF application.
How can I get rid of this error?
Thanks.

Code :

using System.Drawing;

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


            string filename1 = @"C:\images\mainicon.jpg";
            Image image1 = Image.FromFile(filename1);
        }
    }
}

Error :

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

It looks like the compiler is interpreting Image as System.Windows.Controls.Image rather than System.Drawing.Image. The error is correct in that the target class doesn't have a FromFile method.

I'm a bit surprised that you're not getting an ambiguity error instead, but a quick fix would be to fully qualify the class:

var image1 = System.Drawing.Image.FromFile(filename1);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.