尝试从图像列表中删除图像 [英] Trying to remove an Image from Imagelist

查看:100
本文介绍了尝试从图像列表中删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经以编程方式创建了一个图像列表和listview,并在win窗体中添加了一个treeview.当用户浏览到Image Root文件夹时,它将根据子文件夹的数量创建listview和imagelist,并在树视图中显示根文件夹和子文件夹.用户将执行将图像从列表视图移动到树形视图的操作.在我的treeview_DragDrop事件上,当我调用File.Move()函数时,出现错误该进程无法访问该文件,因为该文件正在被另一个进程使用".据我了解,imagelist中的图像需要先进行处理和删除.如何处理图像列表中的对象? Dispose和RemoveAt方法不起作用.这是代码段...

I have created an imagelist and listview programmatically and add a treeview in the win form. When the user will browse to an Image Root folder, it will create the listview and imagelist depending on the number of sub folder and also show the root folder and subfolders in the treeview. User will perform moving an image from the listview to the treeview. On my treeview_DragDrop event, when I call the File.Move() function, I get an error "The process cannot access the file because it is being used by another process". Which I understand the image in the imagelist need to be dispose and remove first. How can I dispose the object from the imagelist? Dispose and RemoveAt method are not working. here is the code snippet...

private void treeView1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(ListViewItem)))
            {
                string DropInPath = string.Empty;
                string SelectedImage = string.Empty;
                ListViewItem li = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
                TreeNode nodeToDropIn = this.treeView1.GetNodeAt(this.treeView1.PointToClient(new Point(e.X, e.Y)));
                if (nodeToDropIn == null) { return; }
                if (nodeToDropIn.Level > 0)
                {
                    try
                    {
                        DropInPath = nodeToDropIn.FullPath;
                        SelectedImage = SelectedFolder + "\\" + li.Text;
                        this.fldImageList.Images[li.Text].Dispose();
                        this.fldImageList.Images.RemoveAt(li.Index);
                        File.Move(SelectedImage, DropInPath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

                if (li == null) { return; }
                nodeToDropIn.Nodes.Add(li.Text);
                fldListview.Items.Remove(li);
            }

推荐答案

实际上,有一种更简单的方法可以做到这一点.除了保留原始图像,您还需要保留图像的副本.我在此处 [ ="
Actually, there''s a simpler way to do this. Rather than keeping the original image, you need to keep a copy of the image instead. I demonstrate a low impact way to manage your images here[^].


这篇关于尝试从图像列表中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆