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

  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
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值