title: Runtime Error after taking screenshots in .NET
body: <p>Given the following code</p>
  
  <pre><code> public static Bitmap PrintWindow(IntPtr hWnd)
      {
          int width = GetWindowInfo(hWnd).rcWindow.Width;
          int height =  GetWindowInfo(hWnd).rcWindow.Height;
          Logger.log("width and height: " + width + "," + height, 0);
          Bitmap bmp = new Bitmap(width, height);
          Graphics memoryGraphics = Graphics.FromImage(bmp);
          memoryGraphics.CopyFromScreen(0, 0, 0, 0, GetWindowInfo(hWnd).rcWindow.Size);
          return bmp;
      }
  </code></pre>
  
  <p>My application takes a lot of screen shots for screen scraping purposes. So this method is called once every 3 seconds roughly. It always starts fine, but after a while I get exceptions here. If I build in error tolerance (to just try again on exception) it does keep running, but my logs show lots of failed attempts.</p>
  
  <p>These are the 3 exceptions:</p>
  
  <pre><code>Exception caught : System.ComponentModel.Win32Exception (0x80004005): The parameter is incorrect
  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 MoonHandler.scraping.User32Facade.PrintWindow(IntPtr hWnd) in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\scraping\User32Facade.cs:line 50
     at MoonHandler.scraping.MoonFacade.getBMP(Boolean fresh) in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\scraping\MoonFacade.cs:line 37
     at MoonHandler.scraping.MoonFacade.isWarpDriveActive() in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\scraping\MoonFacade.cs:line 154
     at MoonHandler.logic.MoonLogic.warping_to_belt() in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\logic\MoonLogic.cs:line 113
     at MoonHandler.logic.MoonLogic.act() in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\logic\MoonLogic.cs:line 47
  
  Exception caught : System.ArgumentException: Parameter is not valid.
     at System.Drawing.Graphics.GetHdc()
     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 MoonHandler.scraping.User32Facade.PrintWindow(IntPtr hWnd) in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\scraping\User32Facade.cs:line 50
     at MoonHandler.scraping.MoonFacade.getBMP(Boolean fresh) in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\scraping\MoonFacade.cs:line 37
     at MoonHandler.scraping.MoonFacade.isWarpDriveActive() in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\scraping\MoonFacade.cs:line 154
     at MoonHandler.logic.MoonLogic.warping_to_belt() in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\logic\MoonLogic.cs:line 113
     at MoonHandler.logic.MoonLogic.act() in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\logic\MoonLogic.cs:line 47
  
  Exception caught : System.ArgumentException: Parameter is not valid.
     at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
     at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
     at MoonHandler.scraping.User32Facade.PrintWindow(IntPtr hWnd) in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\scraping\User32Facade.cs:line 48
     at MoonHandler.scraping.MoonFacade.getBMP(Boolean fresh) in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\scraping\MoonFacade.cs:line 37
     at MoonHandler.scraping.MoonFacade.isWarpDriveActive() in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\scraping\MoonFacade.cs:line 154
     at MoonHandler.logic.MoonLogic.warping_to_belt() in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\logic\MoonLogic.cs:line 113
     at MoonHandler.logic.MoonLogic.act() in d:\users\npiv\documents\visual studio 2010\Projects\MoonMiner\ConsoleApplication2\logic\MoonLogic.cs:line 47
  </code></pre>
  
  <p>I realize these are 3 different exceptions, but I feel they are linked. Since it only starts happening at runtime, is this some sort of memory leak? Can I take the screen shot another way?</p>