Window screenshot using WinAPI

放肆的年华 提交于 2020-01-24 17:25:05

问题


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);

回答1:


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.




回答2:


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;



回答3:


See http://www.developerfusion.com/code/4630/capture-a-screen-shot/



来源:https://stackoverflow.com/questions/2843357/window-screenshot-using-winapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!