Important alert: (current site time 7/15/2013 9:20:31 AM EDT)
 

winzip icon

Capture Screen

Email
Submitted on: 6/5/2002 6:16:56 AM
By: Masoud (M.D) 
Level: Intermediate
User Rating: By 15 Users
Compatibility: VB.NET
Views: 82600
(About the author)
 
     This code can make a picture from Screen. (API)
 
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 3 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 Intermediate 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
7/1/2002 3:08:37 AMBug bitch

Good code. Only very little mistake... Const SRCCOPY is not used... the numeric equivalent is. No big deal. I rated a 5 anyway. Keep sharing with us more code. Thanks
(If this comment was disrespectful, please report it.)

 
7/1/2002 5:08:18 AMMasoud (M.D)

Thank you for your registration.
There isn't SRCCOPY in .NET or i don't know.
anyway thank you.
(If this comment was disrespectful, please report it.)

 
9/29/2002 6:53:08 PM

Any way to make this work in c#.net?
(If this comment was disrespectful, please report it.)

 
10/31/2002 7:04:39 AMZoltan Kasnya

The code is nice...
It's very clear and tight.
Keep up.
(If this comment was disrespectful, please report it.)

 
10/31/2002 1:51:49 PMMasoud (M.D)

Thanks a lot for your attention and support.
(If this comment was disrespectful, please report it.)

 
11/10/2002 5:30:12 PM

+5 for newbie help that will be used by many!
(If this comment was disrespectful, please report it.)

 
11/25/2002 8:15:36 AMDave Andrews

5/5 from me. I am surprised that this can't be done without API calls. Great job.
(If this comment was disrespectful, please report it.)

 
12/3/2002 1:46:42 AMMasoud (M.D)

Thanks a lot for your attention.
(If this comment was disrespectful, please report it.)

 
2/28/2003 9:37:08 AMTropical Wire

Hey, ure code is good. But you can use the sendkeys
(If this comment was disrespectful, please report it.)

 
3/10/2003 8:50:16 AMMasoud (M.D)

Thanks a lot for your supporting.
(If this comment was disrespectful, please report it.)

 
6/7/2003 9:46:06 PM

Hi, found one problem with the code. This code will not capture a form that has an opacity of anything other than 100%. Even if it has 100% opacity then it gets changed.

what i'm trying to is trying to load a form without it flickering as follows:

me.opacity = 0
me.show
application.doevents()
me.opacity = 100

once i try running the screen capture code after this, it does not capture my actual screen. It seems to capture what is behind it (folders, desktop, etc).

Once i comment out the line me.opacity = 0, the capture works perfectly.

I do need to have that line to stop my program from flickering when it loads.

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

 
6/10/2003 3:45:24 PMMasoud (M.D)

I've sent the answer for you dearest.
(If this comment was disrespectful, please report it.)

 
6/10/2003 6:41:18 PM

This is another method which captures screen shots. It works well. Need any info, let me know.

Cheers

Jason


Public Function GetScreenCapture( _
Optional ByVal FullScreen As Boolean = False) As Bitmap
' Captures the current screen and returns as an Image
' object
Dim objSK As SendKeys
Dim imgCapture As Image
If FullScreen = True Then
' Print Screen pressed twice here as some systems
' grab active window "accidentally" on first run
objSK.SendWait("{PRTSC 2}")
Else
objSK.SendWait("%{PRTSC}")
End If
Dim objData As IDataObject = Clipboard.GetDataObject()
Return objData.GetData(DataFormats.Bitmap)
End Function
(If this comment was disrespectful, please report it.)

 
6/10/2003 6:42:37 PM

Sorry about the formatting above. If you cant get it to work, email me.

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

 
10/14/2003 9:04:19 PMJon Davis

Converted this to C#: http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=1663&lngWId=10
(If this comment was disrespectful, please report it.)

 
11/3/2003 6:22:14 PM

Memory Leek?

If you run the code and hit the capture button, approximatly 3K of memory is stolen each time. Memory seems to be stolen in the "Image.FromHbitmap" statement. Is there a memory hole in this method? I cant seem to find a way around this memory leek. Any ideas?
(If this comment was disrespectful, please report it.)

 
11/3/2003 6:42:57 PM

gc.Collect seems to fix my memory leek. I can seem to figure out why it was a problem in the first place. Silly Garbage Collection!
(If this comment was disrespectful, please report it.)

 
12/4/2003 11:57:34 AM

hi, I've try your capture screen code and I see that first the capturing the mouse pointer was hide and show then.
There is a function that non hide the pointer during the capture.

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

 
12/11/2003 2:34:56 PM

First off- code works great and is much faster than using a print screen to capture and copy to the clipboard. However, I also am experiencing a memory leak- mine is 3MB each time the screenshot runs. I've tried using the gc.collect(), but my code bombs out when I do (some kind of null pointer exception or something). It still bombs out after a long period of running without gc.collect(), but I'm guessing its due to the memory leak (in my tests, I'm taking quite a few screenshots).

Any suggestions?

Thanks again.
Tony

When I finish I'll probably upload what I have for anyone that's interested. Its a very thorough and complete monitoring solution which can be used or modified in many ways.

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

 
12/19/2003 6:12:26 AM

Do you have any idea about how to use mouse pointer to capture a portion of the window or screen. Currntly it is capturing whole desktop area.
(If this comment was disrespectful, please report it.)

 
3/20/2004 2:45:00 AM

Nice code. But one hitch. If I use this in a VB.Net Service project to capture the screen at regular interval, BitBlt function fails with error 6. Any clues.
(If this comment was disrespectful, please report it.)

 
5/7/2004 5:44:35 AM

that work with the exec("...HQScreen.exe....")
but apache have to be started not as service
(If this comment was disrespectful, please report it.)

 
7/4/2004 9:19:30 PMmrchan

use getcursorpos() api to get cursor api and bitblt with the positions. this is cool code man.
(If this comment was disrespectful, please report it.)

 
8/2/2004 4:21:25 AM

Hi All,

The screen capture code works fine for stand-alone applications. But if you try to run it from a service, it will show a black square instead of a screenshot. Sometimes changing display mode solves the problem. Does anybody know why that happens and how to fix that?

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

 
10/7/2004 5:20:17 AMNabil Bou Diab

Very neat very cool code ... i'll give you another 5
(If this comment was disrespectful, please report it.)

 
10/7/2004 9:17:30 AMMasoud (M.D)

That's kind of all of you that download this code and even use it.
I'm really happy that this code could help some of you.
Thanks a million.
(If this comment was disrespectful, please report it.)

 
11/25/2004 6:06:18 AM

Well, the code was good as long as I ran it on my desktop. I ran it on another desktop and then closed the rdesktop connection, letting the program run. All I got was black screens
(If this comment was disrespectful, please report it.)

 
11/25/2004 6:07:41 AM

Screen capture or window capture works fine if I run it on my desktop. However if I lock my screen and let the program run, everything is black. Same if I run it on a rdesktop and minimize the rdesktop window. Any way to get around these ?
(If this comment was disrespectful, please report it.)

 
11/25/2004 10:42:14 AMMasoud (M.D)

You should use remoting and send your pictures in bytes from server to the client. In this way you have to draw mouse pointer on your own. Drawing mouse pointer is very easy.
Thank you.
(If this comment was disrespectful, please report it.)

 
11/26/2004 5:42:35 AM

I dont have a server or a client system, I just need to record whatever is going on in a window onto an avi file so I send successive bmp captures into an avi file. The problem is, if that window is not shown, or it is overlapped by another one, or is simply not visible because the screen is locked, this method of capture is not working. I'd really appreciate if you could suggest us a work-around wherein, given a window handle, I can grab the screenshot irrespective of whether that window is visible or not. Thanks
(If this comment was disrespectful, please report it.)

 
11/26/2004 9:45:58 AMMasoud (M.D)

You can use another APIs to see whether that window is visible or not or to see that window is minimize or not. I think searching for these kind of APIs will help you.
Thank you.
(If this comment was disrespectful, please report it.)

 
12/23/2004 8:07:44 PMSAndrewH

Anyone have fix for the memory leak, yet?
(If this comment was disrespectful, please report it.)

 
2/16/2005 5:25:51 PM

First off, awesome code for a stand alone app. I'm rating it 5 stars, if for nothing else but as a great learning tool.

That said, I'm also having the problem with capturing only a black image if I try to run this in the code behind of an asp.net web page (or from directly on the page, for that matter). It doesn't matter what image format I choose, either.

From what I've seen in searching other locations on the net, this seems to be a common problem. Has anyone found or developed a good workaround/solution for this?
(If this comment was disrespectful, please report it.)

 
2/24/2006 10:54:01 AMjsa

hi can any 1 suggest method to avoid memory leak(its approx 3MB of memory)every time i click on capture button
(If this comment was disrespectful, please report it.)

 
8/1/2006 4:24:14 PMRohit

VB 2005 has an even easier way to do this

bmpScreenshot = New Bitmap(Screen.PrimaryScreen.Bounds..Width, Screen.PrimaryScreen.Bounds..Height, PixelFormat.Format32bppArgb)
' Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot)
' Take the screenshot from the upper left corner to the right bottom corner

gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
' Save the screenshot to the specified path that the user has chosen
bmpScreenshot.Save("c:\test.png", ImageFormat.Png)
(If this comment was disrespectful, please report it.)

 
8/5/2006 5:02:55 AMMasoud (M.D)

Yes, this is added to .NET Framework 2 and is really easy but we had not it in .NET Framework 1.
(If this comment was disrespectful, please report it.)

 
10/17/2006 9:38:54 AMnsrijo

That said, I'm also having the problem with capturing only a black image if I try to run this in the code behind of an asp.net web page (or from directly on the page, for that matter). It doesn't matter what image format I choose, either.

From what I've seen in searching other locations on the net, this seems to be a common problem. Has anyone found or developed a good workaround/solution for this?


How Can i solve this problem. I want Screen shot capture in asp.net (web Application)
(If this comment was disrespectful, please report it.)

 
12/8/2006 12:13:04 PMShyam

Hi

I have an application which lists all the active applications in a combo box and lets the user select an application. I am trying to get the handle to this selected application and calling the CaptureWindowToFile function and trying to save it as a bmp. Unfortunately I am unable to get the screen shot of my selected application.

Any help in this regard will be greatly appreciated.

I am using Microsoft Dev Environment 2003, ver 7.1.3088 and .NET framework 1.1

Thanks in Advance,

Shyam

(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.