In VB6 I could do a screen grab using a key event for the Print screen button then get the image from the clipboard and write that image to a file.
keybd_event vbKeySnapshot, 0, 0, 0
' send a print screen button up event
keybd_event vbKeySnapshot, 0, &H2, 0
' paste the clipboard contents into the picture box
ScreenCapture.Picture = Clipboard.GetData(vbCFBitmap)
SavePicture ScreenCapture.Picture, "C:\Caliprogs\Results\B89Drift.bmp"
How can I do this in VB2005.

Screen capture
agentpastone
You can capture an active form while the program is running. Press Alt-PrintScreen to save active form to the clipboard. Then start an application such as WORD and paste the image into the doc.
Jon Erlend Mathisen
Dear rkimble
Thank you for you quick response
I have another Question I can use mouse down event to get use selected area in my form but
How can I let user to select a area in desktop or other form and How can i get the selected area position or size
DaveS_
There are probably a number of ways to go about this. Since you appear to need a user controllable screen capture device, here's an example program that does just that. It creates a full screen, semi-transparent form, that allows the user to select an area of the screen and capture it to disk.
To try it, just create a new project with a blank form and drop this code over the existing Form1 class declaration:
Public
Class Form1 'Create a context menu for controlling the application Private mCtxMnu As New ContextMenu 'Create a panel to serve as the selection area Private mSelPanel As New Panel 'Create a string to hold the screen shot storage location Private mDirPath As String 'Create an integer to store the opacity of the application Private mOpacity As Integer = 0.35 'Program initialization Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Configure the form as a full screen, semi-transparent, 'top most, borderless form Me.BackColor = Color.White Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None Me.ShowInTaskbar = False Me.TopMost = True Me.Size = Screen.PrimaryScreen.Bounds.Size Me.Location = New Point(0, 0) Me.Opacity = Me.mOpacity Me.ContextMenu = Me.mCtxMnu 'Configure the context menu with a capture, select all, and close menu item Me.ContextMenu.MenuItems.Add("Capture", AddressOf CaptureEventHandler) Me.ContextMenu.MenuItems.Add("Select All", AddressOf SelectAllEventHandler) Me.ContextMenu.MenuItems.Add("-") Me.ContextMenu.MenuItems.Add("Close", AddressOf CloseEventHandler) 'Set the selection area color Me.mSelPanel.BackColor = Color.DeepSkyBlue 'Ensure a path exists to save the screen shots to Me.mDirPath = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "Screen Shot Tests") If Not System.IO.Directory.Exists(Me.mDirPath) ThenSystem.IO.Directory.CreateDirectory(
Me.mDirPath) End If End Sub Private Sub CaptureEventHandler(ByVal sender As Object, ByVal e As EventArgs) 'Ensure the selection panel is being displayed If Me.Controls.Contains(Me.mSelPanel) Then 'Create a bitmap and graphics object for capturing the selection area Dim bmp As New Bitmap(Me.mSelPanel.Size.Width, Me.mSelPanel.Size.Height) Dim gfx As Graphics = System.Drawing.Graphics.FromImage(bmp) 'Make sure this application will not be visible Me.Opacity = 0 'Copy the selection area to the screen, then clean up the graphics objectgfx.CopyFromScreen(
Me.mSelPanel.Location, New Point(0, 0), Me.mSelPanel.Size, CopyPixelOperation.MergeCopy)gfx.Dispose()
'Reset the transparency of this application Me.Opacity = Me.mOpacity 'Save the image Dim fn As Stringfn = System.IO.Path.Combine(
Me.mDirPath, String.Concat(Now.ToString("ddMMyyyyhhmmss"), ".jpg"))bmp.Save(fn)
bmp.Dispose()
'Remove the selection panel Me.Controls.Remove(Me.mSelPanel) End If End Sub Private Sub CloseEventHandler(ByVal sender As Object, ByVal e As EventArgs) 'Close the application Me.Close() End Sub Private Sub SelectAllEventHandler(ByVal sender As Object, ByVal e As EventArgs) 'Set the selection panel to the full screen size Me.mSelPanel.Size = Me.Size Me.mSelPanel.Location = New Point(0, 0) If Not Me.Controls.Contains(Me.mSelPanel) Then Me.Controls.Add(Me.mSelPanel) End If End Sub Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown 'Show the selection panel if its not already shown If Me.Controls.Contains(Me.mSelPanel) Then Me.Controls.Remove(Me.mSelPanel) End If 'Reset the seleciton panel size and position If e.Button = Windows.Forms.MouseButtons.Left Then Me.mSelPanel.Size = New Size(1, 1) Me.mSelPanel.Location = e.Location Me.Controls.Add(Me.mSelPanel) End If End Sub Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove 'If the selection is being made in a positive direction, resize 'the panel to fit the selection If e.Button = Windows.Forms.MouseButtons.Left Then If e.X > Me.mSelPanel.Location.X Then If e.Y > Me.mSelPanel.Location.Y Then Me.mSelPanel.Width = e.X - Me.mSelPanel.Location.X Me.mSelPanel.Height = e.Y - Me.mSelPanel.Location.Y End If End If End If End SubEnd
ClassThis should give you the basic peices necessary. Good luck!
Jamesford42
Ken,
Modify the line: graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size,CopyPixelOperation.SourceCopy)
This is where the screen source is being specified. Bounds.X and Bounds.Y should be replaced with the upper left coordinates of the area of the screen you want to capture and Bounds.Size should be replaced with the size of the area.
That should be all there is to it!
-EDIT-
Oh, right, not quite all - when you created the target bitmap, its size should also have been set to the size of the area you actually want to capture.
Microart
Max,
Thanks for the code it works a treat.
Dave
garnett
Hi,
Here is the sample how to do that:
Dim bounds As RectangleDim screenshot As Bitmap
Dim graph As Graphics
bounds = Screen.PrimaryScreen.Bounds
screenshot = New Bitmap(bounds.Width, bounds.Height, Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size,CopyPixelOperation.SourceCopy)
screenshot.Save("c:\temp\image.bmp")
Max
Noah Campbell
Hi Maksim Libenson
I have trted your code and it can capture whole screen no problem.
But if i only want to capture some area in my desktop So , How can't I achieve this ideal
can you teach me or let me know what kind of document I should reference.
Thank you.
Jorgen Aker
SendKeys.Send("{PRTSC}") 'PrintScreen Key
PictureBox1.Image = My.Computer.Clipboard.GetImage 'Or System.Windows.Forms.Clipboard.GetImage()
PictureBox1.Image.Save(FilePath)