views:

166

answers:

1

I have the below Exception Extension for my in house Winform App. My problem is that I get a generic GDI+ error on ss.save("C:\\HelpMe.jpg", ImageFormat.Jpeg);

It is not every time as it will work and then error out. Sometimes it will work a couple of times in a row.

Is this a "locking" issue, possibly? What else should I look at and/or am doing wrong.

I call this like so -->


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  A: 

This is usually because:

  1. the target directory doesn't exist
  2. the target file name is already in use
  3. the target file name is actually a directory
  4. the user doesn't have access to write to the target file

... etc ...

Essentially, this is generally caused by GDI not being able to create/write the file. BTW, in Vista you don't have write access to C:\

csharptest.net
It is Win 7 and all our users are local admins(don't shoot the messenger). Also, I wouldn't think I'd be able to create the file in the first place if that was the issue....
Refracted Paladin