将PictureBox图像更改为应用程序文件夹中的图像

时间:2021-07-22 19:35:18

I want PictureBox to load images from application folder. In the code below it loads picture from exact place. I want it to load images from application's folder so that if I copy it to other computers it could load images.

我希望PictureBox从应用程序文件夹加载图像。在下面的代码中,它从确切的位置加载图片。我希望它从应用程序的文件夹加载图像,这样如果我将其复制到其他计算机,它可以加载图像。

How can I do it?

我该怎么做?

Loads from exact place:

来自确切位置的负载:

PictureBox1.Image = Image.FromFile("D:\68.jpg");

I want it to be like this:

我希望它是这样的:

PictureBox1.Image = Image.FromFile("ApplicationFolder\68.jpg");

2 个解决方案

#1


3  

Using the info in the comment above, you could do:

使用上面评论中的信息,您可以:

PictureBox1.Image = Image.FromFile(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "68.jpg"))

To do use a subdirectory of the assembly base directory:

要使用程序集基目录的子目录:

PictureBox1.Image = Image.FromFile(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SomeFolderInBaseDirectory", "68.jpg"))

#2


0  

Use to keep images in application resources:

用于将图像保留在应用程序资源中:

Add PictureBox control on Form. Select the control on the form and use properties. Find Image under the Appearance section in Properties tab and click on [...]. Select Resource dialog imports all images you're going to use in your application. Then remove the PictureBox from the Form. The application keeps all images in resources

在Form上添加PictureBox控件。在窗体上选择控件并使用属性。在“属性”选项卡的“外观”部分下找到图像,然后单击[...]。选择“资源”对话框将导入您将在应用程序中使用的所有图像。然后从窗体中删除PictureBox。应用程序将所有图像保存在资源中

#1


3  

Using the info in the comment above, you could do:

使用上面评论中的信息,您可以:

PictureBox1.Image = Image.FromFile(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "68.jpg"))

To do use a subdirectory of the assembly base directory:

要使用程序集基目录的子目录:

PictureBox1.Image = Image.FromFile(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SomeFolderInBaseDirectory", "68.jpg"))

#2


0  

Use to keep images in application resources:

用于将图像保留在应用程序资源中:

Add PictureBox control on Form. Select the control on the form and use properties. Find Image under the Appearance section in Properties tab and click on [...]. Select Resource dialog imports all images you're going to use in your application. Then remove the PictureBox from the Form. The application keeps all images in resources

在Form上添加PictureBox控件。在窗体上选择控件并使用属性。在“属性”选项卡的“外观”部分下找到图像,然后单击[...]。选择“资源”对话框将导入您将在应用程序中使用的所有图像。然后从窗体中删除PictureBox。应用程序将所有图像保存在资源中