'测试时用的是这个。wpf的一个Rectangle里刷了截下的图
Public Shared Overloads Function GetScreenSnapshot() As Brush
Dim rc As Rectangle = SystemInformation.VirtualScreen
Using bitmap As New Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb)
Using g As Graphics = Drawing.Graphics.FromImage(bitmap)
g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy)
End Using
End Using
Return New ImageBrush(ToBitmapSource(bitmap))
End Function
'原始版本
Public Shared Overloads Function GetScreenSnapshot() As Bitmap
Dim rc As Rectangle = SystemInformation.VirtualScreen
Using bitmap As New Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb)
Using g As Graphics = Graphics.FromImage(bitmap)
g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy)
End Using
End Using
Return bitmap
End Function
'类型转换器
Public Shared Function ToBitmapSource(bmp As Bitmap) As BitmapSource
Dim returnSource As BitmapSource = Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())
Return returnSource
End Function
Public Shared Overloads Function GetScreenSnapshot() As Brush
Dim rc As Rectangle = SystemInformation.VirtualScreen
Using bitmap As New Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb)
Using g As Graphics = Drawing.Graphics.FromImage(bitmap)
g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy)
End Using
End Using
Return New ImageBrush(ToBitmapSource(bitmap))
End Function
'原始版本
Public Shared Overloads Function GetScreenSnapshot() As Bitmap
Dim rc As Rectangle = SystemInformation.VirtualScreen
Using bitmap As New Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb)
Using g As Graphics = Graphics.FromImage(bitmap)
g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy)
End Using
End Using
Return bitmap
End Function
'类型转换器
Public Shared Function ToBitmapSource(bmp As Bitmap) As BitmapSource
Dim returnSource As BitmapSource = Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())
Return returnSource
End Function