Reputation: 578
I have a list of Image's address. With this code I can read the Image correctly :
List<Image> ImageList = new List<Image>();
List<string> fileNameList = new List<string>();
foreach (var fileName in fileNameList)
{
var request = WebRequest.Create(fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(Username, Password);
using (var response = (FtpWebResponse)request.GetResponse())
using (var stream = response.GetResponseStream())
using (var img = Image.FromStream(stream))
{
img.Save(@"D:\ax.jpg", ImageFormat.Jpeg);
ImageList.Add(img);
}
}
Until this line ImageList.Add(img);
both of the img
and the item in ImageList
are true. But when it's coming out of the last using
, all of the ImageList's properties changes to "threw an exception of type 'System.ArgumentException'"
For example for property of Height
it changed to :
'((new System.Collections.Generic.Mscorlib_CollectionDebugView<System.Drawing.Image>(ImageList)).Items[0]).Height' threw an exception of type 'System.ArgumentException'
What's wrong with my code?
Upvotes: 0
Views: 170
Reputation: 63095
don't use using
statement for image, it will dispose image when leave the block
using (var stream = response.GetResponseStream())
{
var img = Image.FromStream(stream);
img.Save(@"D:\ax.jpg", ImageFormat.Jpeg);
ImageList.Add(img);
}
Upvotes: 1