2

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

Ondřej Ryška
  • 461
  • 1
  • 11
  • 23
  • All of the answerers thus far are correct: the `gvemoBack.jpg` image obviously doesn't exist where the app is looking for it. But you should really skip all of this and embed your image into the application as a resource. Then you code would simply load the resource. It'll always be available, regardless of where the executable file is moved. – Cody Gray - on strike Mar 31 '13 at 13:37
  • @Cody That's what I said. Plus I got the distinction between working directory and executable location correct. ;-) – David Heffernan Mar 31 '13 at 13:38
  • @David Bah, too many answers flooding in at once. I need to stick with questions that are a couple of hours old! Not being able to keep track of where the search begins for unqualified paths is exactly why you should use resources instead. – Cody Gray - on strike Mar 31 '13 at 13:41

7 Answers7

4

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.

jdehaan
  • 19,700
  • 6
  • 57
  • 97
  • Why do you say "the directory where your executable resides"? That seems to be incorrect to me. – David Heffernan Mar 31 '13 at 13:37
  • 1
    That's still not right. Relative paths are relative to the current directory, aka the working directory. – David Heffernan Mar 31 '13 at 13:39
  • True but if he does not manipulate the working directory, this remains the starting directory.... Thanks for correcting me. – jdehaan Mar 31 '13 at 13:41
  • 2
    You may as well get the details right. As it stands we have lots of answers that are technically incorrect. That's not very helpful. – David Heffernan Mar 31 '13 at 13:42
  • 2
    You are fully welcome with your comments, I also want to strive to perfection and don't like misleading answers... Thanks – jdehaan Mar 31 '13 at 13:45
  • Yes I copied image into directory where is .exe and problem is resolved. Yes, better solution is put this image into special project image folder and I will made this. Thanks – Ondřej Ryška Mar 31 '13 at 13:52
3

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Only nitpick I have with this is that I disagree on it being "better" to specify a full absolute path. I'd say that's equally as bad. What path do I hardcode? The one to my executable in `bin\Debug`? What happens when I move it? I guess you could use [`Application.ExecutablePath`](http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx) in conjunction with a relative path. – Cody Gray - on strike Mar 31 '13 at 13:43
  • @Cody I didn't say hard code. I meant making a full path dynamically, e.g. with Path.Combine and Application.ExecutablePath. – David Heffernan Mar 31 '13 at 13:46
3

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.

Jurgen Camilleri
  • 3,559
  • 20
  • 45
1

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.

Patko
  • 4,365
  • 1
  • 32
  • 27
0

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

aguyngueran
  • 1,301
  • 10
  • 23
  • Why is this necessary? Why does it need to be in this path? Are you *sure* that this is the directory used, instead of the current working directory? And what if I don't want it in that path, then what do I do? – Cody Gray - on strike Mar 31 '13 at 13:47
0

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)

  • 1
    That kind of relative path isn't going to work unless you know what the current directory is. And changing the directory is a fragile solution because it is subject to change at any time. Even if you do it right before you try to open the file, you've got a potential race condition on your hands. – Cody Gray - on strike Mar 31 '13 at 13:46
0

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.