我在 Windows 窗体解决方案中有一个图片框图像。用户从数据库中选择一个项目后,我想将图像加载到这个图片框中。图像的文件名将来自数据库,所有图像必须存储在应用程序文件夹 (\Images) 的子文件夹中。 我不想在我的解决方案中包含所有这些(2000-3000 张图像),此外,随着数据库的增长,用户将添加更多图像。 另外,我不想编写绝对路径。所以这不是我想要的:
pictureBox.Image = Image.FromFile(@"C:\Program Files\Application\Images\" + dbase.filename);
我想编写图像文件夹的相对路径,这样无论应用程序安装在何处,都可以从此特定子文件夹加载图像。 我尝试过这样的事情,使用名为“test.jpg”的临时测试图像:
pictureBox.Image = Image.FromFile(@"Images\test.jpg");
pictureBox.Image = Image.FromFile(@"..\Images\test.jpg");
pictureBox.Image = Image.FromFile(@"|DataDirectory|\Images\test.jpg");
但是这些都行不通。我只能让它使用绝对路径,比如“C:\Images\test.jpg”。我该怎么办?
最佳答案
您尝试过 PictureBox.Load Method 吗? ?
If the url parameter indicates a local file, the recommended format is a local file path. For example, an image file named myPicture.jpglocated atc:\ would be accessed by passing c:\myPicture.jpg for the url parameter. 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. 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.
关于c# - 从相对路径中的文件加载 C# 中的图片框图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24764353/