3

I'm trying to make an application that I can open and minimize or just leave it there, then with the help of a hotkey I want it to capture a portion of a screen, I got all that covered and working fine except for when I hit the hotkey and I have a game window opened, instead of taking a screenshot of the game it takes a screenshot of what's underneath that window, I've searched literally everywhere and couldn't find an answer, this is the closest I got to: c# screenshot of active window but doesn't caputre the window

Here's my code:

Bitmap printscreen = new Bitmap(40, 20);
  Graphics graphics = Graphics.FromImage(printscreen as Image);
  graphics.CopyFromScreen(775, 515, 0, 0, printscreen.Size);
  printscreen = Transform(printscreen);
  Bitmap newImage = new Bitmap(100, 50);
  using (Graphics gr = Graphics.FromImage(newImage))
  {
   gr.SmoothingMode = SmoothingMode.HighQuality;
   gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
   gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
   gr.DrawImage(printscreen, new Rectangle(0, 0, 100, 50));
   newImage.Save("code.jpg");
   }

What I'm trying to achieve is capturing a portion of the screen of the active window or a specific window, thanks!

Community
  • 1
  • 1
moadi
  • 31
  • 1
  • 5
    That approach is not going to work for games or other applications that are bypassing the standard Windows graphics pipeline and using technologies like DirectX or OpenGL. Searching for *capture game screen* will yield a lot of results and approaches that involve using DirectX to capture or the DWM shared surface, E.g. http://stackoverflow.com/questions/5069104/fastest-method-of-screen-capturing – Alex K. Jul 29 '16 at 11:30
  • You have to use the overload that includes the CopyPixelOperation argument and include CaptureBlt to ensure you also copy layered windows. It however doesn't work correctly on older Windows versions, forcing you to do it [this way](http://stackoverflow.com/a/3072580/17034). – Hans Passant Jul 29 '16 at 13:44
  • @HansPassant it doesn't seem to work on latest Windows versions either, thanks for the help tho. – moadi Jul 30 '16 at 12:04

0 Answers0