简体   繁体   中英

Assign an external image to a Picturebox in Visual Studio 2008 (C#)?

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?

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

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.

You can get images from Resources

 
 
 
 
  
  
  pictureBox1.Image = Properties.Resources.[image name];
 
 
  

see How to embed and access resources by using Visual C#

Misread the question OP asked to load without using the Resources.

you will need to do

Image.FromFile("c:\\\\image.bmp")

If image if a JPEG then You can cast it to Bitmap

Bitmap myJpeg=(Bitmap) Image.FromFile("myJpegPath");

OR,

 System.Drawing.Image loadImg = System.Drawing.Image.FromFile("c:\\\\image.JPG"); loadImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); 

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

PS 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM