Tuesday, April 5, 2011

Window screenshot using WinAPI

How to make a screenshot of program window using WinAPI & C#?

I sending WM_PAINT (0x000F) message to window, which I want to screenshot, wParam = HDChandle, but no screenshot in my picturebox. If I send a WM_CLOSE message, all waorking (target window closes). What I do wrong with WM_PAINT? May be HDC is not PictureBox (WinForms) component? P.S. GetLastError() == ""

[DllImport("User32.dll")]
public static extern Int64 SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  .....

SendMessage(targetWindowHandle, 0x000F, pictureBox.Handle, IntPtr.Zero);
From stackoverflow
  • pictureBox.Handle is a window handle, not a DC handle. There are several guides online for doing screenshots. One is here. See also @In silico's answer.

    Evl-ntnt : As will be to write then? To copy the image in any place
  • See http://www.developerfusion.com/code/4630/capture-a-screen-shot/

  • You can also take screenshot using purely managed code without the need for interop. The following code will take a snap of a 100x100 area of the screen, of course you can adjust to the full screen. The key function is Graphics.CopyFromScreen

      Bitmap bmp = new Bitmap(100,100);
      using (Graphics g = Graphics.FromImage(bmp))
      {
        g.CopyFromScreen(0, 0, 0, 0, new Size(100, 100));        
      }
      pictureBox1.Image = bmp;
    

0 comments:

Post a Comment