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

标签 c# image path picturebox

我在 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/

相关文章:

css - 在 div 中对齐图像

html - 为什么我的图像超链接可以从远离实际图像的较大边缘点击?

c# - 在分层 SQL 数据库中查找所有父项

c# - Web.Config <customErrors> 中的 defaultRedirect 问题

c# - 在 C# 中获取特定服务的 CPU 使用率

apache - 在 RHEL5 中配置 gcc - 在 $PATH 中找不到可接受的 C 编译器

c++ - visual studio c编译器的路径

c# - asp.net 下拉列表选项中的内容被剪切/重叠

javascript - 尝试使用javascript下载所有谷歌搜索图像

java - Spring框架-如何将目录作为资源注入(inject)?