I have created a picturebox within my panel and I would like to fill it up with an image locally.
this is what I have done so far and the following code is just a small part of my panel creation code.
PictureBox picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(100, 50),
Location = new Point(14, 17)
};
p.Controls.Add(picture);
picture.ImageLocation = @"..\Image\1.jpg";
It does works but not fully as after I have launch my c# windows form application, it jus show a small white box with a red cross in the middle instead of the image. Any idea how to solve it?
Setting the image using
picture.ImageLocation()works fine, but you are using a relative path. Check your path against the location of the.exeafter it is built.For example, if your
.exeis located at:<project folder>/bin/Debug/app.exeThe image would have to be at:
<project folder>/bin/Image/1.jpgOf course, you could just set the image at design-time (the
Imageproperty on thePictureBoxproperty sheet).If you must set it at run-time, one way to make sure you know the location of the image is to add the image file to your project. For example, add a new folder to your project, name it
Image. Right-click the folder, choose “Add existing item” and browse to your image (be sure the file filter is set to show image files). After adding the image, in the property sheet set theCopy to Output DirectorytoCopy if newer.At this point the image file will be copied when you build the application and you can use