通过名称访问图像资源并将其分配给PictureBox [英] Accessing an image resource by name and assigning it to a PictureBox

查看:76
本文介绍了通过名称访问图像资源并将其分配给PictureBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个列表:

List<string> list = new List<string>();
list.Add("dog.jpg");
list.Add("cat.jpg");
list.Add("horse.jpg");

在我的资源中,我有3张图片,分别是狗,猫和马.我想使用列表将它们显示在图片框中.

In my resources, I have 3 images named: dog, cat, and horse. I want to display them in a picture box using the list.

我已经尝试过这样的事情:

I have tried something like this:

pictureBox1.Image = project.Properties.Resources.list[2]; // should display the horse image

问题在于它显示了错误:

The problem is that it displays the error:

资源"不包含列表"的定义.

'Resources' does not contain a definition for 'list' `

如何使用列表中的名称获取图像?

How can I get the image using the name in the list?

推荐答案

属性.资源只知道资源(狗,猫和马),因此您不能给他一个字符串,也不希望他知道该资源. 您需要像这样从ResourceManager使用GetObject方法:

Properties.Resources only knows the resources (dog, cat, and horse) so you can't give him a string and expect him to know the resource. You need to use the GetObject method from the ResourceManager like this:

(Bitmap)Properties.Resources.ResourceManager.GetObject(list[2])

这应该给你马的形象.

这篇关于通过名称访问图像资源并将其分配给PictureBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆