Results 1 to 40 of 58

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

Threaded View

  1. #1

    Thread Starter
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

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

    Here is a class that contains very easy methods to get screen captures. It is not code originally written by me, it was actually found Here. It was originally written in C#, and there was supposedly a VB.NET example in there, however it just looked as if it was ran through some code converter that didn't work. I cleaned it up in order for it to work in .NET, replaced all the API declarations with the correct working ones, and even added a CaptureDeskTopRectangle function to it for desktop regions.

    The file has its own namespace, "ScreenShot", which includes three seperate classes:

    ScreenCapture - Main class, contains functions for obtaining the actual images
    GDI32 = Helper Class with various GDI Functions that are used
    USER32 - Helper Class with various User32 Functions used

    The ScreenCapture has five exposed functions and methods that enables you to get captures:

    CaptureWindow - grabs image of object by its handle
    CaptureScreen - grabs image of entire screen
    CaptureWindowToFile - captures image of object by its handle straight to file
    CaptureScreenToFile - captures image of screen straight to file
    CaptureDesktopRectangle - captures desktop region by a passed in rectangle

    ***.NET 2005 has a .CopyFromScreen method in the Graphics class that allows you to get a desktop region, however, there is no way in 2003, which is why CaptureDesktopRectangle was needed...


    Sample code showing how to use each method is below, the class is available for download at the end of the post.
    VB Code:
    1. Dim SC As New ScreenShot.ScreenCapture
    2.  
    3.         'grabs image of object handle (in this case, the form)
    4.         Dim MyWindow As Image = SC.CaptureWindow(Me.Handle)
    5.        
    6.         'grabs image of entire desktop
    7.         Dim MyDesktop As Image = SC.CaptureScreen
    8.        
    9.         'captures entire desktop straight to file
    10.         SC.CaptureScreenToFile("c:\desktop2.jpg", Imaging.ImageFormat.Jpeg)
    11.  
    12.         'captures image of object handle (in this case, the form) straight to file
    13.         SC.CaptureWindowToFile(Me.Handle, "c:\window2.jpg", Imaging.ImageFormat.Jpeg)
    14.  
    15.         'returns bitmap of region of desktop by passing in a rectangle
    16.         Dim MyBitMap As Bitmap = SC.CaptureDeskTopRectangle(New Rectangle(Me.Location.X, Me.Location.Y, _
    17.                                      Me.Width, Me.Height), Me.Width, Me.Height)
    18.         MyBitMap.Save("c:\desktopregion.jpg", Imaging.ImageFormat.Jpeg)
    19.         'above example gets rectangle of form that it is called in, you can get
    20.         'desktop by calling me.hide before the actual function call in order to
    21.         'get the desktop that is in the bounds of the form (handy when using a
    22.         'transparent Form or control in order to make a "viewfinder" for the capture...)
    23.         'You can pass in any rectangle you want, this was so you can see how
    24.         'it works...
    Happy coding!
    Attached Files Attached Files
    Last edited by gigemboy; May 12th, 2006 at 01:35 PM.

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