通用错误异常 [英] generic error exception

查看:97
本文介绍了通用错误异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我重新调用此函数时,它将生成一个通用的GDI +异常文件正在使用中".此函数在第一次调用时效果很好,但在显示异常后才起作用.

请帮助我删除此例外.

这是代码...

When I am re-calling this function it generates an generic GDI+ exception "File is in use". This function works well for the first call but then after display the exception.

Please help me to remove this exception.

Here is the code...

private Create_Image()
       {
           try
           {
               Control c = zgc;
               System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(c.Width, c.Height);
               c.DrawToBitmap(bmp, c.ClientRectangle);
               bmp.Save(Application.StartupPath + "\\abc.jpg", ImageFormat.Jpeg);
               c = null;
               MessageBox.Show(" Image Successfully Created...");
           }
           catch(Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

推荐答案

完成位图处理.如果您不这样做,那么它将在文件上保持锁定,直到垃圾回收器将其移开并为您摆脱它为止.

无论如何,当您完成位图和其他资源密集型对象的处理后,都应该按常规处置它们.

这是最简洁的方法:
Dispose your bitmap when you have finished with it. If you don''t then it holds a lock on the file until the Garbage collector comes round and gets rid of it for you.

You should routinely Dispose Bitmaps and other resource heavy objects when you are finished with them anyway!

This is the tidiest way to do it:
private Create_Image()
{
    try
    {
        Control c = zgc;
        using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(c.Width, c.Height))
        {
            c.DrawToBitmap(bmp, c.ClientRectangle);
            bmp.Save(Application.StartupPath + "\\abc.jpg", ImageFormat.Jpeg);
            c = null;
        }
        MessageBox.Show(" Image Successfully Created...");
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}


这篇关于通用错误异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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