.NET Compact Framework 2.0
Omschrijving
Via onderstaande code is het eenvoudig om een screenshot te maken van het op dat moment zichtbare venster.
Deze functie p/invoke'd de BitBlt functie welke in core.dll verstopt zit. Core.dll hoef je niet als reference toe te voegen aan je project, deze is namelijk onderdeel van Windows Mobile en dus altijd aanwezig.
Systeemeisen
.NET Compact Framework dient geinstalleerd te zijn.
Onderstaande code is geschreven in VB.NET maar dit is natuurlijk eenvoudig om te zetten naar bijvoorbeeld C#.
Convertors
- Code: Selecteer alles
' 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 Sub CaptureScreen(ByVal Filename As String, ByVal ImageFormat As System.Drawing.Imaging.ImageFormat)
' 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(Me.Width, Me.Height)
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
bm.Save(Filename, ImageFormat)
End Sub


