PDA

Click to See Complete Forum and Search --> : Screen Capture Example - VB.NET


xzanti
Feb 19th, 2010, 04:47 PM
I am looking for some sample code to capture the current form display and save as an image file.

I searched the forum and there is a c# example but nothing in vb.net.

Any help will be greatly appreciated!

xzanti
Feb 19th, 2010, 05:07 PM
ok,

I tried converting the c# code to vb.net (below) but when I attempt to run the code below, I get the following error on loading the form.

"DocumentCaptureFirmwareSupportException Document capture requires firmware version 1.0 or higher. Current firmware version is 0. Please upgrade the firmware"


Imports System.Runtime.InteropServices


Public Class CaptureScreen
' Declarations
<DllImport("coredll.dll")> _
Friend 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 UInteger) As Integer
End Function

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

Const SRCCOPY As Integer = &HCC0020

Public Shared Sub Snapshot(ByVal fileName As String, ByVal rectangle As Rectangle)
'Use a zeropointer to get hold of the screen context
Dim deviceContext As IntPtr = GetDC(IntPtr.Zero)

Using capture As New Bitmap(rectangle.Width, rectangle.Height)
'Get screen context
Using deviceGraphics As Graphics = Graphics.FromHdc(deviceContext)
'Get graphics from bitmap
Using captureGraphics As Graphics = Graphics.FromImage(capture)
' Blit the image data
BitBlt(captureGraphics.GetHdc(), 0, 0, rectangle.Width, rectangle.Height, deviceGraphics.GetHdc(), _
rectangle.Left, rectangle.Top, SRCCOPY)

'Save to disk
capture.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp)
End Using
End Using
End Using
End Sub

Public Shared Function Snapshot(ByVal rectangle As Rectangle) As Bitmap
'Use a zeropointer to get hold of the screen context
Dim deviceContext As IntPtr = GetDC(IntPtr.Zero)

Dim capture As New Bitmap(rectangle.Width, rectangle.Height)

'Get screen context
Using deviceGraphics As Graphics = Graphics.FromHdc(deviceContext)
'Get graphics from bitmap
Using captureGraphics As Graphics = Graphics.FromImage(capture)
' Blit the image data
BitBlt(captureGraphics.GetHdc(), 0, 0, rectangle.Width, rectangle.Height, deviceGraphics.GetHdc(), _
rectangle.Left, rectangle.Top, SRCCOPY)
End Using
End Using

Return capture
End Function
End Class

xzanti
Feb 19th, 2010, 06:29 PM
:o

ok it works, I was not calling the function correctly.

This button click created a screen image
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Rect As Rectangle
Rect.Width = Screen.PrimaryScreen.Bounds.Width
Rect.Height = Screen.PrimaryScreen.Bounds.Height
Rect.X = 0
Rect.Y = 0
CaptureScreen.Snapshot("My Documents\Test.bmp", Rect)
End Sub