使用.NET2.0新增的类截取屏幕
来源:互联网 发布:ubuntu调分辨率 编辑:程序博客网 时间:2024/04/30 09:15
Public Shared Function cap()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(0, 0, 0, 0, _
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截屏的例子Dim desktopbmp As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(desktopbmp)
g.CopyFromScreen(0, 0, 0, 0, _
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
Win32Stuff.vb
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Namespace ScreenshotCaptureWithMouseNamespace ScreenshotCaptureWithMouse.ScreenCapture
Class Win32StuffClass Win32Stuff
Class Variables#Region "Class Variables"
Public Const SM_CXSCREEN As Integer = 0
Public Const SM_CYSCREEN As Integer = 1
Public Const CURSOR_SHOWING As Int32 = 1
<StructLayout(LayoutKind.Sequential)> _
Public Structure ICONINFOStructure ICONINFO
Public fIcon As Boolean
' Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies
Public xHotspot As Int32
' Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot
Public yHotspot As Int32
' Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot
Public hbmMask As IntPtr
' (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon,
Public hbmColor As IntPtr
' (HBITMAP) Handle to the icon color bitmap. This member can be optional if this
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure POINTStructure POINT
Public x As Int32
Public y As Int32
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure CURSORINFOStructure CURSORINFO
Public cbSize As Int32
' Specifies the size, in bytes, of the structure.
Public flags As Int32
' Specifies the cursor state. This parameter can be one of the following values:
Public hCursor As IntPtr
' Handle to the cursor.
Public ptScreenPos As POINT
' A POINT structure that receives the screen coordinates of the cursor.
End Structure
#End Region
Class Functions#Region "Class Functions"
<DllImport("user32.dll", EntryPoint:="GetDesktopWindow")> _
Public Shared Function GetDesktopWindow()Function GetDesktopWindow() As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="GetDC")> _
Public Shared Function GetDC()Function GetDC(ByVal ptr As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="GetSystemMetrics")> _
Public Shared Function GetSystemMetrics()Function GetSystemMetrics(ByVal abc As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="GetWindowDC")> _
Public Shared Function GetWindowDC()Function GetWindowDC(ByVal ptr As Int32) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="ReleaseDC")> _
Public Shared Function ReleaseDC()Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDc As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="GetCursorInfo")> _
Public Shared Function GetCursorInfo()Function GetCursorInfo(ByRef pci As CURSORINFO) As Boolean
End Function
<DllImport("user32.dll", EntryPoint:="CopyIcon")> _
Public Shared Function CopyIcon()Function CopyIcon(ByVal hIcon As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="GetIconInfo")> _
Public Shared Function GetIconInfo()Function GetIconInfo(ByVal hIcon As IntPtr, ByRef piconinfo As ICONINFO) As Boolean
End Function
#End Region
End Class
End Namespace
GDIStuff.vbImports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Namespace ScreenshotCaptureWithMouseNamespace ScreenshotCaptureWithMouse.ScreenCapture
Class Win32StuffClass Win32Stuff
Class Variables#Region "Class Variables"
Public Const SM_CXSCREEN As Integer = 0
Public Const SM_CYSCREEN As Integer = 1
Public Const CURSOR_SHOWING As Int32 = 1
<StructLayout(LayoutKind.Sequential)> _
Public Structure ICONINFOStructure ICONINFO
Public fIcon As Boolean
' Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies
Public xHotspot As Int32
' Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot
Public yHotspot As Int32
' Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot
Public hbmMask As IntPtr
' (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon,
Public hbmColor As IntPtr
' (HBITMAP) Handle to the icon color bitmap. This member can be optional if this
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure POINTStructure POINT
Public x As Int32
Public y As Int32
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure CURSORINFOStructure CURSORINFO
Public cbSize As Int32
' Specifies the size, in bytes, of the structure.
Public flags As Int32
' Specifies the cursor state. This parameter can be one of the following values:
Public hCursor As IntPtr
' Handle to the cursor.
Public ptScreenPos As POINT
' A POINT structure that receives the screen coordinates of the cursor.
End Structure
#End Region
Class Functions#Region "Class Functions"
<DllImport("user32.dll", EntryPoint:="GetDesktopWindow")> _
Public Shared Function GetDesktopWindow()Function GetDesktopWindow() As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="GetDC")> _
Public Shared Function GetDC()Function GetDC(ByVal ptr As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="GetSystemMetrics")> _
Public Shared Function GetSystemMetrics()Function GetSystemMetrics(ByVal abc As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="GetWindowDC")> _
Public Shared Function GetWindowDC()Function GetWindowDC(ByVal ptr As Int32) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="ReleaseDC")> _
Public Shared Function ReleaseDC()Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDc As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="GetCursorInfo")> _
Public Shared Function GetCursorInfo()Function GetCursorInfo(ByRef pci As CURSORINFO) As Boolean
End Function
<DllImport("user32.dll", EntryPoint:="CopyIcon")> _
Public Shared Function CopyIcon()Function CopyIcon(ByVal hIcon As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="GetIconInfo")> _
Public Shared Function GetIconInfo()Function GetIconInfo(ByVal hIcon As IntPtr, ByRef piconinfo As ICONINFO) As Boolean
End Function
#End Region
End Class
End Namespace
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Namespace ScreenshotCaptureWithMouseNamespace ScreenshotCaptureWithMouse.ScreenCapture
Class GDIStuffClass GDIStuff
Class Variables#Region "Class Variables"
Public Const SRCCOPY As Integer = 13369376
#End Region
Class Functions#Region "Class Functions"
<DllImport("gdi32.dll", EntryPoint:="CreateDC")> _
Public Shared Function CreateDC()Function CreateDC(ByVal lpszDriver As IntPtr, ByVal lpszDevice As String, ByVal lpszOutput As IntPtr, ByVal lpInitData As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="DeleteDC")> _
Public Shared Function DeleteDC()Function DeleteDC(ByVal hDc As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="DeleteObject")> _
Public Shared Function DeleteObject()Function DeleteObject(ByVal hDc As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="BitBlt")> _
Public Shared Function BitBlt()Function BitBlt(ByVal hdcDest As IntPtr, ByVal xDest As Integer, ByVal yDest As Integer, ByVal wDest As Integer, ByVal hDest As Integer, ByVal hdcSource As IntPtr, _
ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal RasterOp As Integer) As Boolean
End Function
<DllImport("gdi32.dll", EntryPoint:="CreateCompatibleBitmap")> _
Public Shared Function CreateCompatibleBitmap()Function CreateCompatibleBitmap(ByVal hdc As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="CreateCompatibleDC")> _
Public Shared Function CreateCompatibleDC()Function CreateCompatibleDC(ByVal hdc As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="SelectObject")> _
Public Shared Function SelectObject()Function SelectObject(ByVal hdc As IntPtr, ByVal bmp As IntPtr) As IntPtr
End Function
#End Region
End Class
End Namespace
CaptureScreen.vbImports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Namespace ScreenshotCaptureWithMouseNamespace ScreenshotCaptureWithMouse.ScreenCapture
Class GDIStuffClass GDIStuff
Class Variables#Region "Class Variables"
Public Const SRCCOPY As Integer = 13369376
#End Region
Class Functions#Region "Class Functions"
<DllImport("gdi32.dll", EntryPoint:="CreateDC")> _
Public Shared Function CreateDC()Function CreateDC(ByVal lpszDriver As IntPtr, ByVal lpszDevice As String, ByVal lpszOutput As IntPtr, ByVal lpInitData As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="DeleteDC")> _
Public Shared Function DeleteDC()Function DeleteDC(ByVal hDc As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="DeleteObject")> _
Public Shared Function DeleteObject()Function DeleteObject(ByVal hDc As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="BitBlt")> _
Public Shared Function BitBlt()Function BitBlt(ByVal hdcDest As IntPtr, ByVal xDest As Integer, ByVal yDest As Integer, ByVal wDest As Integer, ByVal hDest As Integer, ByVal hdcSource As IntPtr, _
ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal RasterOp As Integer) As Boolean
End Function
<DllImport("gdi32.dll", EntryPoint:="CreateCompatibleBitmap")> _
Public Shared Function CreateCompatibleBitmap()Function CreateCompatibleBitmap(ByVal hdc As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="CreateCompatibleDC")> _
Public Shared Function CreateCompatibleDC()Function CreateCompatibleDC(ByVal hdc As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="SelectObject")> _
Public Shared Function SelectObject()Function SelectObject(ByVal hdc As IntPtr, ByVal bmp As IntPtr) As IntPtr
End Function
#End Region
End Class
End Namespace
Imports System.Drawing
Imports System.Runtime.InteropServices
Namespace ScreenshotCaptureWithMouseNamespace ScreenshotCaptureWithMouse.ScreenCapture
Class CaptureScreenClass CaptureScreen
'This structure shall be used to keep the size of the screen.
Public Structure SIZEStructure SIZE
Public cx As Integer
Public cy As Integer
End Structure
Private Shared Function CaptureDesktop()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, 0, 0, size.cx, size.cy, hDC, _
0, 0, 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()Function CaptureCursor(ByRef x As Integer, ByRef y As Integer) As 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()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
Imports System.Runtime.InteropServices
Namespace ScreenshotCaptureWithMouseNamespace ScreenshotCaptureWithMouse.ScreenCapture
Class CaptureScreenClass CaptureScreen
'This structure shall be used to keep the size of the screen.
Public Structure SIZEStructure SIZE
Public cx As Integer
Public cy As Integer
End Structure
Private Shared Function CaptureDesktop()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, 0, 0, size.cx, size.cy, hDC, _
0, 0, 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()Function CaptureCursor(ByRef x As Integer, ByRef y As Integer) As 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()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
- 使用.NET2.0新增的类截取屏幕
- GridView 类 asp.net2.0的新增功能
- 截取屏幕的类
- .net2.0中新增的nullable类型
- .net2.0中新增的nullable类型
- ASP.NET2.0新增
- 【屏幕PrtSc键的使用,截取对话框和全屏截取】
- asp.net2.0新增加的安全控件
- asp.net2.0新增加的安全控件
- asp.net2.0新增加的安全控件(译)
- .Net2.0新增的对文件压缩代码
- ASP.Net2.0新增的数据绑定控件
- [.NET2.0]Marshal类的使用
- 使用.Net2.0的事务
- 屏幕截取_Robot类
- ASP.NET2.0 配置中的新增功能
- android截取view的视图 截取屏幕
- 截取屏幕的实现方法
- FreeBSD使用CVSup升级Ports,让它涣然一新
- C和C++的时间编程
- 一个国家商人地位不能太强
- 转载]一个月赚5万美元--国产共享软件开发者周奕
- Linux .so .a .la 库
- 使用.NET2.0新增的类截取屏幕
- 有个表哥要结婚了,对方是有三次婚姻的女人
- 在Mac OSX启动时运行程序的方法
- 《暖暖》
- c语言中static的用法
- 重定向System.out和System.err到JTextPane,分别用黑色红色显示
- c#数据库连接字符串
- More Effective C++读书笔记(二)
- Kerberos认证工作之第二幕