Welcome to WindowsClient.net | Sign in | Join

Mathieu Roseboom

My experiences with .NET!

Sponsors





  • advertise here

Tags

No tags have been created or used yet.

News

This blog is not ready yet.

Taking a screenshot in .NETCF

Some time ago I decided to make my very first tool for Windows Mobile. I wanted to be able to make screenshots easily. Of course there where already some tools available to achieve this but since I'm a developer I wanted to create it on my own.

The result is PocketScreen.

PocketScreen1 

It includes a timer and the option to make several screenshots at a time. (with the timer as interval)

Very simple, very straightforward, but it certainly does the job.

The code to actually create the screenshot is also pretty easy with some P/Invoke's

'  imports the GDI BitBlt function that enables the background of the window
'  to be captured
<DllImport("coredll.dll")> _
Private Shared Function BitBlt(ByVal hdcDest 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 Int32) As Boolean
End Function

<DllImport("coredll.dll")> _
Public Shared Function GetDC(ByVal hWnd As IntPtr) As IntPtr
End Function

<DllImport("coredll.dll")> _
Public Shared Function ReleaseDC(ByVal hWnd As IntPtr, _
                                 ByVal hDC As IntPtr) As Integer
End Function

''' <summary>
''' Creates a screenshot of the current screen, including taskbar and softkeys and saves it at the given location.
''' </summary>
''' <param name="Filename">Filename of the screenshot</param>
''' <param name="ImageFormat">Type of image (PNG, JPG, GIF, BMP)</param>
Private Function CaptureScreen(ByVal Filename As String, _
                               ByVal ImageFormat As System.Drawing.Imaging.ImageFormat) As Boolean
    ' Get the handle of the form's device context and create compatible
    ' graphics and bitmap objects
    Dim srcDC As IntPtr = GetDC(IntPtr.Zero) 
    Dim bm As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
    Dim graphics As Graphics = graphics.FromImage(bm)

    ' Get the handle to the graphics object's device context.
    Dim bmDC As IntPtr = graphics.GetHdc

    ' Copy the form to the bitmap
    BitBlt(bmDC, 0, 0, bm.Width, bm.Height, srcDC, 0, 0, &HCC0020)

    ' Release native resources
    ReleaseDC(IntPtr.Zero, srcDC)
    graphics.ReleaseHdc(bmDC)
    graphics.Dispose()

    ' Save the bitmap
    Try

        bm.Save(Filename, ImageFormat) 

        Return True
    Catch ex As Exception 
       Return False
    End Try
End Function

 

The function CaptureScreen is just a wrapper about the BitBlt function which is in the coredll.dll.

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Page view counter