C# 图像列表,正在删除引用

C# 图像列表,正在删除引用,c#,visual-studio,C#,Visual Studio,目前我正在使用ImageList控件,试图从网络上复制文件并覆盖ImageList上的内容。但是,当我尝试在填充列表后复制图像时,由于图像已加载,因此无法复制。我尝试过使用.Dispose()和.Images.Clear(),但从我读到的内容来看,没有任何内容会删除对图像本身的引用,因此可以替换它 imageList1.Images.Clear(); imageList2.Images.Clear(); int i = 1500;

目前我正在使用ImageList控件,试图从网络上复制文件并覆盖ImageList上的内容。但是,当我尝试在填充列表后复制图像时,由于图像已加载,因此无法复制。我尝试过使用.Dispose()和.Images.Clear(),但从我读到的内容来看,没有任何内容会删除对图像本身的引用,因此可以替换它

        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.ToString(i) + ".bmp");
                counter++;
            }


            i++;
        } while (i < 10000);
imageList1.Images.Clear();
imageList2.Images.Clear();
int i=1500;
字符串文件名,sourcePath,targetPath,destFile=string.Empty;
图像img=null;
int计数器=0;
bool exists=false;
string image=string.Empty;
MiscManager MM=新的MiscManager();
//filePath=MM.GetBobbinImagePath();
filePath=@“C:\Program Files\Images”;
sourcePath=@“\\networkimages”;
targetPath=文件路径;
如果(!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
if(System.IO.Directory.Exists(sourcePath))
{
string[]files=System.IO.Directory.GetFiles(sourcePath);
foreach(文件中的字符串n)
{
fileName=System.IO.Path.GetFileName(n);
destFile=System.IO.Path.Combine(targetPath,文件名);
尝试
{
System.IO.File.Copy(n,destFile,true);
}
抓住
{
Show(“正在使用的文件”,文件名);
}
}
}
做
{
如果(i<10)
{
fileName=“000”+Convert.ToString(i);
}
否则,如果(i>10&&i<100)
{
fileName=“00”+Convert.ToString(i);
}
否则,如果(i>=100&&i<1000)
{
fileName=“0”+Convert.ToString(i);
}
其他的
{
fileName=Convert.ToString(i);
}
image=filePath+fileName+“.bmp”;
exists=文件。exists(图像);
如果(存在)
{
img=Image.FromFile(图像);
imageList1.Images.Add(img);
imageList2.Images.Add(img);
imageList1.Images.SetKeyName(计数器,Convert.ToString(i)+“.bmp”);
imageList2.Images.SetKeyName(计数器,Convert.ToString(i)+“.bmp”);
计数器++;
}
i++;
}而(i<10000);

我对图片列表了解不多,所以非常感谢您的帮助。在c#和VS 2010中执行此操作时,可以使用
内存流
,这样图像就不会保留对文件流的引用:

using(MemoryStream ms = new MemoryStream(File.ReadAllBytes(image))
{
    img = Image.FromStream(ms);
}

太棒了,我现在不能尝试,但我会在早上第一件事就尝试