Ondřej Ryška
Ondřej Ryška

Reputation: 491

FileNotFound when I use Image.FromFile()

I use Image.FromFile(string) method in this situation:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GVEMO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            PaintBackground();
        }
        public void PaintBackground() {
            gameBoard.Image = Image.FromFile("gvemoBack.jpg");
        }
    }
}

gameBoard is name for pictureBox. But when I started this program, I get exception FileNotFound. Image is in main directory (directory with folders bin,classes,properties etc.) I try copy this image into all project directories but exception remain. In which directory must be this image or what I do wrong? I use VS2012 and .NET framework version 4.5. Thx

Upvotes: 2

Views: 33368

Answers (7)

asreen fathima
asreen fathima

Reputation: 1

Even i had the same issue.

I did this to make it work fine:

If the image files are added directly to the project : 1.Right Click on the image->Properties window->Select "Copy always" or "Copy if newer" from the 'Copy' dropdown menu.

That's it.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613252

Since you used a relative path, the image must reside in the working directory. When you start an executable, unless you specify otherwise, the working directory is set to be the directory in which the executable resides. Clearly the image is not there.

Since it can be hard to extert control over the working directory, especially in a GUI app, it is usually better to specify a full absolute path rather than a relative path. For example:

string dir = Path.GetDirectoryName(Application.ExecutablePath);
string filename = Path.Combine(dir, @"MyImage.jpg");

However, far better for your scenario would be to include the image as a resource and so make your executable self-contained. Then you don't need to worry about details like making sure the images land in the same directory as the executable.

Upvotes: 3

user2229315
user2229315

Reputation:

If you don't specify a path, the current directory is used.

You can either use a relative path:

./xxx/yyy/file.ext
../../xxx/yyy/file.ext

or an absolute path:

/xxx/yyy/file.ext

or change the directory before launching the program or during its execution (language/platform dependant solution)

Upvotes: 0

jdehaan
jdehaan

Reputation: 19938

The image must be in the current working directory. At startup, this is the directory where you start your application from, this may be the directory where the executable resides (but must not be). Beware that this working directory can change!

Rather than using a path I would suggest you to use another mechanism. The current working directory can be set to something different if you use a FileOpenDialog for example. So you would be better off using a resource or a setting for the directory where the images reside.

Upvotes: 4

Jurgen Camilleri
Jurgen Camilleri

Reputation: 3589

Make sure your file is visible in the solution explorer. If not, click the Show All Files button in the solution explorer, right-click the image, and click `Include In Project".

If your file is visible, then make sure that the image is being copied to the output directory by right-clicking on the image in the solution explorer, choosing properties, and making sure the Copy to Output Directory option is set to Copy if newer or Copy always.

Upvotes: 3

Patko
Patko

Reputation: 4423

As jdehaan already said, image should (usually) be in the same directory with your executable. Thas is most likely bin\Debug in your case.

If this is a static image you could also embed it within your assembly. Look at Embedding Image Resources in a Project.

Upvotes: 1

aguyngueran
aguyngueran

Reputation: 1321

Please make sure the image is in the same directory as the application executable.

Upvotes: 0

Related Questions