SendKeys和拍摄屏幕照片

时间:2011-07-06 20:07:56

标签: .net vb.net vb.net-2010

我制作了这个VB.NET代码来拍摄我整个屏幕的照片,但是它只拍摄了一张聚焦区域的照片。这是为什么?

  Public Function SaveScreen(ByVal theFile As String) As Boolean

            Try
                SendKeys.Send("{PRTSC}")
                Application.DoEvents()

                Dim data As IDataObject = Clipboard.GetDataObject()

                If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
                    Dim bmp As Bitmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)
                    bmp.Save(theFile, Imaging.ImageFormat.Png)
                End If
                Clipboard.SetDataObject(0)      'save memory by removing the image from the clipboard
                Return True
            Catch ex As Exception
                Return False
            End Try

        End Function

1 个答案:

答案 0 :(得分:0)

而不是SendKeys,你可以试试API函数keybd_event

Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, _
ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

Private Const kbdDown = 0
Private Const kbdUp = 2



Private Sub SendKey(ByVal Key As Byte)
    Call keybd_event(Key, 0, kbdDown, 0)
    Call keybd_event(Key, 0, kbdUp, 0)

End Sub

Print-Key的密钥代码为42(0x2A)