Page 2 of 2 FirstFirst 12
Results 41 to 59 of 59

Thread: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls

  1. #41
    New Member
    Join Date
    Aug 2008
    Location
    Westland Michigan
    Posts
    3

    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

  2. #42
    Addicted Member Cimperiali's Avatar
    Join Date
    Oct 2002
    Location
    Milan, Italy, Europe
    Posts
    188

    Post Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls

    Quote Originally Posted by gigemboy View Post
    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....

  3. #43
    New Member
    Join Date
    Jun 2011
    Posts
    1

    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

  4. #44
    Addicted Member Cimperiali's Avatar
    Join Date
    Oct 2002
    Location
    Milan, Italy, Europe
    Posts
    188

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

  5. #45
    Banned
    Join Date
    Mar 2009
    Posts
    764

    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

  6. #46
    New Member
    Join Date
    May 2011
    Posts
    4

    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.

  7. #47
    New Member
    Join Date
    Oct 2011
    Posts
    4

    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?

  8. #48
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    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:
    1. Friend Sub test()
    2.         While Loaded
    3.  
    4.             ' If the VBA hasn't been attached to yet, it has closed or the WRAM or IRAM are not set...
    5.             If proc Is Nothing OrElse proc.HasExited Then
    6.                 For Each p As Process In Process.GetProcesses
    7.                     If p.MainWindowTitle.Contains("VisualBoyAdvance") Then
    8.  
    9.                         ' Found VBA. Open it with full access.
    10.                         proc = p
    11.                         hWnd = p.MainWindowHandle
    12.                         hProcess = OpenProcess(&H1F0FFF, False, p.Id)
    13.  
    14.                         Exit For
    15.                     End If
    16.                 Next
    17.                 time = Date.Now ' Start time excluding time looking for the process.
    18.             Else
    19.                 If cbCapture.Checked Then
    20.  
    21.                     ' Make we sill have the window handle then grab the screen.
    22.                     If hWnd = IntPtr.Zero Then
    23.                         hWnd = proc.MainWindowHandle
    24.                     Else
    25.                         bmptemp = New Bitmap(240, 160)
    26.                         bmptemp2 = New Bitmap(240, 160)
    27.  
    28.                         ' Grab the screen twice.
    29.                         bmpSrcBig = sc.CaptureWindowRegion(hWnd, New Rectangle(8, 50, 480, 320))
    30.                         bmpSrcBig2 = sc.CaptureWindowRegion(hWnd, New Rectangle(8, 50, 480, 320))
    31.  
    32.                         ' Draw the screen grabs to another bitmap, shrinking them to normal size.
    33.                         Using g As Graphics = Graphics.FromImage(bmptemp)
    34.                             g.InterpolationMode = Drawing2D.InterpolationMode.Bilinear
    35.                             g.DrawImage(bmpSrcBig, 0, 0, 240, 160)
    36.                         End Using
    37.  
    38.                         Using g As Graphics = Graphics.FromImage(bmptemp2)
    39.                             g.InterpolationMode = Drawing2D.InterpolationMode.Bilinear
    40.                             g.DrawImage(bmpSrcBig2, 0, 0, 240, 160)
    41.                         End Using
    42.  
    43.                         ' Lock the bits.
    44.                         bmpDataA = bmptemp.LockBits(VBAScreenRect, ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb)
    45.                         bmpDataB = bmptemp2.LockBits(VBAScreenRect, ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb)
    46.  
    47.                         ' Check if they are not identical.
    48.                         Check()
    49.  
    50.                         ' Unlock the bits.
    51.                         bmptemp.UnlockBits(bmpDataA)
    52.                         bmptemp2.UnlockBits(bmpDataB)
    53.  
    54.                         ' Increase iteration, display percentage and check if we are finished.
    55.                         SetText(Me, (mismatch / iteration) * 100 & " | " & Date.Now.Subtract(time).TotalMilliseconds)
    56.  
    57.                         If iteration = 5000 Then
    58.                             sb = New StringBuilder
    59.                             sb.Append("Iterations: ")
    60.                             sb.AppendLine(iteration)
    61.                             sb.Append("Mismatches: ")
    62.                             sb.AppendLine(mismatch)
    63.                             sb.Append("Mismatch Percent: ")
    64.                             sb.AppendLine((mismatch / iteration) * 100)
    65.                             sb.Append("Time to complete: ")
    66.                             sb.Append(Date.Now.Subtract(time).TotalMilliseconds)
    67.                             MessageBox.Show(sb.ToString)
    68.                             Exit Sub
    69.                         End If
    70.  
    71.                         iteration += 1
    72.                     End If
    73.                 End If
    74.             End If
    75.         End While
    76.     End Sub
    77.  
    78.     Friend Sub Check()
    79.         For y1 As Int32 = 0 To 159
    80.             For x1 As Int32 = 0 To 239
    81.  
    82.                 If Marshal.ReadInt32(bmpDataA.Scan0, (bmpDataA.Stride * y1) + (4 * x1)) <> _
    83.                    Marshal.ReadInt32(bmpDataB.Scan0, (bmpDataB.Stride * y1) + (4 * x1)) Then
    84.                     mismatch += 1
    85.                     Exit Sub
    86.                 End If
    87.  
    88.             Next
    89.         Next
    90.     End Sub
    Prefix has no suffix, but suffix has a prefix.

  9. #49
    New Member
    Join Date
    Oct 2011
    Posts
    4

    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?

  10. #50
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls

    Quote Originally Posted by mankowitz View Post
    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.

  11. #51
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    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?

  12. #52
    New Member
    Join Date
    May 2012
    Posts
    3

    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?

  13. #53
    New Member
    Join Date
    May 2012
    Posts
    3

    Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls

    I define the rectangle by :

    vb Code:
    1. myRect.Location = Me.Location
    2.         myRect.Height = Me.Height
    3.         myRect.Width = Me.Width

    Me is a transparent form...

  14. #54
    New Member
    Join Date
    May 2012
    Posts
    3

    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?

  15. #55
    New Member
    Join Date
    Jun 2012
    Posts
    2

    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?

  16. #56
    New Member
    Join Date
    Jun 2012
    Posts
    2

    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.

  17. #57
    New Member
    Join Date
    Nov 2012
    Posts
    1

    Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls

    So what I need to get this working? pictureboxes? buttons?

  18. #58
    New Member
    Join Date
    Jan 2013
    Posts
    1

    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?

  19. #59
    Junior Member
    Join Date
    Feb 2026
    Posts
    19

    Re: Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls

    Oh man, this is a nice piece of code here. Kudos!

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width