views:

150

answers:

4

How can I assign an external image into the PictureBox in Visual Studio 2008 ?
Typically, When we use ChooseImage in PictureBox , Visual Studio adds the image to the exe file and it causes increasing exe file's volume, I wanna add the image from a directory beside the exe file.
Is it possible in Visual Studio 2008?

P.S: I don't want add the image with C# code, because VS2008 doesn't show it in developing time.

+2  A: 

I think you can do this in C# code using the Image.FromFile method. As long as the C# code is in the initialization of the form / control, it should run at both design time and run time.

davisoa
Do you mean, I have to add the c# code into the `InitializeComponent()` ?
Mohammad
I've added this code `this.pictureBox1.Image = Image.FromFile(@"Images\flower4.png");` but it causes an error !!!
Mohammad
No you totally should not modify `InitializeComponent` code as it gets generated by designer. This line should be placed after this call in the constructor. What kind of error do you get? Please also see my answer about linked resources.
gaearon
I would not use the relative file path you are using, I would use the full path: for example c:\MyProject\Images\flower4.png. If you are putting the code inside of the InitializeComponent() routine the computer might not be looking in the right spot.
Steve Ellinger
As an aside, gaearon is correct in that if you make any changes in the designer it will possibly delete without warning any code you have put in the InitializeComponent() method.
Steve Ellinger
+1  A: 
VoodooChild
As I said I don't want to use `Resources` , because it increases the exe file volume.
Mohammad
+1  A: 

You can use Linked resources.

When you add a linked resource, the .resx file that stores your project resource information includes only a relative path to the resource file on disk. If you add images, videos, or other complex files as linked resources, you can edit them using a default editor that you associate with that file type in the Resource Designer. When you add an embedded resource, the data is stored directly in the project's resource (.resx) file. Strings can only be stored as embedded resources.embedded resources.

MSDN on Linked vs. Embedded Resources

P.S. I however still don't get why would you need it—even if .exe doesn't get bigger, the whole app package would have a bigger size, wouldn't it?

Best,
Dan

gaearon
Bigger exe file runs with delay, isn't it ?
Mohammad
I don't think so. But if you're unsure, you can measure it.
gaearon
+1  A: 

If this is WinForms, it's better to just set the ImageLocation of the PictureBox to the path to your image. Do this from the designer to have it show at design time.

Jeff M