Abdulmajeed Alshari

Abdulmajeed Alshari

  • NA
  • 266
  • 61.3k

"Parameter is not valid." in System.Drawing.Bitmap

Oct 14 2020 6:48 PM
I built simple application to take full screenshot each 100ms of computer's screen and set the screenshots on the PictureBox :
 
  1. private void StartWork()      
  2.       {      
  3.           try      
  4.           {     
  5. // width :1916 and height :1074 is the best measure to take full screenshot of whole laptotp's screen   
  6.               using (Image bitmap = new Bitmap(1916, 1074))      
  7.               {      
  8.                   using (Graphics g = Graphics.FromImage(bitmap))      
  9.                   {      
  10.                       g.CopyFromScreen(Point.Empty, Point.Empty, new Size(1916, 1074));      
  11.                   }      
  12.                   pictureBox1.BackgroundImage = (Image)bitmap.Clone();      
  13.                   pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;      
  14.                    //Sending Function to Send every token screenshot to all connected TcpClients    
  15.                  // Sending(bitmap);      
  16.                     
  17.                   pictureBox1.Invalidate();      
  18.                   pictureBox1.Refresh();      
  19.               }      
  20.           }      
  21.           catch (Exception ee)      
  22.           {      
  23.           //    MessageBox.Show(ee.Message);      
  24.           }      
  25.       }      
 calling StartWork() in every 100ms in Tick event :
  1. private void timer1_Tick(object sender, EventArgs e)  
  2.       {  
  3.           StartWork();  
  4.       }  
it is run well and i see the screenshots appears on PictureBox one after one but after several seconds I am facing this exception :  
Data = {System.Collections.ListDictionaryInternal}
HResult = -2147024809
 Message = "Parameter is not valid."
Source = "System.Drawing" 
 StackTrace = " at System.Drawing.Graphics.GetHdc()\r\n at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation)\r\n at System.Drawing.Graphics.CopyF...
TargetSite = {IntPtr GetHdc()}
 
I was searched in simillar problems in  CSharp Corner and Stackoverflow and try to change a lot of things but really i becommed tired. 
please developers how solve this problem .
Thanks in Advance . 
 
 

Answers (4)