Capture Screen: This does what is says; it basically captures the whole of the screen once the file name of the resulting image has been specified. https://downufile563.weebly.com/boom-2-the-best-audio-enhancement-app-1-2-1.html. Capture Area: Holding down the left mouse button, users draw a rectangle specifying which part of the screen they wish to capture. Need to capture your screen as a video file? Maybe you want to record a gameplay video, or create a tutorial on how to do something on your PC. For any number of reasons, knowing how to video screen capture can come in handy. Luckily, you can easily make these recordings with software known as VLC.
I was recently updating some documentation and wanted toprogrammatically capture some screenshots of the application indifferent states. This article describes how you can easilycapture screenshots in your own applications.
This code demonstrates how to get a screen shot of the entire desktop, or a particular window, using.NET (and a few API functions). The C# version is shown here, but the download includes a VB.NET version too. Using the class is simple - there are four methods public Image CaptureScreen.
This article makes use of a number of Win32 API methods.Although you may not have much call to use them directly in dayto day .NET (not to mention Microsoft wanting everyone to useuniversal 'apps' these days), they are still extraordinarilyuseful and powerful.
This article does assume you know the basics of platform invokeso I won't cover it here. In regards to the actual API's I'musing, you can find lots of information about them either onMSDN, or PInvoke.net.
A number of the API's used in this article are GDI calls.Generally, when you're using the Win32 GDI API, you need to dothings in pairs. If something is created (pens, brushes,bitmaps, icons etc.), then it usually needs to be explicitlydestroyed when finished with (there are some exceptions justto keep you on your toes). Although there haven't been GDIlimits in Windows for some time now (as far as I know!), it'sstill good not to introduce memory leaks. In addition, devicecontexts always have a number of objects associated with them.If you assign a new object to a context, you must restore theoriginal object when you're done. I'm a little rusty with thisso hopefully I'm not missing anything out.
Screen Capture Windows 10
Setting up a device context for use with BitBlt
To capture a screenshot, I'm going to be using the
BitBlt
API.This copies information from one device context to another,meaning I'm going to need a source and destination context toprocess.The source is going to be the desktop, so first I'll use the
GetDesktopWindow
and GetWindowDC
calls to obtain this. Ascalling GetWindowDC
essentially places a lock on it, I alsoneed to release it when I'm finished with it.Now for the destination - for this, I'm going to create a memorycontext using
CreateCompatibleDC
. When you call this API, youpass in an existing DC and the new one will be created based onthat.There's still one last step to perform - by itself, that memoryDC isn't hugely useful. We need to create and assign a GDIbitmap to it. To do this, first create a bitmap using
CreateCompatibleBitmap
and then attach it to the DC usingSelectObject
. SelectObject
will also return the relevant oldobject which we need to restore (again using SelectObject
)when we're done. We also use DeleteObject
Alexa app for apple mac. to clean up thebitmap.Although this might seem like a lot of effort, it's not all thatdifferent from using objects implementing
IDisposable
in C#,just C# makes it a little easier with things like the using
statement.With the above setup out the way, we have a device context whichprovides access to a bitmap of the desktop, and we have a newdevice context ready to transfer data to. All that's left to dois make the
BitBlt
call.If you've ever used the
DrawImage
method of a Graphics
object before, this call should be fairly familiar - we pass inthe DC to write too, along with the upper left corner where datawill be copied (0, 0
in this example), followed by the width
and height
of the rectangle - this applies to both the sourceand destination. Finally, we pass in the source device context,and the upper left corner where data will be copied from, alongwith flags that detail how the data will be copied.In my old VB6 days, I would just use
SRCCOPY
(direct copy),but in those days windows were simpler things. The CAPTUREBLT
flag ensures the call works properly with layered windows.If the call fails, I throw a new
Win32Exception
object withoutany parameters - this will take care of looking up the resultcode for the BitBlt
failure and filling in an appropriatemessage.Now that our destination bitmap has been happily 'painted' withthe specified region from the desktop we need to get it into.NET-land. We can do this via the
FromHbitmap
static method ofthe Image
class - this method accepts a GDI bitmap handle andreturn a fully fledged .NET Bitmap
https://sokolcommunity692.weebly.com/web-2-0-photo-montage.html. object from it.Putting it all together
As the above code is piecemeal, the following helper method willaccept a
Rectangle
which describes which part of the desktopyou want to capture and will then return a Bitmap
objectcontaining the captured information.Note the
try . finally
block used to try and free GDIresources if the BitBlt
or FromHbitmap
calls fail. Alsonote how the clean-up is the exact reverse ofcreation/selection.Now that we have this method, we can use it in various ways asdemonstrated below.
If you want to capture a window in your application, you couldcall
Capture
with the value of the Bounds
property of yourForm
. But if you want to capture an external window thenyou're going to need to go back to the Win32 API. Modern warfare battle royale beta. TheGetWindowRect
function will return any window's boundaries.Win32 has its own version of .NET's
Rectangle
structure, namedRECT
. This differs slightly from the .NET version in that ithas right
and bottom
properties, not width
and height
.The Rectangle
class has a helper method, FromLTRB
whichconstructs a Rectangle
from left, top, right and bottomproperties which means you don't need to perform the subtractionyourself.Depending on the version of Windows you're using, you may findthat you get slightly unexpected results when calling
Form.Bounds
or GetWindowRect
. As I don't want to digressto much, I'll follow up why and how to resolve in another post(the attached sample application includes the complete codefor both articles).C# Screen Capture
Capturing the active window
As a slight variation on the previous section, you can use the
GetForegroundWindow
API call to get the handle of the activewindow..NET offers the
Screen
static class which provides access toall monitors on your system via the AllScreens
property. Youcan use the FromControl
method to find out which monitor aform is hosted on, and get the region that represents themonitor - with or without areas covered by the task bar andother app bars. This means it trivial to capture the contents ofa given monitor.Capturing the entire desktop
It is also quite simple to capture the entire desktop withouthaving to know all the details of monitor arrangements. We justneed to enumerate the available monitors and use
Rectangle.Union
to merge two rectangles together. When this iscomplete, you'll have one rectangle which describes allavailable monitors.There is one slight problem with this approach - if theresolutions of your monitors are different sizes, or aremisaligned from each other, the gaps will be filled in solidblack. It would be nicer to make these areas transparent,however at this point in time I don't need to capture thewhole desktop so I'll leave this either as an exercise for thereader, or a subsequent update.
https://besttfil991.weebly.com/how-do-you-install.html. Of course, you could just call
CaptureRegion
with a customrectangle to pick up some arbitrary part of the desktop. Theabove helpers are just that, helpers!C++ Screenshot
A note on display scaling and high DPI monitors
Although I don't have a high DPI monitor, I did temporarilyscale the display to 125% to test that the correct regions werestill captured. I tested with a manifest stating that theapplication supported high DPI and again without, in both casesthe correct sized images were captured.
A demonstration program for the techniques in this article isavailable from the links below. It's also available onGitHub.
Update History
- 2017-08-27 - First published
- 2020-11-22 - Updated formatting
![Capture Capture](https://askleo.askleomedia.com/wp-content/uploads/2008/07/cmd_prompt_torn.png)
Filename | Description | Version | Release Date |
---|---|---|---|
SimpleScreenshotCapture.zip
| Sample project for the capturing screenshots using C# and p/invoke article. | 27/08/2017 | Download |
While we appreciate comments from our users, please follow our posting guidelines. Have you tried the Cyotek Forums for support from Cyotek and the community?
Ram S
![Free windows screen capture software Free windows screen capture software](https://www.equipwater.com/wp-content/uploads/2019/11/manual-basket-screen-equipwater.jpeg)
I love your code above - but i need the code to be in VBScript. Not sure how i can save the above as .vb and then convert it to .vbs (Vbscript code)? Any help?