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![]()


LinkBack URL
About LinkBacks






Reply With Quote






Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum