I am making a screen capturing application and everything is going fine. All I need to do is capture the active window and take a screenshot of this active window. Does anyone know how I can do this?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Here is a snippet to capture either the desktop or the active window. It has no reference to Windows Forms.
How to capture the whole screen:
How to capture the active window:
Originally found here: http://www.snippetsource.net/Snippet/158/capture-screenshot-in-c
If you want to use managed code: This will capture any window via the ProcessId.
I used the following to make the window active.
I used the print screen to capture a window.
http://www.developerfusion.com/code/4630/capture-a-screen-shot/
for capturing current window use
Based on ArsenMkrt's reply, but this one allows you to capture a control in your form (I'm writing a tool for example that has a WebBrowser control in it and want to capture just its display). Note the use of PointToScreen method:
Having the Bitmap object you can just call Save on it
The above assumes the GC will grab the bitmap, but maybe it's better to assign the result of someControl.getScreenshot() to a Bitmap variable, then dispose that variable manually when finished, especially if you're doing this grabbing often (say you have a list of webpages you want to load and save screenshots of them):
Update:
Now WebCapture tool is ClickOnce-deployed (http://gallery.clipflair.net/WebCapture) from the web (also has nice autoupdate support thanks to ClickOnce) and you can find its source code at http://ClipFlair.codeplex.com.
I assume you use
Graphics.CopyFromScreen
to get the screenshot.You can use P/Invoke to GetForegroundWindow (and then get its position and size) to determine which region you need to copy from.