Image.FromFile

M

Mathieu Chavoutier

Hi.

I want to open an Image. But, I don't know how to do it in a relativ way :
I have a directory with pictures : Picture.
It is located in my work directory.

So, how can I open the image ?

For the moment, I have that, and it does not work :
Image newImage = Image.FromFile("../Picture/Computer.jpg");

I have tried this also :

Image newImage = Image.FromFile("Picture/Computer.jpg");

But, same result : it does not find the file.

Any idea ?
 
M

Morten Wennevik

Hi Mathieu,

If you are using Visual Studio .Net remember that the code directory is not the same as the startup directory. So, if your work directory is the same as the code directory you need to do Image.FromFile("../../Picture/Computer.jpg");

... means go to parent directory, which causes you to be inside "bin", so go back one more to get to your work directory.

Happy coding!
Morten Wennevik [C# MVP]
 
M

Mathieu Chavoutier

Morten Wennevik said:
Hi Mathieu,

If you are using Visual Studio .Net remember that the code directory is
not the same as the startup directory. So, if your work directory is the
same as the code directory you need to do
Image.FromFile("../../Picture/Computer.jpg");
.. means go to parent directory, which causes you to be inside "bin", so
go back one more to get to your work directory.

With the Application.StartupPath, I have noticed that I am in :
\bin\debug

But, the problem is that when I will do a release, will it be ok like this ?
(in the release, I won't be in \debug, nop ?)
 
D

Daniel O'Connell [C# MVP]

Mathieu Chavoutier said:
not the same as the startup directory. So, if your work directory is the
same as the code directory you need to do
Image.FromFile("../../Picture/Computer.jpg");
go back one more to get to your work directory.

With the Application.StartupPath, I have noticed that I am in :
\bin\debug

But, the problem is that when I will do a release, will it be ok like this
?
(in the release, I won't be in \debug, nop ?)

No, it will break in the release version. I would either use conditional
compilation(one of the few times I would) to define the string differently
based on the build version or have the build perform a post-build step and
copy the files to a particular runtime folder.
 
M

Morten Wennevik

I would put the Picture directory in the debug directory. Then you could use

Image.FromFile("Picture/Computer.jpg");

or to be on the safe side

Image.FromFile(Application.StartupPath + "/Picture/Computer.jpg");


Otherwise, you should probably use the UserAppDataPath property and put the Picture directory there, or make a registry entry specifying where the Picture directory is located.

Happy coding!
Morten Wennevik [C# MVP]
 
M

Mathieu Chavoutier

Daniel O'Connell said:
No, it will break in the release version. I would either use conditional
compilation(one of the few times I would) to define the string differently
based on the build version or have the build perform a post-build step and
copy the files to a particular runtime folder.

Isn't it possible to find the main directory ?
When I work in
c:\blabla\
The compiler knows where to find code sources. So, why is it so difficult to
find the same directory ?
 
M

Mathieu Chavoutier

Morten Wennevik said:
I would put the Picture directory in the debug directory. Then you could use

Image.FromFile("Picture/Computer.jpg");

But it is boring to move it every time I will have to do something.
or to be on the safe side

Image.FromFile(Application.StartupPath + "/Picture/Computer.jpg");

That doesn't work, because Application.StartupPath is already in Debug :blush:/
I wanted to do so :blush:)
Otherwise, you should probably use the UserAppDataPath property and put
the Picture directory there,

1.0.1598.24545 is the name of the directory.

Hum :blush:/

or make a registry entry specifying where the Picture directory is
located.

Ouch. I though something easy to do :blush:)

Seriously, there is no way to find the main directory ?
 
M

Morten Wennevik

The main directory of your program is Application.StartupPath. In your case it is something\bin\debug because that is where your program is located when you compile in debug mode. If you switch to release mode, you will find that the directory changes to something\bin\release. Wherever your program is located, Application.StartupPath will report which directory it is.

Happy coding!
Morten Wennevik [C# MVP]
 
D

Daniel O'Connell [C# MVP]

Mathieu Chavoutier said:
But it is boring to move it every time I will have to do something.


That doesn't work, because Application.StartupPath is already in Debug :blush:/
I wanted to do so :blush:)

the Picture directory there,

1.0.1598.24545 is the name of the directory.

Hum :blush:/


located.

Ouch. I though something easy to do :blush:)

Seriously, there is no way to find the main directory ?

What would be the "main directory"? Generally its whatever directory the app
runs from. Its a pain with debugging, but thats how it is. If you are using
VS2003, you might want to look in to pre and post build events and see if
you can automate moving your files to the bin/debug/Pictures/ directory.
 
M

Mathieu Chavoutier

Morten Wennevik said:
The main directory of your program is Application.StartupPath. In your
case it is something\bin\debug because that is where your program is located
when you compile in debug mode. If you switch to release mode, you will
find that the directory changes to something\bin\release. Wherever your
program is located, Application.StartupPath will report which directory it
is.

But it is not normal to change it.
My main directory is blabla, not blabla\bin\Debug, then blabla\bin\Release,
and later blabla.

Why can't I get easily blabla from the beginning ?
 
M

Mathieu Chavoutier

Daniel O'Connell said:
What would be the "main directory"? Generally its whatever directory the app
runs from.

I think that if VS is moving my objects files, I don't have to know it, and
it doesn't have to modify my way of programming.
Its a pain with debugging, but thats how it is. If you are using
VS2003, you might want to look in to pre and post build events and see if
you can automate moving your files to the bin/debug/Pictures/ directory.

I will look for it, thanks.
 
J

Jon Skeet [C# MVP]

Mathieu Chavoutier said:
But it is not normal to change it.
My main directory is blabla, not blabla\bin\Debug, then blabla\bin\Release,
and later blabla.

Why can't I get easily blabla from the beginning ?

Have you tried setting the Working Directory of the project?
 
M

Mathieu Chavoutier

Jon Skeet said:
Have you tried setting the Working Directory of the project?

Do you mean programmatically or with the VS tools ?

If you speak about doing it with the program, it is not possible [for me to
find my directory programmatically]
But, if you speak about an VS tools, I am interested, :blush:)
I don't understand how does it works.
 
J

Jon Skeet [C# MVP]

Mathieu Chavoutier said:
Have you tried setting the Working Directory of the project?

Do you mean programmatically or with the VS tools ?

If you speak about doing it with the program, it is not possible [for me to
find my directory programmatically]
But, if you speak about an VS tools, I am interested, :blush:)
I don't understand how does it works.

I mean manually, from VS.NET. Right click on the project, select
Properties, then go to Configuration Properties / Advanced. There's a
property there called Working Directory.
 
M

Mathieu Chavoutier

Jon Skeet said:
Mathieu Chavoutier said:
Have you tried setting the Working Directory of the project?

Do you mean programmatically or with the VS tools ?

If you speak about doing it with the program, it is not possible [for me to
find my directory programmatically]
But, if you speak about an VS tools, I am interested, :blush:)
I don't understand how does it works.

I mean manually, from VS.NET. Right click on the project, select
Properties, then go to Configuration Properties / Advanced. There's a
property there called Working Directory.

For me it was in Debug, but I finally found, thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top