Load image image in C # from file in relative path

I have an image in Windows Form Solution. After the user selects an item from the database, I want to load the image into this window. The file name will be obtained from the database, and all images should be saved in a subfolder of the application folder (\ Images). I do not want to include all of these (2000-3000 images) in my solution, in addition, more images will be added by users as the database grows. Also, I do not want to encode the absolute path. So this is not what I want:

pictureBox.Image = Image.FromFile(@"C:\Program Files\Application\Images\" + dbase.filename);

I want to encode the relative path to the image folder, so no matter where the application is installed, images can be downloaded from this particular subfolder. I tried such things using a temporary test image called "test.jpg":

pictureBox.Image = Image.FromFile(@"Images\test.jpg");
pictureBox.Image = Image.FromFile(@"..\Images\test.jpg");
pictureBox.Image = Image.FromFile(@"|DataDirectory|\Images\test.jpg");

But that will not work. I can get it to work with an absolute path, for example "C: \ Images \ test.jpg". What should I do?

+4
source share
3 answers

Have you tried the PictureBox.Load Method ?

url , - . , myPicture.jpglocated atc:\ , c:\myPicture.jpg url. , http://www.contoso.com/path/images/image.jpg, , . /images/image.jpg, , , . Load ImageLocation url.

+2

, , :

string ImagesDirectory = 
    Path.Combine(
        Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
        "Images"
    );
+1

picturbox - .

:

  • .
  • Upload the image to the image folder from the resource folder.

    Form1.pictureBox1.Image = yourProject.Properties.Resources.YourImage;

Note: image type is not required [.bmp, .jpg, etc.]

0
source

Source: https://habr.com/ru/post/1548523/


All Articles