Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 **************
C#
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

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

C#
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

C#
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);
}
}
}
Posted
Updated 29-Mar-15 18:34pm
v2
Comments
Manoj K Bhoir 30-Mar-15 1:06am    
On which line you are getting this exception?
Member 11428137 30-Mar-15 4:45am    
Are you running this over remote desktop, or as a service? CopyFromScreen won't work in either of these situations. It used to work in XP like that, but it was considered a security risk.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900