Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi There, Im using Vb.net Framework 4.

I was wondering wow do i take a screenshot of only an active windows....similar to the ALT+PrtScn. I would like to implement a screen capture for errors handling. I have current code that is able to take screenshots but it takes a screen capture of the entire primary monitor. Any Help or Guidances would be great.


Private Function TakeImage()
      Return TakeImage(0, 0, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
  End Function


Private Function TakeImage(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer)
      Dim Img As New Bitmap(Width, Height)
      Dim g As Graphics = Graphics.FromImage(Img)
      g.CopyFromScreen(X, Y, 0, 0, Img.Size)
      g.Dispose()

      Return Img
  End Function




After having a look around googs abit more found handy bit of code for what im looking for....maybe it may help you. (The code basically takes a screenshot of the active window)
Dim bm As New Bitmap(Me.Width, Me.Height)
       Dim g As Graphics = Graphics.FromImage(bm)
       g.CopyFromScreen(Me.Location, New Point(0, 0), New Size(Me.Width, Me.Height))
       bm.Save("C:\formgrab.bmp", Drawing.Imaging.ImageFormat.Bmp)
Posted
Updated 30-Aug-11 9:27am
v2

1 solution

1) Capture the entire screen
2) Find the location and dimension of the window you want to capture
3) Use the information from 2) to get the window capture from the image from 1)

TeboScreen: Basic C# Screen Capture Application[^]
This article can help you learn how you do the actual capture.

If your target platform is Windows Vista or later, then you can choose to disregard the above and use the Dynamic Window Manager (DWM).
Have a look at Thumbnailgadget: Windows Vista API meets the Sidebar[^] and
Vista Thumbnail-Enhanced Virtual Desktop Manager[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Aug-11 17:29pm    
That will certainly work, by 5, but don't you think there should be a capture for just one window initially, somewhere?
--SA
Simon Bang Terkildsen 30-Aug-11 17:34pm    
Most definitely I actually wrote that, but I were not able to keep it simple and easy to read so I decided to remove it.
To OP:
The first article shows how to capture a specific area of the screen, so if you swap 1) and 2) you have the position and dimension and you can then use that to only capture the part of the screen you care about.

The DWM option captures the Window only even if another window is on top of it, it wont work, however, if the window is minimized.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900