|
-
Aug 23rd, 2010, 09:21 PM
#41
New Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
I am having a weird problem and was hoping someone could offer a pointer or two...
Consider the following sub:
Code:
Private Sub _timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim sc As New ScreenShot.ScreenCapture
Debug.Print("Tick: " & Now)
If rdp.Count > 0 Then
For Each r As _rdp In rdp
r.pic.Image = sc.CaptureWindow(r.rdp.Handle).GetThumbnailImage(160, 120, Nothing, Nothing)
Next
End If
sc = Nothing
End Sub
It triggers every 3 seconds, refreshing the r.pic.image with a screen cap from r.rdp.handle. Pretty straight forward, right? Well, except what is happening is that the resulting image is re-shrunk every iteration so the first image is 160x120, the second is much smaller, the third - even smaller, the fourth is basically a single pixel and from 5 on it is nothing.
If I run without GetThumbnailImage(), the image in the picturebox moves every iteration by roughly 30 pixels in both X and Y.
Anyone ever run into this before?
HELP!
Ron
-
Apr 11th, 2011, 03:18 PM
#42
Addicted Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
 Originally Posted by gigemboy
replaced all the API declarations with the correct working ones
Nice job. I stepped in looking to remember how we used to do it in previous versions, and then I remembred the main matter: using unmanaged code (=calling Api directly somewhere in code) steal one of the main goals of a framework like Net: portability.
Indeed there was not other way to do it in 1.0 and, i do belive, in 1.1. since 2.0 we all are a bit more lucky and we can grab screen easier in a managed mode.
an easy example - nothing to compare with the wonderful job of gigemboy - :
a form ("frmCapture"), two buttons ("btnWholeScreen", "btnThisForm") a pictureBox ("picCaptured") to show result is acquired (or you can save to a file).
Code:
Public Class frmCapture
Private Sub frmCapture_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
picCaptured.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Private Sub btnWholeScreen_Click(sender As System.Object, e As System.EventArgs) Handles btnWholeScreen.Click
Dim Bmp As Bitmap = GetPicture(My.Computer.Screen.Bounds, New Point(0, 0))
'Bmp.Save("C:\CapturedScreen.png")
picCaptured.Image = Bmp
End Sub
Private Sub btnThisForm_Click(sender As System.Object, e As System.EventArgs) Handles btnThisForm.Click
Dim Bmp As Bitmap = GetPicture(Me.Bounds, New Point(Me.Left, Me.Top))
'Bmp.Save("C:\CapturedForm.png")
picCaptured.Image = Bmp
End Sub
Private Function GetPicture(bounds As Rectangle, upleftSource As Point) As Bitmap
Dim retBmp As New Bitmap(bounds.Width, bounds.Height)
Dim G As Graphics = Graphics.FromImage(retBmp)
G.CopyFromScreen(upleftSource, New Point(0, 0), bounds.Size, CopyPixelOperation.SourceCopy)
G.Dispose()
Return retBmp
End Function
Special thanks to some wonderful people,
such as Lothar the Great Haensler, Aaron Young,
dr_Michael, Chris Eastwood, TheOnlyOne ClearCode....
-
Jun 10th, 2011, 09:05 PM
#43
New Member
How do you draw area with mouse?
I can easily get the form or whole desktop captured. What I'm having an issue with is being able to select the area to capture with the mouse. I'm very surprised that hasn't been covered in this thread.
Can someone please post code to capture the screen that you select by drawing a box with the mouse?
Thanks in advance
Schrady
-
Jun 11th, 2011, 11:36 AM
#44
Addicted Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
A trick to capture a region could be: make the whole screen capture, then show that picture and let user use the mouse to get a portion of it. It seems to me PickPick (a free utility to do that job among others) uses that logic.
Special thanks to some wonderful people,
such as Lothar the Great Haensler, Aaron Young,
dr_Michael, Chris Eastwood, TheOnlyOne ClearCode....
-
Jun 12th, 2011, 02:46 PM
#45
Banned
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
screen capture code :
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim screenBounds = Screen.PrimaryScreen.Bounds
Dim screenShot As New Bitmap(screenBounds.Width, screenBounds.Height)
Using g = Graphics.FromImage(screenShot)
g.CopyFromScreen(screenBounds.Location, Point.Empty, screenBounds.Size)
End Using
PictureBox1.Image = screenShot
'Dim filePath = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyPictures, "ScreenShot.bmp")
'screenShot.Save(filePath, Imaging.ImageFormat.Bmp)
End Sub
End Class
-
Aug 11th, 2011, 02:55 AM
#46
New Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
If you want to use some screen recording software to help you record movement on screen, you should choose the right program. The one I used can help me capture whatever streaming video and audio files on screen with high quality, and it will also help me save it to standard video file. And you'll be able to upload your tutorial video to video sharing website or share with your friends.
-
Oct 12th, 2011, 03:01 PM
#47
New Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
This is a strange error:
I am using this library to capture the contents of a web browser which is in another window.
First, I find the window using this:
For Each p As Process In Process.GetProcesses
If p.MainWindowTitle = "whatever" Then
handle = p.MainWindowHandle
Next
I can then look at the image with this:
Dim sc As New ScreenCapture
Dim bmp As Bitmap = CType(sc.CaptureWindow(handle), Bitmap)
The odd thing is that the image I get is of a RECENT window state, but not the current state. For example, suppose I go to a web page with a form on it and click submit and then get a response, AND THEN run the code above. The resulting bmp contains an image of the screen from BEFORE I clicked submit.
I am running vista. Any thoughts?
-
Mar 14th, 2012, 07:27 PM
#48
Hyperactive Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
I have been using this for awhile and just recently I have decided to test the speed of it. I am capturing a window (480, 320) that runs at 60fps. I found using this test that, even though I capture twice immediately after each other, the captures are not always exactly the same. Below is the code I used. It's about as fast as I can make it.
Notice the two calls to CaptureWindowRegion. They happen right after each other. No other code or delays between them. 60fps means that each frame takes 16 milliseconds. This code finished an iteration in an average of 20 milliseconds, but most of that time is spent comparing the bitmaps.
My question is: Are there any updated function in the newest version of .NET that could speed this capturing process up? Any way to upgrade this code to be faster?
Stats for this code:
- Iterations: 5000
- Mismatches: 252
- Mismatch Percent: 5.04
- Time to complete: 104780.9931 (milliseconds)
Here is the test code I used.
vb Code:
Friend Sub test()
While Loaded
' If the VBA hasn't been attached to yet, it has closed or the WRAM or IRAM are not set...
If proc Is Nothing OrElse proc.HasExited Then
For Each p As Process In Process.GetProcesses
If p.MainWindowTitle.Contains("VisualBoyAdvance") Then
' Found VBA. Open it with full access.
proc = p
hWnd = p.MainWindowHandle
hProcess = OpenProcess(&H1F0FFF, False, p.Id)
Exit For
End If
Next
time = Date.Now ' Start time excluding time looking for the process.
Else
If cbCapture.Checked Then
' Make we sill have the window handle then grab the screen.
If hWnd = IntPtr.Zero Then
hWnd = proc.MainWindowHandle
Else
bmptemp = New Bitmap(240, 160)
bmptemp2 = New Bitmap(240, 160)
' Grab the screen twice.
bmpSrcBig = sc.CaptureWindowRegion(hWnd, New Rectangle(8, 50, 480, 320))
bmpSrcBig2 = sc.CaptureWindowRegion(hWnd, New Rectangle(8, 50, 480, 320))
' Draw the screen grabs to another bitmap, shrinking them to normal size.
Using g As Graphics = Graphics.FromImage(bmptemp)
g.InterpolationMode = Drawing2D.InterpolationMode.Bilinear
g.DrawImage(bmpSrcBig, 0, 0, 240, 160)
End Using
Using g As Graphics = Graphics.FromImage(bmptemp2)
g.InterpolationMode = Drawing2D.InterpolationMode.Bilinear
g.DrawImage(bmpSrcBig2, 0, 0, 240, 160)
End Using
' Lock the bits.
bmpDataA = bmptemp.LockBits(VBAScreenRect, ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb)
bmpDataB = bmptemp2.LockBits(VBAScreenRect, ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb)
' Check if they are not identical.
Check()
' Unlock the bits.
bmptemp.UnlockBits(bmpDataA)
bmptemp2.UnlockBits(bmpDataB)
' Increase iteration, display percentage and check if we are finished.
SetText(Me, (mismatch / iteration) * 100 & " | " & Date.Now.Subtract(time).TotalMilliseconds)
If iteration = 5000 Then
sb = New StringBuilder
sb.Append("Iterations: ")
sb.AppendLine(iteration)
sb.Append("Mismatches: ")
sb.AppendLine(mismatch)
sb.Append("Mismatch Percent: ")
sb.AppendLine((mismatch / iteration) * 100)
sb.Append("Time to complete: ")
sb.Append(Date.Now.Subtract(time).TotalMilliseconds)
MessageBox.Show(sb.ToString)
Exit Sub
End If
iteration += 1
End If
End If
End If
End While
End Sub
Friend Sub Check()
For y1 As Int32 = 0 To 159
For x1 As Int32 = 0 To 239
If Marshal.ReadInt32(bmpDataA.Scan0, (bmpDataA.Stride * y1) + (4 * x1)) <> _
Marshal.ReadInt32(bmpDataB.Scan0, (bmpDataB.Stride * y1) + (4 * x1)) Then
mismatch += 1
Exit Sub
End If
Next
Next
End Sub
Prefix has no suffix, but suffix has a prefix.
-
Mar 15th, 2012, 07:33 AM
#49
New Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
Troy-
Does that code work for an INACTIVE window, one that is hidden behind another window?
-
Mar 15th, 2012, 05:36 PM
#50
Hyperactive Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
 Originally Posted by mankowitz
Troy-
Does that code work for an INACTIVE window, one that is hidden behind another window?
Yes.
Prefix has no suffix, but suffix has a prefix.
-
Apr 11th, 2012, 12:17 PM
#51
Frenzied Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
Did you ever find a solution for Windows 7 saving it as a black image?
-
May 1st, 2012, 02:04 AM
#52
New Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
I have 2 monitors and i am getting black capture from one of them (not the primary).
If i define a rectangle that spans from monitor 1 to monitor 2 i am getting black for the portion of the secondary monitor and the normal contents from the primary one.
What i have to do to the class to get correct results?
-
May 1st, 2012, 02:06 AM
#53
New Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
I define the rectangle by :
vb Code:
myRect.Location = Me.Location myRect.Height = Me.Height myRect.Width = Me.Width
Me is a transparent form...
-
May 1st, 2012, 02:19 AM
#54
New Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
it seems CaptureScreen gets only the primary screen...
Any way to capture all desktop?
-
Jun 17th, 2012, 03:35 PM
#55
New Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
Hi! First of all I just wanted to say thanks for this wonderful piece of code. There is one thing I'm having troubles, though. I am trying to take a take a screenshoot of a message box of a game, but for some reason it only takes a screenshoot of the game but ignores the message box. It works perfect on windows 7, but it does not work on windows XP.
Here's a picture:
http://imageshack.us/photo/my-images...blewindow.png/
Any thoughts?
-
Jun 19th, 2012, 10:17 AM
#56
New Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
Also, if it helps... The problem also occurs whenever I disable Aero in Windows 7.
-
Nov 25th, 2012, 05:00 PM
#57
New Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
So what I need to get this working? pictureboxes? buttons?
-
Jan 30th, 2013, 03:23 AM
#58
New Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
The only way I have been able to get around getting black screenshots is to disable desktop composition by right clicking the exe and going to properties and ticking it then applying and saving, however in windows 8 this option is gone I hope that helps with users on win7 however is their a way with code to force an application in win8 to disable desktop composition?
-
Feb 8th, 2026, 12:22 AM
#59
Junior Member
Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
Oh man, this is a nice piece of code here. Kudos!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|