+ Reply to Thread
Results 1 to 9 of 9

Thread: Creating a Screen Shot tool - VB.NET

  1. #1
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    16
    Posts
    8,777
    Blog Entries
    5

    Creating a Screen Shot tool - VB.NET

    I will in this tutorial show you how to create a program taking screen shots, I will begin with just showing how to take a screen shot of the whole screen but I will then continue a little more advanced. In the tutorial I will show you a bit how to use bitmaps and graphics but if you want to have a better base about them before reading this tutorial you can go here. Anyway we should start now.







    The whole Screen

    First we will create the code that takes the image from the whole screen and saves it in a variable.


    [highlight=VB.NET] Dim Img As New Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
    Dim g As Graphics = Graphics.FromImage(Img)
    g.CopyFromScreen(0, 0, 0, 0, Img.Size)
    g.Dispose()[/highlight]

    In the above code we're creating a new bitmap with the same size as the screen( by using Screen.PrimaryScreen.WorkingArea.Width and Screen.PrimaryScreen.WorkingArea.Height) and then creates a Graphics for the new bitmap called g. Now we're using the actual "screen shooter" by using CopyFromScreen. The 4 zeros means the upperleft corner of the image taken will be at the upperleft corner of your screen which will be stored at the upperleft corner of your image, we will come to this more later when we'll take screen shots of just part of the screen. Then we set the size of the image we want to take which should be the same size as the bitmap which is the same size as the screen. Then at the end we disposes the Graphics since we won't use it anymore. The bitmap variable will now "contain" your screen.






    Part of the Screen

    But it's not sure we actually want to take a screen shot of the whole screen as we just did. If we only want a part of the screen the easiest thins is to just take an image of a part of the screen, we don't have to get the whole screen to then crop it. It could be done like this:

    [highlight=VB.NET] Dim Img As New Bitmap(50, 50)
    Dim g As Graphics = Graphics.FromImage(Img)
    g.CopyFromScreen(300, 50, 0, 0, Img.Size)
    g.Dispose() [/highlight]

    This above code will take a picture of the size 50x50 with its upperleft corner located at (300,50) on the screen. The two zeros that are still there is since the image we take's upperleft corner should be place in the image variable's upperleft corner even though it's smaller.







    General function

    We can now make a function which will work in any cases. We will create two version of the function, one which will take no parameters(if we want to take the whole screen) and the other with all parameters(size and location). Like this:



    [highlight=VB.NET] Private Function TakeImage()
    Return TakeImage(0, 0, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
    End Function
    Private Function TakeImage(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer)
    Dim Img As New Bitmap(Width, Height)
    Dim g As Graphics = Graphics.FromImage(Img)
    g.CopyFromScreen(X, Y, 0, 0, Img.Size)
    g.Dispose()

    Return Img
    End Function[/highlight]

    So in the above code we have one version of the function which takes the parameters X(the X position of the Screen where we want to take the image), Y(the Y position of the Screen where we want to take the image), Width(the width of the image we want to take) and Height(the height of the image we want to take) to take a screen shot from the screen at the wanted position with the wanted size. Then it returns that image. In the other version we're simply returning the value which will be returned from the other version while setting X=0, Y=0, Width=Screen.PrimaryScreen.WorkingArea.Width and Height=Screen.PrimaryScreen.WorkingArea.Height. In this way we can easy take images from the whole screen or from a part of the screen. The reason why one of the versions is using the other is that now if we want to change anything we just do it in the one that actually are taking the picture.






    Removing the Form

    Now when you're taking you're images your screenshot form is included in your images, to solve this we'll making the form invisible, take the screen shot and then make your from visible again. But if we just use Me.Visible = False and Me.Visible = True the form won't have time to become invisible before the image is taken. To solve this we will use Opacity since that will be applied faster.


    [highlight=VB.NET] Private Function TakeImage()
    Return TakeImage(0, 0, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
    End Function
    Private Function TakeImage(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer)

    Dim Opacity As Double = Me.Opacity
    Me.Opacity = 0

    Dim Img As New Bitmap(Width, Height)
    Dim g As Graphics = Graphics.FromImage(Img)
    g.CopyFromScreen(X, Y, 0, 0, Img.Size)
    g.Dispose()

    Me.Opacity = Opacity

    Return Img
    End Function[/highlight]

    Here's what I talked about earlier, now we only need to modify one of those. Anyway, we declares a variable and stores the form's current Opacity. Then we set the Opacity of the form to 0 which means it will be invisible, then just before we're returning the images we will set the opacity back to the original value(the one we stored in the variable).


    And that was pretty much it, now you can use the function we've made to return images from the screen, I won't show how to create a nice UI since that could different a lot depending what you want it to do and what you think is needed. Hope you found this tutorial useful, Bye

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,660
    Blog Entries
    57

    Re: Creating a Screen Shot tool - VB.NET

    Now for a smart one that auto-detects areas +rep
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    16
    Posts
    8,777
    Blog Entries
    5

    Re: Creating a Screen Shot tool - VB.NET

    Auto detect areas of what?

  4. #4
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Creating a Screen Shot tool - VB.NET

    Nicely done! +rep

  5. #5
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,660
    Blog Entries
    57

    Re: Creating a Screen Shot tool - VB.NET

    For example, sub-regions of a window.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #6
    Learning Programmer Bioshox is on a distinguished road
    Join Date
    Oct 2009
    Location
    Manchester, UK
    Posts
    87

    Re: Creating a Screen Shot tool - VB.NET

    Could be usefull in some applications, great work!

  7. #7
    Newbie gerard321 is an unknown quantity at this point
    Join Date
    Dec 2009
    Posts
    2

    Re: Creating a Screen Shot tool - VB.NET

    Hi,

    I used the code and ran into something strange. Within the region I want to copy there is an graph (graphics object) and an overlaying text box with a title. The code provided copies the graphics object but not the title, altough it is within the region specfied.

    Can you explain or better help me with this?

    Thanks in advance,

    Gerard

  8. #8
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    16
    Posts
    8,777
    Blog Entries
    5

    Re: Creating a Screen Shot tool - VB.NET

    How does your objects looks like? And how does your code look like? Is it the same as mine?

  9. #9
    Newbie gerard321 is an unknown quantity at this point
    Join Date
    Dec 2009
    Posts
    2

    Re: Creating a Screen Shot tool - VB.NET

    Hi,

    I do not know what was wrong last time. I tried it again and works like a charm.

    Gerard

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Tutorial - How to create a Splash Screen?
    By diz in forum CSharp Tutorials
    Replies: 8
    Last Post: 01-10-2010, 04:54 PM
  2. Screen Shot
    By VBnet in forum VB Tutorials
    Replies: 7
    Last Post: 05-08-2009, 08:19 AM
  3. C++ and SDL Game
    By oogie in forum C and C++
    Replies: 3
    Last Post: 04-17-2009, 04:24 PM
  4. Replies: 0
    Last Post: 08-25-2007, 10:16 PM
  5. capture screen shot from an application during run time
    By engr in forum Visual Basic Programming
    Replies: 1
    Last Post: 01-29-2007, 03:38 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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