简体   繁体   English

在C#中从相对路径中的文件加载图片框图像

[英]Load picturebox image in C# from file in relative path

I have a picturebox image in a Windows Form Solution. 我在Windows窗体解决方案中有一个picturebox图像。 After user selects an item from a database, I want to load an image into this picturebox. 用户从数据库中选择一个项目后,我想将图像加载到此图片框中。 The filename of the image will come from the database and all images must be stored in a subfolder of the application folder (\\Images). 图像的文件名将来自数据库,所有图像必须存储在应用程序文件夹(\\ Images)的子文件夹中。 I don't want to include all these (2000-3000 images) in my solution, besides, more images will be added by users as the database grows. 我不想在我的解决方案中包含所有这些(2000-3000图像),此外,随着数据库的增长,用户将添加更多图像。 Also, I don't want to code an absolute path. 另外,我不想编写绝对路径。 So this is not what I want: 所以这不是我想要的:

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

I want to code a relative path to the image folder, so regardless of where the application will be installed, the images can be loaded from this particular subfolder. 我想编写图像文件夹的相对路径,因此无论应用程序将安装在何处,都可以从此特定子文件夹加载图像。 I've tried things like these, using a temporary test-image called "test.jpg": 我尝试过这样的事情,使用名为“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 these don't work. 但这些都行不通。 I can only get it to work with an absolute path, like "C:\\Images\\test.jpg". 我只能用绝对路径来工作,比如“C:\\ Images \\ test.jpg”。 What should I do? 我该怎么办?

Have you tried the PictureBox.Load Method ? 你试过PictureBox.Load方法吗?

If the url parameter indicates a local file, the recommended format is a local file path. 如果url参数指示本地文件,则推荐的格式是本地文件路径。 For example, an image file named myPicture.jpglocated atc:\\ would be accessed by passing c:\\myPicture.jpg for the url parameter. 例如,名为myPicture.jpglocated atc:\\的图像文件将通过传递c:\\ myPicture.jpg来访问url参数。 A full path, such as http://www.contoso.com/path/images/image.jpg , or a relative path, such as ./images/image.jpg, can be used. 可以使用完整路径,例如http://www.contoso.com/path/images/image.jpg或相对路径,例如./images/image.jpg。 If a relative path is used, it will be considered relative to the working directory. 如果使用相对路径,则将其视为相对于工作目录。 A call to the Load method sets the ImageLocation property to the value of url. 对Load方法的调用将ImageLocation属性设置为url的值。

I would use reflection to get the executing directory, such as: 我会使用反射来获取执行目录,例如:

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

The best way to load an image to a picturbox is, load required image from your project resource folder. 将图像加载到picturbox的最佳方法是从项目资源文件夹中加载所需的图像。

For this do the following: 为此,请执行以下操作:

  1. add required images to resource folder. 将所需图像添加到资源文件夹。
  2. load image to picturebox from resource folder. 从资源文件夹将图像加载到picturebox。

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

Note: image type is not required [.bmp, .jpg etc] 注意:图像类型不是必需的[.bmp,.jpg等]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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