使用 ImageList 从 ListView 中删除缩略图。文件被锁定?

发布于 2024-12-18 19:15:30 字数 1263 浏览 1 评论 0原文

我有一个 ListView,它使用以下代码显示 ImageList

注意:我使用 XML 将各个图像放入 ImageList。

for (int i = 0; i < pictureList.Count; i++)
{
   Image temp = Image.FromFile(pictureList[i].InnerXml.ToString());
   Image img = temp.GetThumbnailImage(120, 120, null, new IntPtr());
   imageList.Images.Add(img);
}

当我要删除一个项目时,我使用以下代码:

node.ParentNode.RemoveChild(node);// This deletes said node from the XML
doc.Save(xmlpath); // This then saves the updated XML document

imageList1.Images.Clear(); //Clear ImageList1

refreshThumbnails();  //Function that Regenerates thumbnails (without deleted image)

File.Delete(picturePath);  //Deletes the image

上面的代码仅在图像未在另一个 XML 中共享时执行。

我尝试添加一个计时器,直接从 ListView 中删除特定项目并清除 ImageList。它们都会间歇性地给出一个通用 IO 错误,指出文件被锁定。

唯一一致的是,如果它是相册中的最后一张图像,它总是会抛出相同的 I/O 错误。

我的主要问题是如何避免这个错误?大约 75% 的情况下我可以删除,另外 25% 的情况下会出现“文件已锁定”错误。

===============================================

我想我找到了我的回答。

加载图像时:

imageList.Images.Add(img);

我忘记在事后处理图像。由于我刷新缩略图以反映最新的 XML 状态,因此它锁定了加载的最后一个图像。因此,每当我删除最后一个图像(也称为列表中的唯一图像)时,我都会尝试删除锁定的文件。

这看起来正确吗?我对 C# 非常陌生,很高兴它现在能正常工作。

I have a ListView that is displaying an ImageList by using the following code:

Note: I am using an XML to get the individual images into the ImageList.

for (int i = 0; i < pictureList.Count; i++)
{
   Image temp = Image.FromFile(pictureList[i].InnerXml.ToString());
   Image img = temp.GetThumbnailImage(120, 120, null, new IntPtr());
   imageList.Images.Add(img);
}

When I go to delete an item I use the following code:

node.ParentNode.RemoveChild(node);// This deletes said node from the XML
doc.Save(xmlpath); // This then saves the updated XML document

imageList1.Images.Clear(); //Clear ImageList1

refreshThumbnails();  //Function that Regenerates thumbnails (without deleted image)

File.Delete(picturePath);  //Deletes the image

The above code only executes when the image is not shared in another XML.

I have tried adding a timer, removing the specific item from the ListView directly and clearing the ImageList. They all intermittently give a generic IO error saying that a file is locked.

The only thing consistent is that if it is the last image in an album, it will always throw the same I/O error.

My main question is how to avoid this error? I can delete about 75% of the time, the other 25% it gives me a "file locked" error.

=============================================

I think I found my answer.

When loading the images with:

imageList.Images.Add(img);

I forgot to dispose the image afterwords. Since I refresh the thumbnail to reflect the latest XML status, it locks the last image it loads. So whenever I delete the last image (AKA the only image in a list) I am trying to delete a locked file.

Does this seem right? I am very new to C# and I am just glad it is working for now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文