473,480 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Image.FromFile

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 ?
Nov 16 '05 #1
14 21271
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]
Nov 16 '05 #2

"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de
news:opr748tzlzklbvpo@morten_x.edunord...
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 ?)
Nov 16 '05 #3

"Mathieu Chavoutier" <no****@no.spam> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...

"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de
news:opr748tzlzklbvpo@morten_x.edunord...
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 ?)


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.

Nov 16 '05 #4

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]
Nov 16 '05 #5

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> a écrit dans le
message de news:OG**************@TK2MSFTNGP09.phx.gbl...

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 ?
Nov 16 '05 #6

"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de
news:opr7493ny0klbvpo@morten_x.edunord...

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 :o/
I wanted to do so :o)
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 :o/

or make a registry entry specifying where the Picture directory is

located.

Ouch. I though something easy to do :o)

Seriously, there is no way to find the main directory ?
Nov 16 '05 #7
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]
Nov 16 '05 #8

"Mathieu Chavoutier" <no****@no.spam> wrote in message
news:uN****************@TK2MSFTNGP10.phx.gbl...

"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de
news:opr7493ny0klbvpo@morten_x.edunord...

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 :o/
I wanted to do so :o)
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 :o/

or make a registry entry specifying where the Picture directory is

located.

Ouch. I though something easy to do :o)

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.

Nov 16 '05 #9

"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de
news:opr75atpzgklbvpo@morten_x.edunord...
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 ?
Nov 16 '05 #10

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> a écrit dans le
message de news:Og*************@tk2msftngp13.phx.gbl...

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.
Nov 16 '05 #11
Mathieu Chavoutier <no****@no.spam> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #12

"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de
news:MP************************@msnews.microsoft.c om...
Mathieu Chavoutier <no****@no.spam> wrote:
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?


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, :o)
I don't understand how does it works.
Nov 16 '05 #13
Mathieu Chavoutier <no****@no.spam> wrote:
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, :o)
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #14

"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de
news:MP************************@msnews.microsoft.c om...
Mathieu Chavoutier <no****@no.spam> wrote:
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, :o)
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.
Nov 16 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1798
by: pnp | last post by:
Why does Image.FromFile() fails (OutOfMemoryException) when I try to load an icon file from the filesystem, that contains 32bit and 24bit icons, but works fine for Icon files with only 32bit icons...
3
19971
by: anastasia | last post by:
I get an out of memory exception when attempting to excecute the following code: original = System.Drawing.Image.FromFile(file.FileName,true); I ONLY get this exception when the file is in the...
8
4158
by: bsmalik | last post by:
Hi All, I am trying to do a simple thing. ----------------------------- Dim NCImage As System.Drawing.Image = System.Drawing.Image.FromFile("NC.jpg") ---------------------------- I have the...
2
11961
by: steve | last post by:
imports system.drawing imports system.drawing.image dim img as image = image.fromfile("a.bmp") picturebox1.image = img i'm using vs 2003 and the error i have is that fromfile is not a member...
0
3869
by: Taiwo | last post by:
When I use the method "System.Drawing.Image.FromFile(pictureFile)" where pictureFile is the path to a valid image file, the file is locked even minutes after the statement executes. This code is in...
4
3027
by: escristian | last post by:
Hello. I'm trying to create an Image so I use something like this: Image newImage = Image.FromFile(filename); Now when it's a bmp file and certain .gif files it gives me an exception that...
8
1659
by: =?Utf-8?B?WVhR?= | last post by:
Hello, I used Image.FromFile method to get lots of images from files, it's very slow in Windows Vista, but it's fast in Windows XP! could anyone please tell how to speed in Windows Vista? thank you
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.