在C#中处理图像 [英] Disposing images in C#

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

问题描述

大家好,



关于WinForms的快速提问:



我正在尝试使用Microsoft.VisualBasic.dll文件删除图像。它本身工作正常。



但是,我正在尝试从List中处理缩略图,出于某种原因,我收到错误消息说该文件正在使用中。这并不是一直发生的,这意味着我很难找出问题所在。



缩略图图像正在datagridviews数据行中使用,我认为我处理的图像没问题,但有些显然是错误的。



这是代码(构造函数,创建缩略图图像,删除缩略图图像:





  //  构造函数 
public Pic( int id, string name, byte grade, string path,Image thumb, bool 已选择)
{
.id = id;
this .name = name;
this .grade = grade;
this .path = path;
this .thumb = thumb;
this .selected = selected;
}









  private   void  bLoadImages_Click( object  sender,EventArgs e) 
{
if (ofdImages.ShowDialog()!= DialogResult.Cancel)
{
int i = 0 ;
plh.clearList();
dgv.DataSource = null ;
dgv.Rows.Clear();

foreach string picname in ofdImages.FileNames)
{
Image img = Image.FromFile(picname);
Image thumby = thumbnail2(img,img.Width,img.Height);

Pic pic = new Pic(i,picname, 0 ,thumby, false );
plh.addToList(pic);
img.Dispose();
i ++;
}

updateDGV();
}
}


...

// < span class =code-comment>创建缩略图图像
Image thumb = image.GetThumbnailImage(Convert.ToInt32(dWidth),Convert.ToInt32(dHeight),()= > false IntPtr .Zero);
返回拇指;







  if (imgpath == PicList.imgList [j] .name)
{
PicList.imgList [j]的.thumb.Dispose(); // 删除缩略图
PicList.imgList.RemoveAt(j); // 从图像列表中删除文件实例
尝试 // 将文件移动到回收站(为此,需要参考microsoft.VisualBasic.dll )
{
Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(imgpath,UIOption.OnlyErrorDialogs,RecycleOption.SendToRecycleBin,UICancelOption.DoNothing);
}







有人看到了什么问题吗?如果没有,有没有办法看到什么控件或参数使图像保持活着? (我只看到DeleteFile对话框中的错误,它只是说明图像正在使用中。



非常感谢!



Petter

解决方案


我认为你试图在处理后删除图片,但由于某种原因它''花费时间处理图片。

尝试在Disposed Handler之后删除图片,如果没有任何Disposed方法,那么你可以使用while循环(在try ... catch部分)。 / BLOCKQUOTE>

Hi all,

A quick question on WinForms:

I''m trying to delete images using the Microsoft.VisualBasic.dll file. It works fine in itself.

However, I''m trying to dispose a thumbnail image from a List and for some reason, I get error messages saying that the file is in use. This doesn''t happen all the time, which means it''s very difficult for me to pinpoint the problem.

The thumbnail images are being used in a datagridviews datarows, and I think I dispose an image alright, but somethng is obviously wrong.

Here is the code (constructor, creating a thumbnail image, and deleting a thumbnail image:


//Constructor
        public Pic(int id, string name, byte grade, string path, Image thumb, bool selected  )
        {
            this.id = id;
            this.name = name;
            this.grade = grade;
            this.path = path;
            this.thumb = thumb;
            this.selected = selected;
        }





        private void bLoadImages_Click(object sender, EventArgs e)
        {
            if (ofdImages.ShowDialog() != DialogResult.Cancel)
            {
                int i = 0;
                plh.clearList();
                dgv.DataSource = null;
                dgv.Rows.Clear();

                foreach (string picname in ofdImages.FileNames)
                {
                    Image img = Image.FromFile(picname);
                    Image thumby = thumbnail2(img, img.Width, img.Height);

                    Pic pic = new Pic(i, picname, 0, "", thumby, false);
                    plh.addToList(pic);
                    img.Dispose();
                    i++;
                }

                updateDGV();
            }
        }


...

//To create the thumbnail image
            Image thumb = image.GetThumbnailImage(Convert.ToInt32(dWidth), Convert.ToInt32(dHeight), () => false, IntPtr.Zero);
            return thumb;




if (imgpath == PicList.imgList[j].name)
                        {
                            PicList.imgList[j].thumb.Dispose(); //delete the thumbnail image
                            PicList.imgList.RemoveAt(j); //remove file instance from list of images
                            try //move file to recycle bin (for this, reference is needed to microsoft.VisualBasic.dll)
                            {
                                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(imgpath, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.DoNothing);
                            }




Does someone see what''s wrong? If not, is there any way to see what control or parameter is keeping the image alive? (I only see the error from the DeleteFile dialog box, which simply states that the image is in use.

Thanks a lot!

Petter

解决方案

Hi I think you are trying to delete the picture after disposing it but for some reason it''s taking time to dispose the picture.
Try removing the picture after Disposed Handler and if there is not any Disposed method then you can use a while loop (in the try...catch section).


这篇关于在C#中处理图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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