C# 从带有路径的资源文件中加载 PictureBox 图像(第 3 部分)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17193825/
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-08-10 08:48:00  来源:igfitidea点击:

Loading PictureBox Image from resource file with path (Part 3)

c#picturebox

提问by zetar

I understand that this question has been asked (and answered) before. However, none of the solutions are working for me.

我知道这个问题之前已经被问过(和回答过)。但是,没有一个解决方案对我有用。

Below is a screen capture of all the relevant pieces of the puzzle:

以下是拼图所有相关部分的屏幕截图:

Screen capture http://dinosaur-island.com/PlantPictureBoxScreenCap.jpg

屏幕截图 http://dinosaur-island.com/PlantPictureBoxScreenCap.jpg

As you can see there are numerous bitmaps of plants loaded as resources into the Images folder. There is a form with a picturebox named "PlantPicture". There is string, which I know has a good path (because I've checked it in the debugger):

如您所见,有许多植物位图作为资源加载到 Images 文件夹中。有一个带有名为“PlantPicture”的图片框的表单。有一个字符串,我知道它有一个很好的路径(因为我已经在调试器中检查过它):

            PicPath = PicPath+".bmp";

Screen capture http://dinosaur-island.com/PlantDebugger.jpg

屏幕截图 http://dinosaur-island.com/PlantDebugger.jpg

I've tried numerous ways of loading, casting, etc., etc.

我已经尝试了多种加载、铸造等方式。

采纳答案by ispiro

The path should be something like: "Images\a.bmp". (Note the lack of a leading slash, and the slashes being backslashes.)

路径应该是这样的:"Images\a.bmp"。(注缺乏领导斜线和斜线是斜线。)

And then:

进而:

pictureBox1.Image = Image.FromFile(@"Images\a.bmp");

I just tried it to make sure, and it works. This is besides the other answer that you got - to "copy always".

我只是试了一下以确保它有效。这是除了您得到的另一个答案之外 - “始终复制”。

回答by zetar

Setting "Copy to Output Directory" to "Copy always" or "Copy if newer" may help for you.

将“复制到输出目录”设置为“始终复制”或“如果更新则复制”可能对您有所帮助。

Your PicPath is a relative path that is converted into an absolute path at some time while loading the image. Most probably you will see that there are no images on the specified location if you use Path.GetFullPath(PicPath)in Debug.

您的 PicPath 是一个相对路径,在加载图像时会在某个时间转换为绝对路径。如果您Path.GetFullPath(PicPath)在 Debug 中使用,您很可能会看到指定位置上没有图像 。

回答by Alin Leon

Ok...so first you need to import the image into your project.

好的...所以首先您需要将图像导入到您的项目中。

1) Select the PictureBox in the Form Design View

1)在Form Design View中选择PictureBox

2) Open PictureBox Tasks
(it's the little arrow printed to right on the edge of the PictureBox)

2)打开PictureBox Tasks
(它是PictureBox边缘右边印的小箭头)

3) Click on "Choose image..."

3) 点击“选择图片...”

4) Select the second option "Project resource file:"
(this option will create a folder called "Resources" which you can access with Properties.Resources)

4)选择第二个选项“项目资源文件:”
(此选项将创建一个名为“Resources”的文件夹,您可以使用Properties.Resources访问该文件夹)

5) Click on "Import..." and select your image from your computer
(now a copy of the image will be saved in "Resources" folder created at step 4)

5) 单击“导入...”并从您的计算机中选择您的图像
(现在图像的副本将保存在步骤 4 创建的“资源”文件夹中)

6) Click on "OK"

6)点击“确定”

Now the image is in your project and you can use it with the Properties command. Just type this code when you want to change the picture in the PictureBox:

现在图像在您的项目中,您可以通过“属性”命令使用它。当您想更改图片框中的图片时,只需键入以下代码:

pictureBox1.Image = Properties.Resources.MyImage;

Note:
MyImage represent the name of the image...
After typing "Properties.Resources.", all imported image files are displayed...

注:
MyImage 代表图片名称...
输入“Properties.Resources.”后,显示所有导入的图片文件...

回答by Samy Massoud

The accepted answer has major drawback!
If you loaded your image that way your PictureBox will lockthe image,so if you try to do any future operations on that image,you will get error message image used in another application!
This article show solution in VB

接受的答案有主要缺点!
如果您以这种方式加载图像,您的 PictureBox 将锁定图像,因此如果您以后尝试对该图像执行任何操作,您将收到另一个应用程序中使用的错误消息图像!
这篇文章展示了 VB 中的解决方案

and This is C# implementation

这是 C# 实现

 FileStream fs = new System.IO.FileStream(@"Images\a.bmp", FileMode.Open, FileAccess.Read);
  pictureBox1.Image = Image.FromStream(fs);
  fs.Close();

回答by chenjesu

It depends on your file path. For me, the current directory was [project]\bin\Debug, so I had to move to the parent folder twice.

这取决于您的文件路径。对我来说,当前目录是[project]\bin\Debug,所以我不得不移动到父文件夹两次。

Image image = Image.FromFile(@"..\..\Pictures\"+text+".png");
this.pictureBox1.Image = image;

To find your current directory, you can make a dummy label called label2 and write this:

要找到您的当前目录,您可以创建一个名为 label2 的虚拟标签并编写以下内容:

this.label2.Text = System.IO.Directory.GetCurrentDirectory();