|
VB .NET Helper Newsletter
|
Rod Stephens
|
Aug 09, 2009 06:06 PDT
|
Have a great week and thanks for subscribing!
Rod
RodSte-@vb-helper.com
Books To Keep: http://www.BooksToKeep.com
----------
*** Now Available ***
Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/
Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========
VB.NET Contents:
1. New HowTo: Use the Clipboard to grab an image of the screen in Visual
Basic .NET
2. New HowTo: Get a form's image with or without decorations in Visual
Basic .NET
3. New HowTo: Grab the image of another program's form in Visual Basic
.NET
==========
++++++++++
<VB.NET>
++++++++++
==========
1. New HowTo: Use the Clipboard to grab an image of the screen in Visual
Basic .NET
http://www.vb-helper.com/howto_net_grab_desktop_prntscrn.html
http://www.vb-helper.com/HowTo/howto_net_grab_desktop_prntscrn.zip
The GetDesktopImage function grabs the desktop image by using the
keybd_event API function to generate VK_SNAPSHOT, which simulates
pressing the PrntScrn key.
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal
bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Private Const VK_SNAPSHOT As Short = &H2CS
' Return the desktop image.
Private Function GetDesktopImage() As Image
keybd_event(System.Windows.Forms.Keys.Snapshot, 0, 0, 0)
System.Threading.Thread.Sleep(200)
If Clipboard.ContainsImage() Then Return Clipboard.GetImage()
Return Nothing
End Function
==========
2. New HowTo: Get a form's image with or without decorations in Visual
Basic .NET
http://www.vb-helper.com/howto_net_get_form_image.html
http://www.vb-helper.com/HowTo/howto_net_get_form_image.zip
The GetFormImage function gets the form's image. It starts by using the
form's DrawToBitmap method to make the form draw itself into a bitmap.
Then if the routine should remove the decorations (title bar and
borders), it maps the point (0, 0) in the form's client area to screen
coordinates. It subtracts the form's X and Y position from the mapped
point's coorrdinates to see how big the title bar is and copies the rest
of the form's image into a new bitmap.
Private Function GetFormImage(Optional ByVal with_decorations As Boolean
= True) As Image
Dim bm As New Bitmap(Me.Width, Me.Height)
Me.DrawToBitmap(bm, New Rectangle(0, 0, Me.Width, Me.Height))
If Not with_decorations Then
' Trim off the decorations.
Dim client_origin As Point = Me.PointToScreen(New Point(0, 0))
Dim deco_left As Integer = client_origin.X - Me.Left
Dim deco_top As Integer = client_origin.Y - Me.Top
Dim wid As Integer = Me.ClientRectangle.Width
Dim hgt As Integer = Me.ClientRectangle.Height
Dim new_bm As New Bitmap(wid, hgt)
Dim gr As Graphics = Graphics.FromImage(new_bm)
gr.DrawImage(bm, New Rectangle(0, 0, wid, hgt), New
Rectangle(deco_left, deco_top, wid, hgt), GraphicsUnit.Pixel)
bm = new_bm
End If
Return bm
End Function
==========
3. New HowTo: Grab the image of another program's form in Visual Basic
.NET
http://www.vb-helper.com/howto_net_get_program_image.html
http://www.vb-helper.com/HowTo/howto_net_get_program_image.zip
This program uses the SetForegroundWindow, keybd_event, and
GetWindowRect API functions so it declares them.
Imports System.Runtime.InteropServices
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As
IntPtr) As Boolean
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal
bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Private Const VK_SNAPSHOT As Short = &H2CS
Public Overloads Declare Function GetWindowRect Lib "User32" Alias
"GetWindowRect" (ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Int32
<StructLayout(LayoutKind.Sequential)> _
Public Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
When you click the Go button, the program reads the name of a target
application from the txtProgram text box. It loops through the processes
running on the system to find the target program and passes its main
window handle to function GetProgramImage.
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGo.Click
Dim target As String = txtProgram.Text.ToLower()
For Each proc As Process In Process.GetProcesses()
' See if this is the target program.
If proc.ProcessName.ToLower().Contains(target) Then
' We found it. Take its picture.
Dim bm as Bitmap = GetProgramImage(proc.MainWindowHandle)
picImage.Image = bm
Exit For
End If
Next proc
End Sub
Function GetprogramImage calls SetForegroundWindow to move the target
application to the top. It then calls GetDesktopImage to grab the
desktop's image.
The program then uses GetWindowRect to get the target program's
location, copies that part of the desktop image into a Bitmap, and
returns the result.
' Return a program's image.
Private Function GetProgramImage(ByVal handle As IntPtr) As Image
' Bring it to the top.
SetForegroundWindow(handle)
' Get the desktop image.
Dim desktop_bm As Bitmap = GetDesktopImage()
' Find the program's window.
Dim program_rect As RECT
GetWindowRect(handle, program_rect)
' Copy that part of the desktop image.
Dim wid As Integer = program_rect.Right - program_rect.Left
Dim hgt As Integer = program_rect.Bottom - program_rect.Top
Dim dest_rect As New Rectangle(0, 0, wid, hgt)
Dim src_rect As New Rectangle(program_rect.Left, program_rect.Top,
wid, hgt)
Dim new_bm As New Bitmap(wid, hgt)
Using gr As Graphics = Graphics.FromImage(new_bm)
gr.DrawImage(desktop_bm, dest_rect, src_rect,
GraphicsUnit.Pixel)
End Using
Return new_bm
End Function
Function GetDesktopImage uses the keybd_event API function to simulate
pressing PrntScrn to copy the desktop image to the clipboard.
' Return the desktop image.
Private Function GetDesktopImage() As Image
keybd_event(System.Windows.Forms.Keys.Snapshot, 0, 0, 0)
System.Threading.Thread.Sleep(200)
If Clipboard.ContainsImage() Then Return Clipboard.GetImage()
Return Nothing
End Function
==========
Archives:
http://www.topica.com/lists/VBHelper
http://www.topica.com/lists/VB6Helper
http://www.topica.com/lists/VBNetHelper
Post questions at:
http://www.topica.com/lists/VBHelperQA
|
|
 |
|