Imports System.IO
Public Class CaptureScreen
'
Captura de pantalla y retorna base64
Public Shared Function ToBase64(widthScreen As Integer, heighScreen As Integer, leftScreen As Integer, topScreen As Integer) As String
Dim imageString As String = ""
Try
' Capture screen image to bitmap
Dim b As Bitmap
b = New Bitmap(widthScreen, heighScreen,
Imaging.PixelFormat.Format16bppRgb555)
Dim g As Graphics = Graphics.FromImage(b)
g.CopyFromScreen(New Point(leftScreen,
topScreen), New Point(0, 0),
New Size(widthScreen + 5,
heighScreen + 5))
' Convert bitmap to byte array
Dim ms = New MemoryStream()
b.Save(ms,
System.Drawing.Imaging.ImageFormat.Jpeg)
Dim bitmapBytes = ms.ToArray()
' Convert byte array to base64 string
imageString =
Convert.ToBase64String(bitmapBytes)
Catch ex As Exception
End Try
'
Return base64
Return imageString
End Function
'
Captura pantalla y retorna ruta del fichero imagen
Public Shared Function ToFile(widthScreen As Integer, heighScreen As Integer, leftScreen As Integer, topScreen As Integer, defaultFileName As String) As String
Dim TempDir As String =
System.AppDomain.CurrentDomain.BaseDirectory & "TEMP"
If Not
System.IO.Directory.Exists(TempDir) Then
System.IO.Directory.CreateDirectory(TempDir)
End If
Dim fileName As String = TempDir & "\" & defaultFileName
& "_screen.jpg"
Try
' Capture screen image to bitmap
Dim b As Bitmap
b = New Bitmap(widthScreen, heighScreen,
Imaging.PixelFormat.Format16bppRgb555)
Dim g As Graphics = Graphics.FromImage(b)
g.CopyFromScreen(New Point(leftScreen,
topScreen), New Point(0, 0),
New Size(widthScreen + 5,
heighScreen + 5))
' Check File Name
CheckFileName(fileName, "jpg")
' Save bitmap to file
b.Save(fileName)
Catch ex As Exception
End Try
'
Return file name
Return fileName
End Function
Private Shared Sub CheckFileName(ByRef fileName As String, fileExtension As String)
'
Check File Name
If
System.IO.File.Exists(fileName) Then
Try
System.IO.File.Delete(fileName)
Catch ex As Exception
End Try
If System.IO.File.Exists(fileName) Then
Dim i As Integer = 1
fileName = fileName.Replace("." & fileExtension, "") & "(" & i & ")." & fileExtension
While System.IO.File.Exists(fileName)
i = i + 1
fileName =
fileName.Replace("." & fileExtension, "") & "(" & i & ")." & fileExtension
End While
End If
End If
End Sub
' Captura imagen del formulario activo y
retorna base64
Private Declare Auto Function BitBlt Lib "gdi32.dll" (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 _
System.Int32) As Boolean
Private Const SRCCOPY As Integer = &HCC0020
Private m_PrintBitmap As Bitmap
Public Shared Function GetFormImage(me_gr As Graphics, WidthScreen
As Integer, HeighScreen As Integer) As String
'
Get this form's Graphics object.
'
Make a Bitmap to hold the image.
Dim bm As New Bitmap(WidthScreen,
HeighScreen, me_gr)
Dim bm_gr As Graphics =
me_gr.FromImage(bm)
Dim bm_hdc As IntPtr = bm_gr.GetHdc
'
Get the form's hDC. We must do this after
'
creating the new Bitmap, which uses me_gr.
Dim me_hdc As IntPtr = me_gr.GetHdc
'
BitBlt the form's image onto the Bitmap.
BitBlt(bm_hdc, 0, 0, WidthScreen,
HeighScreen,
me_hdc, 0, 0, SRCCOPY)
me_gr.ReleaseHdc(me_hdc)
bm_gr.ReleaseHdc(bm_hdc)
'Bitmap
to base64
Dim ms = New MemoryStream()
bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg) ' Use appropriate
format here
Dim bitmapBytes =
ms.ToArray()
'
Convert byte array to base64 string
Return
Convert.ToBase64String(bitmapBytes)
End Function
' Llamada desde el formulario activo --> Dim imageBase64 As String =
CaptureScreen.GetFormImage(Me.CreateGraphics, Me.Width , Me.Height )
End Class