Image list, removing reference
Currently I'm using an ImageList control, attempting to copy files off the network and overwrite whats on the ImageList. However, when I try to copy the images once the List has be populated I'm unable since the images are loaded. I've tried using .Dispose() and .Images.Clear() but from what I've read nothing removes the reference to the Image itself so it can be replaced.
imageList1.Images.Clear();
imageList2.Images.Clear();
int i = 1500;
string fileName,sourcePath,targetPath,destFile = string.Empty;
Image img = null;
int counter = 0;
bool exists = false;
string image = string.Empty;
MiscManager MM = new MiscManager();
//filePath = MM.GetBobbinImagePath();
filePath = @"C:\Program Files\Images";
sourcePath = @"\\network Images";
targetPath = filePath;
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
foreach (string n in files)
{
fileName = System.IO.Path.GetFileName(n);
destFile = System.IO.Path.Combine(targetPath, fileName);
try
{
System.IO.File.Copy(n, destFile, true);
}
catch
{
MessageBox.Show("File in use",fileName);
}
}
}
do
{
if (i < 10)
{
fileName = "000" + Convert.ToString(i);
}
else if (i > 10 && i < 100)
{
fileName = "00" + Convert.ToString(i);
}
else if (i >= 100 && i < 1000)
{
fileName = "0" + Convert.ToString(i);
}
else
{
fileName = Convert.ToString(i);
}
image = filePath + fileName + ".bmp";
exists = File.Exists(image);
if (exists)
{
img = Image.FromFile(image);
imageList1.Images.Add(img);
imageList2.Images.Add(img);
imageList1.Images.SetKeyName(counter, Convert.ToString(i) + ".bmp");
imageList2.Images.SetKeyName(counter, Convert.ToSt开发者_如何学Cring(i) + ".bmp");
counter++;
}
i++;
} while (i < 10000);
I don't know much about an Image list so any assistance is always greatly appreciated. Doing this in c# and VS 2010
You can use a MemoryStream
so your image doesn't hold on to the reference to the file stream:
using(MemoryStream ms = new MemoryStream(File.ReadAllBytes(image))
{
img = Image.FromStream(ms);
}
精彩评论