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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15386139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:46:35  来源:igfitidea点击:

Change PictureBox image to images from application folder

vb.netvisual-studio-2012picturebox

提问by Murad Talibov

I want PictureBoxto 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:.jpg");

I want it to be like this:

我希望它是这样的:

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

回答by codechurn

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"))

回答by Vadim

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