|
|
Title | Grab part of the desktop image from a tray icon in VB .NET |
Description | This example shows how to grab part of the desktop image from a tray icon in VB .NET. |
Keywords | desktop, grab pixels, image, background, VB.NET, NotifyIcon, ContextMenu |
Categories | Graphics, Windows, VB.NET, Controls |
|
|
This program starts hidden but displays a NotifyIcon in the System Tray. When the user right-clicks the tray icon and selects the Capture JPEG command, the program grabs the complete desktop image, copies it into the picDesktop PictureBox, and displays its form. The form is maximized and picDesktop is docked to fill the form so the desktop image fills the form.
|
|
Private Sub mnuCaptureJpeg_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
mnuCaptureJpeg.Click
' Get the desktop image.
m_bmDesktop = DesktopImage()
' Display the image.
picDesktop.Image = DirectCast(m_bmDesktop.Clone(), _
Bitmap)
Me.Show()
End Sub
' Return an image of the desktop.
Private Function DesktopImage() As Bitmap
' Get the desktop size in pixels.
Dim desktop_win As Int32 = GetDesktopWindow()
Dim desktop_dc As Int32 = GetDC(desktop_win)
Dim desktop_bounds As Rectangle = Screen.GetBounds(New _
Point(1, 1))
Dim desktop_wid As Int32 = desktop_bounds.Width
Dim desktop_hgt As Int32 = desktop_bounds.Height
' Make a Bitmap to hold the image.
Dim bm As New Bitmap(desktop_wid, desktop_hgt)
Dim bm_gr As Graphics = Graphics.FromImage(bm)
Dim bm_hdc As IntPtr = bm_gr.GetHdc
' Copy the desktop's image.
StretchBlt( _
bm_hdc, 0, 0, desktop_wid, desktop_hgt, _
desktop_dc, 0, 0, desktop_wid, desktop_hgt, _
SRCCOPY)
' Release the bitmap's and desktop's DCs.
bm_gr.ReleaseHdc(bm_hdc)
ReleaseDC(desktop_win, desktop_dc)
' Return the result.
Return bm
End Function
|
|
When the program starts, it grabs the complete desktop image, copies it into the picDesktop PictureBox, and displays its form. The form is maximized and picDesktop is docked to fill the form so the desktop image fills the form.
When the user presses the mouse down on picDesktop, the MouseDown event handler starts drawing a rubberband box. It saves the mouse's current position and makes a Graphics object to draw on the PictureBox.
When the user moves the mouse, the MouseMove event handler updates the location of the rubberband box's second corner, redraws the desktop image, and then draws the new rubberband box.
When the user releases the mouse, the MouseUp event handler hides the program's form. It uses the GetFolderPath function to get the user's desktop folder and then loops through a series of numbers until it finds a filename of the form "Screen Grab123.jpg" that is not in use.
Next it copies the selected part of the desktop image into a new Bitmap and uses the Bitmap's Save method to save the it in the file as a JPEG image.
Finally the program ends.
|
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Me.Hide()
Application.DoEvents()
' Get the desktop image.
m_bmDesktop = DesktopImage()
' Display the image.
picDesktop.Image = DirectCast(m_bmDesktop.Clone(), _
Bitmap)
Me.Show()
End Sub
|
|
The DesktopImage function uses the GetDesktopWindow API function to get the hWnd of the desktop window. It uses GetDC to get a device context for that window. It uses Screen.GetBounds to see how big the desktop is.
Next the function makes a Bitmap big enough to hold the desktop's image, creates a Graphics object attached to it, and gets the Bitmap's device context.
The function then uses StretchBlt to copy the desktop window's image onto the Bitmap. It releases the Bitmap's and desktop's device context, and returns the Bitmap.
|
|
' Return an image of the desktop.
Private Function DesktopImage() As Bitmap
' Get the desktop size in pixels.
Dim desktop_win As Int32 = GetDesktopWindow()
Dim desktop_dc As Int32 = GetDC(desktop_win)
Dim desktop_bounds As Rectangle = Screen.GetBounds(New _
Point(1, 1))
Dim desktop_wid As Int32 = desktop_bounds.Width
Dim desktop_hgt As Int32 = desktop_bounds.Height
' Make a Bitmap to hold the image.
Dim bm As New Bitmap(desktop_wid, desktop_hgt)
Dim bm_gr As Graphics = Graphics.FromImage(bm)
Dim bm_hdc As IntPtr = bm_gr.GetHdc
' Copy the desktop's image.
StretchBlt( _
bm_hdc, 0, 0, desktop_wid, desktop_hgt, _
desktop_dc, 0, 0, desktop_wid, desktop_hgt, _
SRCCOPY)
' Release the bitmap's and desktop's DCs.
bm_gr.ReleaseHdc(bm_hdc)
ReleaseDC(desktop_win, desktop_dc)
' Return the result.
Return bm
End Function
|
|
When the user presses the mouse down on picDesktop, the MouseDown event handler starts drawing a rubberband box. It saves the mouse's current position and makes a Graphics object to draw on the PictureBox.
When the user moves the mouse, the MouseMove event handler updates the location of the rubberband box's second corner, redraws the desktop image, and then draws the new rubberband box.
When the user releases the mouse, the MouseUp event handler hides the program's form. It uses the GetFolderPath function to get the user's desktop folder and then loops through a series of numbers until it finds a filename of the form "Screen Grab123.jpg" that is not in use.
Next it copies the selected part of the desktop image into a new Bitmap and uses the Bitmap's Save method to save the it in the file as a JPEG image.
|
|
' Start dragging to select an area.
Private Sub picDesktop_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
picDesktop.MouseDown
m_Drawing = True
' Save the point's coordinates.
m_X1 = e.X
m_X2 = e.X
m_Y1 = e.Y
m_Y2 = e.Y
' Make a Graphics object to draw on.
m_grDesktop = picDesktop.CreateGraphics()
End Sub
' Continue drawing.
Private Sub picDesktop_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
picDesktop.MouseMove
If Not m_Drawing Then Exit Sub
' Save the new point.
m_X2 = e.X
m_Y2 = e.Y
' Redraw the desktop image.
m_grDesktop.DrawImage(m_bmDesktop, 0, 0)
' Draw the rubberband rectangle.
Dim rect As New Rectangle( _
Min(m_X1, m_X2), Min(m_Y1, m_Y2), _
Abs(m_X1 - m_X2), Abs(m_Y1 - m_Y2))
m_grDesktop.DrawRectangle(Pens.Blue, rect)
End Sub
' Stop drawing.
Private Sub picDesktop_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
picDesktop.MouseUp
If Not m_Drawing Then Exit Sub
m_Drawing = False
' Hide the form.
Me.Hide()
' Get the name of a file on the desktop.
Dim file_path As String = _
GetFolderPath(SpecialFolder.Desktop)
If Not file_path.EndsWith("\") Then file_path &= "\"
file_path &= "Screen Grab"
Dim file_name As String
For i As Integer = 0 To 1000000
file_name = file_path & i.ToString & ".jpg"
Dim file_info As New FileInfo(file_name)
If Not file_info.Exists() Then Exit For
Next i
' Grab the selected piece of the image.
Dim rect As New Rectangle( _
Min(m_X1, m_X2), Min(m_Y1, m_Y2), _
Abs(m_X1 - m_X2), Abs(m_Y1 - m_Y2))
Dim bm As Bitmap = DirectCast( _
m_bmDesktop.Clone(rect, m_bmDesktop.PixelFormat), _
Bitmap)
' Save the image into the file.
bm.Save(file_name, ImageFormat.Jpeg)
' Unload.
Me.Close()
End Sub
|
|
If the user right-clicks the tray icon and selects Exit, the form simply closes.
|
|
Private Sub mnuExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuExit.Click
Me.Close()
End Sub
|
|
|
|
|
|