Getting a Screenshot with VB.NET on the Compact Framework 2.0
Posted by anoriginalidea on January 3, 2008
I believe that “thumbnail” images of data in applications is a powerful way of giving an overview to a user.
One sort of application that uses this technique is the “Flip 3D” functionality of Vista.
The code sample below provides a routine that “grabs” a rectangle of the currently displayed screen. Just like it’s desktop equivalent, it can only take screenshots of the visible screen, not graphics that are off-screen. (Anyone know how to do that? Please let me know!)
Here’s the code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Private Function GetControlBitmap(ByVal gx As Graphics, ByVal rect As Rectangle) As Bitmap
' Create the bitmap to output ' Create compatible graphics ' Blit the image data ' Cleanup
The function “GetControlBitmap” will get a bitmap of a control or any rectangle passed into it.
picScreenshot.Image = GetControlBitmap(Me.CreateGraphics, Me.SomeControl.Bounds)
End Sub
Const SRCCOPY As Integer = &HCC0020
Public Declare Function BitBlt Lib "coredll.dll" (ByVal hdc As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As Integer) As Boolean
Dim loBitmap As Bitmap = New Bitmap(rect.Width, rect.Height)
Dim loCompGraphics As Graphics = Graphics.FromImage(loBitmap)
BitBlt(loCompGraphics.GetHdc(), 0, 0, rect.Width, rect.Height, gx.GetHdc(), rect.Left, rect.Top, SRCCOPY)
loCompGraphics.Dispose()
Return loBitmap
End Function
Links
- Creating a screen snapshot in CF v2 by Alex Yahnin
- Alex Yakhnin – Excellent CF coding blog. The information here is a goldmine of information that I wish I had a year ago.


Tank3 said
Thanks for the Code! I think there is a followup to this snippet at
mick said
there is a 3d alttab program that seems to save also not visible parts of screen.
maybe asking that developer and posting results here would solve this problem