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



我正试图通过点击屏幕上的按钮将表单的屏幕截图保存为.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)As System.Drawing.Bitmap"的参数"s"指定参数。">

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

然后给出错误:

未声明名称"Control"。

这段代码没有截图表单,而是截图整个屏幕。要获取表单的屏幕截图,您需要此代码,要使用它,请参阅下面的小节。参数应该是您想要截图的表单的名称。在这种情况下,我只将Me称为主窗体窗口

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

最新更新