保存表单的屏幕截图时,未为参数指定参数

时间:2017-10-29 15:34:00

标签: vb.net

我正在尝试通过单击屏幕上的按钮将表单的屏幕截图保存为.png file,我用来截取表单的代码是:

Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
    Dim tmpImg As New Bitmap(Control.Width, Control.Height)
    Using g As Graphics = Graphics.FromImage(tmpImg)
        g.CopyFromScreen(Control.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(Control.Width, Control.Height))
    End Using
    Return tmpImg
End Function

然后在buttons_click代码

中调用此函数
Private Sub SaveBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintBox.Click
    Call TakeScreenShot()
End Sub

给出错误:

  

错误1未为'Public Function Screenshot(s As System.Windows.Forms.Control)的参数's'指定参数As System.Drawing.Bitmap'。“

Private Sub SaveBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintBox.Click
    Call TakeScreenShot(Control)
End Sub

然后给出错误:

  

名称'Control'未声明。

1 个答案:

答案 0 :(得分:2)

此代码不会截取表单的截图,它会截取整个屏幕截图。要获取表单的屏幕截图,您需要使用此代码,并使用它,请参阅下面的子代码。该参数应该是您要截取屏幕截图的表单的名称。在这种情况下,我只是将Private Function TakeScreenShot(ByVal Control As Control) As Bitmap Dim tmpImg As New Bitmap(Control.Width, Control.Height) Control.DrawToBitmap(tmpImg, New Rectangle(0, 0, Control.Width, Control.Height)) Return tmpImg End Function Private Sub Button99_Click(sender As Object, e As EventArgs) Handles Button99.Click Dim bmp As Bitmap bmp = TakeScreenShot(Me) End Sub 称为主窗体

{{1}}