2011-07-06 92 views
0

目前,我有以下VB.NET代码,使我的桌面的截图,但是只需要激活屏幕的画面:拍摄画面的照片与VB.NET

Public Function SaveScreen(ByVal theFile As String) As Boolean 

     Try 
      SendKeys.Send("%{PRTSC}")   '<alt + printscreen> 
      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 

下面的代码是我如何执行上述功能,如果这有什么差别,我不认为它的作用:

SaveScreen("C:\Lexer_trace\screen.png") 

现在,我需要能够把整个屏幕的图片,而不仅仅是聚焦窗口。我将如何做到这一点?

由于提前,

洛根

+0

[此链接](http://vbtab.blogspot.com/2013/11/how-to-take-a-screenshot-print-screen-using-vb- net.html)可能会解释得很好.. – Jasir

回答

2

您的评论说,您要发送alt + printscreen这不只是捕捉当前活动窗口。

如果你只是发送printscreen它应该捕获整个桌面。

+0

我刚刚尝试过,并且不起作用。还有其他建议吗?我甚至尝试+ {PRTSC} ... – Logan

+0

@Logan - 没有别的使用系统调用。 – ChrisF

4

您应该使用System.Drawing.Graphics.CopyFromScreen() See Here从屏幕

复制简单查询屏幕的全尺寸在通过为点。 类似的东西对你有什么用.CopyFromScreen()

Public Sub SaveScreen(filename As String) 

    Dim screenSize = SystemInformation.PrimaryMonitorSize 
    Dim bitmap = New Bitmap(screenSize.Width, screenSize.Height) 
    Dim g = Graphics.FromImage(bitmap) 

    g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize) 
    g.Flush() 
    bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png) 

End Sub 
0

好眼前的解决将是只发送打印屏幕:

SendKeys.Send("{PRTSC}") 

但是,这充其量是一个跛脚的黑客行为。要可靠地截屏,您需要使用桌面句柄(0)的GetDC和其内容的BitBlt的P/Invoke到Bitmap。在完成之前,请不要忘记桌面上的直流电ReleaseDC

或者使用Graphics.CopyFromScreen

0

你试过不发送Alt键呢?

是这样的:

SendKeys.Send("{PRTSC}")   '<printscreen> 
+0

我刚刚尝试过,并且不起作用。还有其他建议吗?我甚至尝试过+ {PRTSC} ... – Logan