ScreenShot上的GDI +一般错误

时间:2009-09-09 20:59:35

标签: c# winforms exception error-handling screenshot

我的内部Winform App有以下Exception Extension。我的问题是我在generic GDI+

上收到ss.save("C:\\HelpMe.jpg", ImageFormat.Jpeg);错误

它不是每次都会起作用然后出错。有时它会连续几次工作。

这可能是一个“锁定”问题吗?我还应该看看和/或做错什么。

我称之为 - >


catch (Exception ex)
        {
            ex.LogError(HUD.ShellForm);
        }

public static void LogError(this Exception exception, DevExpress.XtraEditors.XtraForm whichForm)
    {
        GetDesktopImage(whichForm);
        SendExceptionMail(exception);

        ExceptionMessageBox box = new ExceptionMessageBox(exception);
        box.Show(whichForm);
    }

    private static void SendExceptionMail(Exception exception)
    {
        SmtpClient smtpClient = new SmtpClient("MailServer");

        MailMessage message = new MailMessage
            {
                From = new MailAddress("MATRIX@anEmail"),
                Subject = "MATRIX Application Error",
                Body = exception.Message
            };

        Attachment attachment = new Attachment(@"C:\\HelpMe.jpg");
        message.Attachments.Add(attachment);

        message.To.Add("Developer@anEmail");
        message.To.Add("HelpDesk@anEmail");
        smtpClient.Send(message);
    }

    ///<summary>
    /// Grabs a screen shot of the App and saves it to the C drive in jpg
    ///</summary>
    private static void GetDesktopImage(DevExpress.XtraEditors.XtraForm whichForm)
    {
        Rectangle bounds = whichForm.Bounds;

        using (Bitmap ss = new Bitmap(bounds.Width, bounds.Height))
        using (Graphics g = Graphics.FromImage(ss))
        {
            g.CopyFromScreen(whichForm.Location, Point.Empty, bounds.Size);
            ss.Save("C:\\HelpMe.jpg", ImageFormat.Jpeg);
        }
    }

1 个答案:

答案 0 :(得分:1)

这通常是因为:

  1. 目标目录不存在
  2. 目标文件名已在使用中
  3. 目标文件名实际上是一个目录
  4. 用户无权写入目标文件
  5. ...等......

    基本上,这通常是由GDI无法创建/写入文件引起的。顺便说一句,在Vista中,您没有C:\

    的写入权限