vb.net screen capture

Do you want to make a vb.net app that can capture screenshots ? Well you can do that easily with vb.net

If you have any suggestions or comments on this tutorial please visit idealprogrammer forums !

This is the code you will need to capture screenshots using vb.net. It uses gdi classes and some more classes.

Thanks to mike gold of vbdotnetheaven

Private Sub PerformCapture()
‘ turn the form invisible so you don’t show it during capture
Me.Visible = False
‘use the GDI call and create a DC to the whole display
Dim dc1 As IntPtr = CreateDC(“DISPLAY”, Nothing, Nothing, CType(Nothing, IntPtr))
‘create a Graphics object for the screen dc
Dim g1 As Graphics = Graphics.FromHdc(dc1)
‘ create a compatible bitmap the size of the entire screen
MyImage = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1)
‘ use the bitmap to create another Graphics surface so we can BitBlast into the bitmap
Dim g2 As Graphics = Graphics.FromImage(MyImage)
‘ Now go retrace our steps and get the device contexts for both the bitmap and the screen
‘ Note: Apparently you have to do this, and can’t go directly from the aquired dc or exceptions are thrown
‘ When you try to release the dcs
dc1 = g1.GetHdc()
Dim dc2 As IntPtr = g2.GetHdc()
‘ Bit Blast the screen into the Bitmap
BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc1, 0, 0, 13369376)
‘ Remember to release the dc’s, otherwise problems down the road
g1.ReleaseHdc(dc1)
g2.ReleaseHdc(dc2)
‘ Save the image to JPEG file
MyImage.Save(“c:\Captured.jpg”, ImageFormat.Jpeg)
‘ Now we can view our form again, so make it visible
Visible = True
MessageBox.Show(“Finished Saving Image”)
‘ Set the Bounds of the form so that it fills the entire screen
‘ Because in our Paint event handler we are going to draw the bitmap to the form
Me.SetBounds(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
‘ ************** Set up functions for doing additional bitmap manipulation such as cropping a portion of
‘ ************* the image ****************(Not required for screen capture)SetupCropping()
firstCrop = True
End Sub

This application has a few additional functions. One is that you can crop a piece of the image in your screen capture as shown in figure 2:

Figure 2 – Cropping a Piece of the image

This is useful for pulling things out of the screen capture that you find necessary in your final image. Outlining the cropping region is performed using the MouseDown, MouseUp, and MouseMove event handlers. You need to maintain the state in which the cropping is in in order to successfully draw the cropping region. Below is the state diagram for tracking the drawing of the crop rectangle.

Figure 3 – UML State Chart of Cropping Drawn with WithClass 2000

Below is the code for the MouseMove event handler. As shown in the state diagram, it checks the state and condition to see if we should be doing the calculation on the cropping rectangle and if we should invalidate the rectangle.

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
‘ Check the state and condition to makes sure we can draw the cropping rectangle
If (CroppingPaint AndAlso CropOn) AndAlso StartedCrop Then
‘ calculate width of cropping rectangle
DeltaX = e.X – StartPoint.X
If DeltaX < 0 Then DeltaX = 0 End If DeltaY = e.Y - StartPoint.Y If DeltaY < 0 Then DeltaY = 0 End If ' force the cropping rectangle to redraw Invalidate(New Rectangle(StartPoint.X, StartPoint.Y, DeltaX + 20, DeltaY + 20)) End If End Sub Improvements The cropping invalidation is a little messy, it needs to add rubber-banding functionality to the line drawing of the rectangle. Also I had to offset the image to get the cropping to work out the first time through the capture. Not sure if its something I'm missing or a problem, but its in there like a band-aid. Some nice improvements might include an undo, to undo cropping, the ability to put the image into a specified file rather than in c:\\capture.jpg. (Currently this is where the image is placed when its captured or saved). Additional features would be to save the image in different image formats, include a preference dialog for suffixes and prefixes to the captured image name, add a feature to enter text into the captured image. As with other projects in .NET the sky is the limit, but its a snap to add many of these additional functions using this environment. Stay