方法一:
模拟键盘prtscr键
vb实现
Dim img As Image
SendKeys.SendWait("{prtsc}")
img = My.Computer.Clipboard.GetImage
img.Save(Application.StartupPath & "\2.jpg")
C#实现
Bitmap img;
SendKeys.SendWait("{prtsc}");
IDataObject data = Clipboard.GetDataObject();//从剪贴板中获取数据
img = (Bitmap)data.GetData(typeof(Bitmap));
img.Save("c:\\a.bmp");
方法二:
使用API
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As Int32) As Int32
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer
'屏幕截图
Public Shared Sub ScreenToBitmap()
Dim DisplayDC As New IntPtr(CreateDC("DISPLAY", Nothing, Nothing, 0))
Dim G1 As Graphics = Graphics.FromHdc(DisplayDC)
Dim Bmp As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height, G1)
Dim G2 As Graphics = Graphics.FromImage(Bmp)
Dim BmpDC As IntPtr = G2.GetHdc()
BitBlt(BmpDC.ToInt32, 0, 0, My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height, DisplayDC.ToInt32, My.Computer.Screen.Bounds.Left, My.Computer.Screen.Bounds.Top, &HCC0020)
G2.ReleaseHdc(BmpDC)
DisplayDC = Nothing
'Return Bmp
Bmp.Save("1.bmp")
End Sub
End Class