问题
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 = HDC
handle, 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