dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: The handle is invalid error exception when screenshoot from screen why and how to sol

  1. #1
    Join Date
    Jan 2014
    Posts
    29

    The handle is invalid error exception when screenshoot from screen why and how to sol

    Hi guys i have problem when i take screen shoot from screen it give me exception

    error the handle is invalid as following

    ee the end of this message for details on invoking
    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************
    System.ComponentModel.Win32Exception: The handle is invalid
    at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation)
    at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize)
    at Sales.Form1.timer4_Tick(Object sender, EventArgs e)
    at System.Windows.Forms.Timer.OnTick(EventArgs e)
    at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)

    this is my code as following :

    under timer tick event problem happen in screen shoot

    the proplem in these lines

    -------------

    Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    Graphics graphics = Graphics.FromImage(bitmap as Image);
    graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
    bitmap.Save("C:\\screenshot.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);

    ---------

    if i put these lines under button event work ok

    but if i but these lines under timer make problem and give me exception above why

    this code of screenshoot work in my windows xp but when i put in windows server 2003 it give me error in screenshoot under timer event

    private void timer4_Tick(object sender, EventArgs e)
    {
    Sales.SalesClass SalesClass1 = new Sales.SalesClass();
    if (SalesClass1.IsStartValid("Data Source=192.168.1.5;Initial Catalog=Altawi-last06-01-2015;User ID=admin;Password=123"))
    {
    issent = 1;
    label7.Text = issent.ToString();
    }
    else
    {
    issent = 0;
    label7.Text = issent.ToString();
    }
    if (Convert.ToInt32(label3.Text) > 0 && Convert.ToInt32(label2.Text) > Convert.ToInt32(label3.Text) &&Convert.ToInt32(label7.Text) == 0)
    {
    Sales.SalesClass SalesClass2 = new Sales.SalesClass();
    SalesClass2.InsertShipment("Data Source=192.168.1.5;Initial Catalog=Altawi-last06-01-2015;User ID=admin;Password=123", Convert.ToInt32(label3.Text), Convert.ToInt32(label2.Text));
    Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    Graphics graphics = Graphics.FromImage(bitmap as Image);
    graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
    bitmap.Save("C:\\screenshot.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
    string smtpAddress = "smtp.mail.yahoo.com";
    int portNumber = 587;
    bool enableSSL = true;
    string emailFrom = "ahmed_elbarbary.201023@yahoo.com";
    string password = "3444";
    string emailTo = "ahmed_elbarbary.201023@yahoo.com";
    string subject = "Working Start"+label6.Text;
    string body =
    "Quantity Required" + "" + "(" + label2.Text + ")" + "/" +
    "Quantity Shipped" + "/" + "(" + label3.Text + ")" + "/" +
    "Quantity Remaining" + "/" + "(" + label4.Text + ")";
    using (MailMessage mail = new MailMessage())
    {
    mail.From = new MailAddress(emailFrom);
    mail.To.Add(emailTo);
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;
    mail.Attachments.Add(new Attachment("c:\\screenshot.jpeg"));
    System.Net.Mail.SmtpClient shggmtp = new SmtpClient(smtpAddress, portNumber);
    smtp.Credentials = new NetworkCredential(emailFrom, password);
    smtp.EnableSsl = enableSSL;
    smtp.Send(mail);
    }
    }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,248

    Re: The handle is invalid error exception when screenshoot from screen why and how to

    Please reformat your code using code tags.

  3. #3
    Join Date
    Jan 2014
    Posts
    29

    Re: The handle is invalid error exception when screenshoot from screen why and how to

    Code:
    private void timer4_Tick(object sender, EventArgs e)
    {
    Sales.SalesClass SalesClass1 = new Sales.SalesClass();
    if (SalesClass1.IsStartValid("Data Source=192.168.1.5;Initial Catalog=Altawi-last06-01-2015;User ID=admin;Password=123"))
    {
    issent = 1;
    label7.Text = issent.ToString();
    }
    else
    {
    issent = 0;
    label7.Text = issent.ToString(); 
    }
    if (Convert.ToInt32(label3.Text) > 0 && Convert.ToInt32(label2.Text) > Convert.ToInt32(label3.Text) &&Convert.ToInt32(label7.Text) == 0)
    {
    Sales.SalesClass SalesClass2 = new Sales.SalesClass();
    SalesClass2.InsertShipment("Data Source=192.168.1.5;Initial Catalog=Altawi-last06-01-2015;User ID=admin;Password=123", Convert.ToInt32(label3.Text), Convert.ToInt32(label2.Text));
    Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    Graphics graphics = Graphics.FromImage(bitmap as Image);
    graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
    bitmap.Save("C:\\screenshot.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
    string smtpAddress = "smtp.mail.yahoo.com";
    int portNumber = 587;
    bool enableSSL = true;
    string emailFrom = "ahmed_elbarbary.201023@yahoo.com";
    string password = "3444";
    string emailTo = "ahmed_elbarbary.201023@yahoo.com";
    string subject = "Working Start"+label6.Text;
    string body =
    "Quantity Required" + "" + "(" + label2.Text + ")" + "/" +
    "Quantity Shipped" + "/" + "(" + label3.Text + ")" + "/" +
    "Quantity Remaining" + "/" + "(" + label4.Text + ")";
    using (MailMessage mail = new MailMessage())
    {
    mail.From = new MailAddress(emailFrom);
    mail.To.Add(emailTo);
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;
    mail.Attachments.Add(new Attachment("c:\\screenshot.jpeg"));
    System.Net.Mail.SmtpClient shggmtp = new SmtpClient(smtpAddress, portNumber);
    smtp.Credentials = new NetworkCredential(emailFrom, password);
    smtp.EnableSsl = enableSSL;
    smtp.Send(mail);
    }
    }
    }
    </code>
    <code>
    Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    Graphics graphics = Graphics.FromImage(bitmap as Image);
    graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
    bitmap.Save("C:\\screenshot.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
    Last edited by Arjay; April 2nd, 2015 at 03:58 PM. Reason: Replaced <> tags with [] tags

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,248

    Re: The handle is invalid error exception when screenshoot from screen why and how to

    Ahmedsa, use [] code tags instead of <> tags. Also, please format your code and use some whitespace, indents, etc.

    Code that is jammed all together is extremely hard to read and debug.

    For your issue, I would start with formatting your code and putting using() {} blocks around all objects that implement IDisposable.

    It could be that the code is holding on to open handles where a second part of the code can access the object because it hasn't been released.

    Use of using blocks will help ensure that items get released properly.

  5. #5
    Join Date
    Jan 2010
    Posts
    1,133

    Re: The handle is invalid error exception when screenshoot from screen why and how to

    Also, is this a service by any chance? If so, take a look at this.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)