I've created a listbox of Images and I want to resize all of them. I've got the method down but I cant seem to loop through the items in the listbox:
foreach (Image I in listbox1.items)
{
Resize(I, x, y)
}
I get this error "unable to cast object of type system.string to type system.drawing.image". Any Ideas?
Earlier I was also using an Image cast on a listbox selected item:
Picturebox1.Image = (Image)listbox.selecteditem;
I r开发者_StackOverflowemember it working, but it won't anymore. I'm assuming I remember the code wrong, any alternatives?
You're ListBox.Items.Add'ing wrong. Add the Image object, not a string path to the image or url or Image.ToString().
I get it now.. you're not doing ListBox.Items.Add(Image) because otherwise you see 'garbage' in the listbox, so the answer is to create a wrapper object:
class ImageWrapper
{
public Image image;
public string displayName;
public override string ToString()
{
return displayName;
}
}
then do
var iw = new ImageWrapper();
iw.image = <yourImage>;
iw.displayName = "Text for listbox here";
ListBox.Items.Add(iw);
精彩评论