C#如何在列表框中循环显示图像
问题描述:
我创建了一个图像列表框,我想调整它们的大小。我已经得到了下来的方法,但我不能通过ListBox中的项目似乎循环:C#如何在列表框中循环显示图像
foreach (Image I in listbox1.items)
{
Resize(I, x, y)
}
我得到这个错误“无法施展System.String类型的对象键入System.Drawing.Image对象”。有任何想法吗?
早些时候,我还用浇铸在列表框中选择的项目图片:
Picturebox1.Image = (Image)listbox.selecteditem;
我记得它的工作,但它不会了。我假设我记得代码错了,任何替代品?
答
你是ListBox.Items.Add'ing错误。添加Image对象,而不是图像或URL或Image.ToString()的字符串路径。
现在我得到它..你没有做ListBox.Items.Add(图片),否则你看到在列表框中“垃圾”,所以答案是创建一个包装对象:
class ImageWrapper
{
public Image image;
public string displayName;
public override string ToString()
{
return displayName;
}
}
然后做
var iw = new ImageWrapper();
iw.image = <yourImage>;
iw.displayName = "Text for listbox here";
ListBox.Items.Add(iw);
向我们展示ListBox.Items.Add的代码,你在代码的那一部分做错了。 – servermanfail 2011-04-09 21:52:33
将图像放入ListBox没有任何意义,它不知道如何显示它们。很明显,你把字符串放在里面。你的代码不能使用字符串。 – 2011-04-09 21:59:58