c# - How to save Image in collection and load in picturebox -
i declare list store image's url
, call "loadbitmap" method image's stream.
//this list store url list list<string> imagefilelist=new list<string>(); //this list store return image stream list<stream> imagefiles = new list<stream>(); imagefilelist.add("some picture url"); imagefilelist.add("some picture url"); imagefilelist.foreach(delegate(string imgurl) { stream tempfile = loadbitmap(imgurl); imagefiles.add(tempfile); tempfile.dispose(); }); private stream loadbitmap(string imageurl) { stream image; httpwebrequest request =(httpwebrequest)webrequest.create(imageurl); using (var response = request.getresponse()) using (var stream = response.getresponsestream()) { image=stream; } return image; }
i store image's stream in list .
loop load these stream in picturebox
foreach (stream imagefile in imagefilelist) { picturebox eachpicturebox = new picturebox(); eachpicturebox.sizemode = pictureboxsizemode.autosize; //load image resource eachpicturebox.image = image.fromstream(imagefile); }
but when program run "eachpicturebox.image = image.fromstream(imagefile);
"
line occurred error : webexception.
try list<image>
, imagelist
store image ocurred error again.
has idea of how solve problem?
you have disposed stream , trying image stream. don't dispose stream or instead store image in list , use when need. example can use such code:
list<image> images = new list<image>(); ... using (var s = new system.io.filestream(imagefilepath, system.io.filemode.open)) { images.add(image.fromstream(s)); }
then when need use image, list, example:
picturebox1.image = images[0];
Comments
Post a Comment