Important alert: (current site time 7/15/2013 9:16:18 AM EDT)
 

winzip icon

ScreenCapture

Email
Submitted on: 12/27/2003 6:26:50 PM
By: SpaceMonkey 
Level: Advanced
User Rating: By 19 Users
Compatibility: VB.NET
Views: 29368
 
     ScreenCapture.vb is a .Net class that allows you take a screenshot (printscreen) of the desktop, or part of it, or of the active window, or of any window you got the HWND from. It doesn't use sendkeys or the clipboard, this is a proper implementation with API. Basicly the class exists out of two shared functions that are overloaded a bit, give me a vote if you like it. Usage: PictureBox1.Image = ScreenCapture.GrabScreen() PictureBox1.Image = ScreenCapture.GrabActiveWindow() PictureBox1.Image = ScreenCapture.GrabWindow(SomeHwnd) PictureBox1.Image = ScreenCapture.GrabScreen(X, Y, Width, Height) PictureBox1.Image = ScreenCapture.GrabScreen(Rect) PictureBox1.Image = ScreenCapture.GrabScreen(Location, Size)
 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
12/28/2003 5:58:16 AMPietro ing. Cecchi

all we needed about capture...
I give you 5
(If this comment was disrespectful, please report it.)

 
12/30/2003 7:14:19 AMSameers (theAngrycodeR)

Hello,

Is there a way to capture a hidden form? nice code BTW.

Sameers
(If this comment was disrespectful, please report it.)

 
1/2/2004 9:42:47 PMFish123456

This code has a memory leak in it... It's not releasing all the memory it uses after each pass.
(If this comment was disrespectful, please report it.)

 
1/3/2004 5:44:44 AMSpaceMonkey

Fish123456:

There is no memory leak in the code, memory is released, but the Garbage Collector of .Net is just a bit slow to release it.

If the memory usage is a problem you can call "GC.Collect()" to have the Garbage Collector clean up immediately.


(If this comment was disrespectful, please report it.)

 
1/4/2004 6:07:25 PMFish123456

Ok, you are right space monkey. The memory was released, but only after up to 60 MB's had been used. The GC.collect method works fine, but I do not know the CPU usage overhead.
(If this comment was disrespectful, please report it.)

 
1/23/2004 3:40:28 PM

I really like the idea, it would come in very very handy for something that I am trying to do but on the web using asp.net. I am trying to use it w/ this and I'm just getting a big black box. Is there any way to simulate a printscreen to a web browser?
(If this comment was disrespectful, please report it.)

 
1/23/2004 3:41:25 PM

This is a great idea. Will it work to printscreen on a web browser. I am trying to use it in a asp.net web application and my bitmap is a big black box.
(If this comment was disrespectful, please report it.)

 
1/24/2004 4:11:23 AMSpaceMonkey

When using ASP you are dealing with security settings and limited access to the target system, I don't think you can use an API based solution for this.

Never done ASP myself (yet), can't really help you out on that one.

(If this comment was disrespectful, please report it.)

 
2/21/2004 5:35:38 AM

thats gr8, 5 for you
(If this comment was disrespectful, please report it.)

 
2/25/2004 12:07:57 AM

its good
(If this comment was disrespectful, please report it.)

 
5/5/2004 6:36:10 PMNelson Lamprecht

I have used this code, by creating a silent app and calling from a webpage using psexec to execute it on a remote webpage and then display the resulting saved imaged in that webpage. Works great for my role in helpdesk...:)
(If this comment was disrespectful, please report it.)

 
9/7/2004 5:04:23 PM

Please email me the code, it isn't available on Planet-Source-Code.

Thanks
(If this comment was disrespectful, please report it.)

 
9/8/2004 3:22:22 AMSpaceMonkey

Use the 'download code' link next to the Winzip icon to download the source code...
(If this comment was disrespectful, please report it.)

 
2/10/2005 11:42:37 PM

hi,
excelent code... but :)

i am using this to take screen caps of active window 5times per sec, to anaylze and interact with another program....

after ~80 minutes every time, it crashes and the interface gets quite screwey, also the line:
hOffscreenDC = CreateCompatibleDC(hWindowDC)
returns 0 to hOffscreenDC every time there after.

any idea why this might happen?
(If this comment was disrespectful, please report it.)

 
2/19/2005 5:28:32 AMSpaceMonkey

5 Times a second is alot, you are probaly running out of virtual memory by the slow garbage collecting by .NET.

Try calling GC.Collect() once in a while to force .NET to collect the freed memory.

Also you might recode the function so that it doesn't create new device contexes all the time. You could create them once at program start en release them at the end of the program, reusing them during runtime.

Make sure you properly release the bitmaps returned by these functions as well.
(If this comment was disrespectful, please report it.)

 
4/5/2005 4:56:52 PM

Trying to use the GrabWindow(somehWnd) which in your code expects an Int32. I realize that the low level Win API uses Int32 for handles, but obtaining a handle from a control in .Net provides a System.IntPtr. What procedure are you recommending to get the Handle as an Integer?
(If this comment was disrespectful, please report it.)

 
4/6/2005 2:58:00 AMSpaceMonkey

On of the member functions of the System.IntPtr class is ToInt32(), which will return the handle as Int32.

So you can use something like MyForm.Handle.ToInt32()

Thanks for the feedback, and remember to vote for the code if you find it useful :o)
(If this comment was disrespectful, please report it.)

 
4/6/2005 8:45:01 AM

Thamks for the rapid response. Your code does seem to work well at what it does. I have seen other code streams that work similarly. The one thing I have been unable to find is a method that will capture a hidden window (one that is not topmost). There was an MSKB article Q194580 that worked for VB, but does not seem effective for .Net.
(If this comment was disrespectful, please report it.)

 
9/28/2005 4:51:31 AMArvin Meralpis

Is there a pure, manage, dotNet way of doing this?
(If this comment was disrespectful, please report it.)

 
9/28/2005 9:54:49 AMSpaceMonkey

Arvin, no there isn't
(If this comment was disrespectful, please report it.)

 
11/5/2005 9:10:19 PMIndra Kurniawan

awesome, you saved my time. thanks for that.
(If this comment was disrespectful, please report it.)

 
2/25/2006 1:41:34 AMjsa

excellent code,keep it up
(If this comment was disrespectful, please report it.)

 
4/11/2006 2:53:33 PMSpencer Lerch

Great code that saved me a lot of time for a project that is short on time. This will be a huge help with error resolution. Thanks
(If this comment was disrespectful, please report it.)

 
7/31/2006 5:42:26 AMSUN

This is wonderful.
is there any way to capture the screen of another wiondow?
i mean i open anopther website on another browser window and how can i capture only that window?

Thank you
(If this comment was disrespectful, please report it.)

 
11/24/2006 4:59:36 PMJesse Yeager

Thanks. This helped me a lot, trying to move some VB6 code to .NET. I did it without the SelectObject and compatible DC stuff.

Dim desktopDC As Integer
desktopDC = GetDC(0)

Dim hBitmap As Integer
hBitmap = GetCurrentObject(desktopDC, OBJ_BITMAP)

Dim objBMP As Bitmap
objBMP = Bitmap.FromHbitmap(New IntPtr(hBitmap))

picBox.Image = objBMP
picBox.Refresh()

ReleaseDC(0, desktopDC)
objBMP = Nothing

(If this comment was disrespectful, please report it.)

 
8/18/2007 11:31:11 PMJose Pablo Islas

Why not just use Graphics.CopyFromScreen
(If this comment was disrespectful, please report it.)

 
8/18/2007 11:39:25 PMJose Pablo Islas

Public Shared Function GetScreenShot() As Image
Dim screenBounds As Rectangle = Screen.PrimaryScreen.Bounds
Dim theImg As New Bitmap(screenBounds.Width, screenBounds.Height)
Dim g As Graphics = Graphics.FromImage(theImg)
g.CopyFromScreen(0, 0, 0, 0, New Size(screenBounds.Width, screenBounds.Height))

Return theImg
End Function
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.