使用.NET2.0新增的类截取屏幕

来源:互联网 发布:ubuntu调分辨率 编辑:程序博客网 时间:2024/04/30 09:15
 Public Shared Function cap() As Bitmap
        
Dim desktopbmp As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
        
Dim g As Graphics = Graphics.FromImage(desktopbmp)
        g.CopyFromScreen(
0000, _
        
New Size(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, _
        System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height))
        
Dim rect As Rectangle = New Rectangle(Cursor.Position.X - Cursor.Current.HotSpot.X, _
        Cursor.Position.Y 
- Cursor.Current.HotSpot.Y, _
        Cursor.Current.Size.Width, Cursor.Current.Size.Height)
        Cursor.Current.Draw(g, rect)
        
Return desktopbmp
    
End Function
CodeProject上的一个使用API截屏的例子
Win32Stuff.vb
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices


Namespace ScreenshotCaptureWithMouse.ScreenCapture
    
Class Win32Stuff

Class Variables


Class Functions
    
End Class

End Namespace

GDIStuff.vb
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices

Namespace ScreenshotCaptureWithMouse.ScreenCapture
    
Class GDIStuff
Class Variables


Class Functions
    
End Class

End Namespace

CaptureScreen.vb
Imports System.Drawing
Imports System.Runtime.InteropServices


Namespace ScreenshotCaptureWithMouse.ScreenCapture
    
Class CaptureScreen
        
'This structure shall be used to keep the size of the screen.
        Public Structure SIZE
            
Public cx As Integer
            
Public cy As Integer
        
End Structure


        
Private Shared Function CaptureDesktop() As Bitmap
            
Dim size As SIZE
            
Dim hBitmap As IntPtr
Dim hDC As IntPtr = Win32Stuff.GetDC(Win32Stuff.GetDesktopWindow())
            
Dim hMemDC As IntPtr = GDIStuff.CreateCompatibleDC(hDC)

            size.cx 
= Win32Stuff.GetSystemMetrics(Win32Stuff.SM_CXSCREEN)

            size.cy 
= Win32Stuff.GetSystemMetrics(Win32Stuff.SM_CYSCREEN)

            hBitmap 
= GDIStuff.CreateCompatibleBitmap(hDC, size.cx, size.cy)

            
If hBitmap <> IntPtr.Zero Then
                
Dim hOld As IntPtr = DirectCast(GDIStuff.SelectObject(hMemDC, hBitmap), IntPtr)

                GDIStuff.BitBlt(hMemDC, 
00, size.cx, size.cy, hDC, _
                 
00, GDIStuff.SRCCOPY)

                GDIStuff.SelectObject(hMemDC, hOld)
                GDIStuff.DeleteDC(hMemDC)
                Win32Stuff.ReleaseDC(Win32Stuff.GetDesktopWindow(), hDC)
                
Dim bmp As Bitmap = System.Drawing.Image.FromHbitmap(hBitmap)
                GDIStuff.DeleteObject(hBitmap)
                GC.Collect()
                
Return bmp
            
End If
            
Return Nothing

        
End Function



        
Private Shared Function CaptureCursor(ByRef x As IntegerByRef y As IntegerAs Bitmap
            
Dim bmp As Bitmap
            
Dim hicon As IntPtr
            
Dim ci As New Win32Stuff.CURSORINFO()
            
Dim icInfo As Win32Stuff.ICONINFO
            ci.cbSize 
= Marshal.SizeOf(ci)
            
If Win32Stuff.GetCursorInfo(ci) Then
                
If ci.flags = Win32Stuff.CURSOR_SHOWING Then
                    hicon 
= Win32Stuff.CopyIcon(ci.hCursor)
                    
If Win32Stuff.GetIconInfo(hicon, icInfo) Then
                        x 
= ci.ptScreenPos.x - CInt(icInfo.xHotspot)
                        y 
= ci.ptScreenPos.y - CInt(icInfo.yHotspot)

                        
Dim ic As Icon = Icon.FromHandle(hicon)
                        bmp 
= ic.ToBitmap()
                        
Return bmp
                    
End If
                
End If
            
End If

            
Return Nothing
        
End Function


        
Public Shared Function CaptureDesktopWithCursor() As Bitmap
            
Dim cursorX As Integer = 0
            
Dim cursorY As Integer = 0
            
Dim desktopBMP As Bitmap
            
Dim cursorBMP As Bitmap
            
Dim g As Graphics
            
Dim r As Rectangle

            desktopBMP 
= CaptureDesktop()
            cursorBMP 
= CaptureCursor(cursorX, cursorY)
            
If desktopBMP IsNot Nothing Then
                
If cursorBMP IsNot Nothing Then
                    r 
= New Rectangle(cursorX, cursorY, cursorBMP.Width, cursorBMP.Height)
                    g 
= Graphics.FromImage(desktopBMP)
                    g.DrawImage(cursorBMP, r)
                    g.Flush()

                    
Return desktop                
Else
                    
Return desktopBMP
                
End If
            
End If

            
Return Nothing

        
End Function

  
End Class

End Namespace
 
原创粉丝点击