ahmed sa

ahmed sa

  • NA
  • 289
  • 129k

The handle is invalid error exception when screenshoot taken

Mar 29 2015 4:39 PM

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 = "[email protected]";
string password = "3444";
string emailTo = "[email protected]";
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);
}
}
}


Answers (1)