Results 1 to 12 of 12

Thread: Screen image capture class.

  1. #1
    PowerPoster VBDT's Avatar
    Join Date
    Sep 05
    Location
    CA - USA
    Posts
    2,919

    Screen image capture class.

    SCapture Class. Last updated: 08/05/2008


    I created the SCapture class to allow us capture still images from the screen. The SCapture class uses some Api methods to implement the capture. The main reason I used the Api functions such as BitBlt to build the bitmap image because the .Net way of capturing an image does not capture transparent image as transparent; check it here.
    Class shared methods:
    • FullScreen - Captures the full screen (all monitors in a single image).
    • DisplayMonitor- Captures a display monitor.
    • ActiveWindow – Captures the active window.
    • Window– Captures a window specified by the handle or a point (overloaded).
    • Control– Captures a control of a window specified by a handle or a point (overloaded).
    • ScreenRectangle– Captures a rectangle image from the screen.
    • All methods can capture images that include the cursor. New

    Using the code:
    vb Code:
    1. Private Sub ControlButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ControlButton.Click
    2.     Try
    3.         'Capture the image of this button including the cursor.
    4.         Dim img As Image = SCapture.Control(Control.MousePosition, True)
    5.         'Save the captured image.
    6.         img.Save(filePath, Drawing.Imaging.ImageFormat.Png)
    7.         'Also display the captured image in a PictureBox.
    8.         Me.DisplayPictureBox.BackgroundImage = img
    9.     Catch ex As Exception
    10.         'Show a MessageBox if the capture of image failed.
    11.         MessageBox.Show("Failed to capture the control!" _
    12.         & Environment.NewLine & ex.Message, "Capture Error!", _
    13.         MessageBoxButtons.OK, MessageBoxIcon.Error)
    14.     End Try
    15. End Sub
    Author name:
    Arman Ghazanchyan

    Update history:

    09/17/2007
    1. I added a method DisplayMonitor that implements a monitor image capture.
    2. Also the FullScreen method is improved to capture a single image of all monitors that are installed on the system.

    06/29/2008
    1. I added an ability to all methods to capture images that include the cursor.

    07/21/2008
    1. I just changed the class name from ICapture to SCapture.

    08/05/2008
    1. I added an overloadable Window method that takes a point as one of its arguments and returns the bitmap of a window at the point.
    2. Changed all capture methods return types from Image to Bitmap.

    You can check the demo project for the examples of how to capture images with various methods.

    Please feel free to rate this post , comment and notify me about any problem associated with it.
    Attached Images Attached Images  
    Attached Files Attached Files

  2. #2
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 00
    Location
    Ontario, Canada
    Posts
    1,264

    Thumbs up Re: Screen image capture class.

    The code seems to work nicely. However FullScreen only captures the left screen of my two screens (i have dual monitors - a common trend these days). It should capture all screens - or at least have a parameter allowing to select all screens.
    ~Peter

    Still using WinXP Pro SP3, and loving it. Windows 7 is pretty good.

  3. #3
    PowerPoster VBDT's Avatar
    Join Date
    Sep 05
    Location
    CA - USA
    Posts
    2,919

    Re: Screen image capture class.

    Quote Originally Posted by MrGTI
    The code seems to work nicely. However FullScreen only captures the left screen of my two screens (i have dual monitors - a common trend these days). It should capture all screens - or at least have a parameter allowing to select all screens.
    I updated the class so it will capture all monitors in a single image by using the FullScreen method and added a method DisplayMonitor that will capture the image of a particular monitor.

  4. #4
    New Member
    Join Date
    Jul 08
    Location
    Germany
    Posts
    7

    Re: Screen image capture class.

    Hi!
    I Have done some (useful) addons to the SCapture Class.
    And made some Changes.
    See attached Code.
    It is a bit Quick and dirty, have no time......
    Attached Files Attached Files
    Last edited by Pamkkkkk; Jul 29th, 2008 at 09:35 AM.

  5. #5
    PowerPoster VBDT's Avatar
    Join Date
    Sep 05
    Location
    CA - USA
    Posts
    2,919

    Re: Screen image capture class.

    Quote Originally Posted by Pamkkkkk
    Hi!
    I Have done some (useful) addons to the SCapture Class.
    And made some Changes.
    See attached Code.
    It is a bit Quick and dirty, have no time......
    Hi Peter Kriegel, nice job. I specially like the dragging thing. The SCapture class spouse to be the base class so you can add some other methods to it. Although I like what you have done with the class, I have some comments about changes that you made to the SCapture class.

    Firs of all I say that the Window method changes are not correct. It is true that a window is a control but the “Control” method with a point argument will not always capture a window. For example if the point is in a button area which is in its turn on a form than the WindowFromPoint Api method will return the handle of the button not the form that it is on. Thus it will capture the button not the form.

    Second, I would remove all the Optional keywords from the methods' arguments since Visual Studio's Code Analyser suggests to change them to none optional. Now, this is just a warning and it is left to the developer to decide. I prefer the arguments to be none optional so that the developer can specify the argument value by forcing them to not make mistakes.

    Third, use the Return key word instead of assigning the return value of the Window method to the method. I guess this is a VB6 stile. In VB.Net always use the Return key word.

    So far I just noticed these three things but over all it is a great job. If I have a time, than I will take a look on the dragging thing.

  6. #6
    New Member
    Join Date
    Jul 08
    Location
    Germany
    Posts
    7

    Re: Screen image capture class.

    Hi Arman Ghazanchyan !
    First i like to thank you for your kind feed back!

    Quote Originally Posted by VBDT

    Firts of all I say that the Window method changes are not correct. It is true that a window is a control but the “Control” method with a point argument will not always capture a window. For example if the point is in a button area which is in its turn on a form than the WindowFromPoint Api method will return the handle of the button not the form that it is on. Thus it will capture the button not the form.
    Yes You are right i lost it in the code-fight...
    You can get the Mainwindowhandel from a Controlhandel by using API GetAncestor(ControlHandel,GA_ROOTOWNER) where GA_ROOTOWNER = 3

    Quote Originally Posted by VBDT
    Second, I would remove all the Optional keywords from the methods' arguments since Visual Studio's Code Analyser suggests to change them to none optional. Now, this is just a warning and it is left to the developer to decide. I prefer the arguments to be none optional so that the developer can specify the argument value by forcing them to not make mistakes.
    Ok! its really a developer taste.... but i desided me to VB because its less code writing against C#.
    I think it is OK to make Optional Parameters when you have one optional Parameter.
    So there is an overload unnecessary and you have default values that you can save to declare into your class.

    I saved my Time, writing so much overloads get bored and the user of the class has comfort but......its really a developer taste

    Quote Originally Posted by VBDT
    Third, use the Return key word instead of assigning the return value of the Window method to the method. I guess this is a VB6 stile. In VB.Net always use the Return key word.
    .

    I think it is even a developer taste! When you use return, the function is leaved immediately. If you assign the value to the Function (variable) so you can do something after that and leave the Function by Exit Function or the end of the function. For me its a bit more flowcontrol.

    Quote Originally Posted by VBDT
    So far I just noticed these three things but over all it is a great job. If I have a time, than I will take a look on the dragging thing.


    What a bummer!
    I thought the ClipperTool is the favorite... Its is cleaner designed, and do the Layer trick like Hudini
    The Dragtool is very quick and dirty coded and i'm new on so much event handling stuff, it is translated from VB6 Code and is to much API32 burden.

  7. #7
    PowerPoster VBDT's Avatar
    Join Date
    Sep 05
    Location
    CA - USA
    Posts
    2,919

    Re: Screen image capture class.

    Quote Originally Posted by Pamkkkkk
    Hi Arman Ghazanchyan !
    First i like to thank you for your kind feed back!



    Yes You are right i lost it in the code-fight...
    You can get the Mainwindowhandel from a Controlhandel by using API GetAncestor(ControlHandel,GA_ROOTOWNER) where GA_ROOTOWNER = 3



    Ok! its really a developer taste.... but i desided me to VB because its less code writing against C#.
    I think it is OK to make Optional Parameters when you have one optional Parameter.
    So there is an overload unnecessary and you have default values that you can save to declare into your class.

    I saved my Time, writing so much overloads get bored and the user of the class has comfort but......its really a developer taste




    I think it is even a developer taste! When you use return, the function is leaved immediately. If you assign the value to the Function (variable) so you can do something after that and leave the Function by Exit Function or the end of the function. For me its a bit more flowcontrol.




    What a bummer!
    I thought the ClipperTool is the favorite... Its is cleaner designed, and do the Layer trick like Hudini
    The Dragtool is very quick and dirty coded and i'm new on so much event handling stuff, it is translated from VB6 Code and is to much API32 burden.
    Hmm, I don’t know how did I hurt your feelings but if I did than my apologies.

    And yes, you are right; we can use the GetAncestor Api method to get the parent window in the chain. Thanks for that!

  8. #8
    New Member
    Join Date
    Jul 08
    Location
    Germany
    Posts
    7

    Re: Screen image capture class.

    Quote Originally Posted by VBDT
    Hmm, I don’t know how did I hurt your feelings but if I did than my apologies.
    No ! You didn't hurt me !
    One of my faforite is to discuss over Programming technics.(therfor i am Fire and Flame)

    Please think on it, english is not my mother toungue, and it comes to misunderstandings some times, thats why i am using the Smilies so much to prevent for. (I dont write these replys, i develop them ...)
    Last edited by Pamkkkkk; Aug 2nd, 2008 at 11:20 AM.

  9. #9
    New Member
    Join Date
    Jul 08
    Location
    Germany
    Posts
    7

    Re: Screen image capture class.

    Quote Originally Posted by VBDT
    The SCapture class uses some Api methods to implement the capture. The main reason I used the Api functions such as BitBlt to build the bitmap image because the .Net way of capturing an image does not capture transparent image as transparent; check it here.
    In some cases you can use a supposed bug as a feature.
    (its not a bug its a feature )

    My idea is to let the user diside if he want to snap an image with or without transparency.
    The reason for that you can find here:
    http://weblogs.asp.net/kennykerr/arc...sta-tools.aspx

    So the Scapture class can use the API capturing for transparency and the .NET function for untransparency snaps.

  10. #10
    New Member
    Join Date
    Mar 09
    Posts
    1

    Talking Re: Screen image capture class.

    I had to register just so I could reply.

    I have to say, thank you for this sample, it's exactly what I was looking for, all other examples out there only gave you the first monitor instead of full screen like yours.

    Good Job!

  11. #11
    PowerPoster VBDT's Avatar
    Join Date
    Sep 05
    Location
    CA - USA
    Posts
    2,919

    Re: Screen image capture class.

    Quote Originally Posted by Sentax
    I had to register just so I could reply.

    I have to say, thank you for this sample, it's exactly what I was looking for, all other examples out there only gave you the first monitor instead of full screen like yours.

    Good Job!
    Thanks, I am glad you liked it

  12. #12
    New Member
    Join Date
    Apr 10
    Posts
    1

    Re: Screen image capture class.

    Thanks Man This works really well

Posting Permissions

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