vb.net form2 和 form1 的屏幕截图
vb.net Screen Capture of form2 with form1
我这里有问题。
我有一个 form1,我用它来获取设置等等。
但是我也想使用一个面板来查看 form2 中的内容。
Form2 = 没有 formborderstyles 的全屏表单
这个 form2 在大屏幕上显示信息,我需要在我的 form1(在这个面板中)中看到 form2 的全部内容,就像屏幕截图一样。
有人可以帮忙吗?
一个Panel在WindowsForms中会产生闪烁的问题,建议你放置一个PictureBox ] 在 Panel 内设置了 PictureBox.Dock = Fill
属性(或者只使用 PictureBox 而不是 Panel) 然后你可以使用例如我的 TakeScreenshotFromForm() function from my ElektroKit Framework.
一个完整的工作示例:
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Public Class Form1 : Inherits Form
Friend WithEvents ScreenshotTimer As New System.Windows.Forms.Timer
Private Sub Test() Handles MyBase.Shown
Form2.Show()
With Me.ScreenshotTimer
.Interval = 100
.Enabled = True
.Start()
End With
Me.PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
End Sub
Private Sub ScreenshotTimer_Tick(sender As Object, e As EventArgs) Handles ScreenshotTimer.Tick
If Me.PictureBox1.BackgroundImage IsNot Nothing Then
Me.PictureBox1.BackgroundImage.Dispose()
End If
Me.PictureBox1.BackgroundImage = TakeScreenshotFromForm(Form2, includeMouse:=True)
End Sub
Public Shared Function TakeScreenshotFromForm(ByVal f As Form,
Optional ByVal includeMouse As Boolean = False,
Optional ByVal pixelFormat As PixelFormat = PixelFormat.Format24bppRgb) As Image
If Not f.Visible Then
Return Nothing
End If
Dim bmp As New Bitmap(f.Size.Width, f.Size.Height, pixelFormat)
Using g As Graphics = Graphics.FromImage(bmp)
g.InterpolationMode = InterpolationMode.Default
g.PixelOffsetMode = PixelOffsetMode.Default
g.CopyFromScreen(f.Location, New Drawing.Point(0, 0), f.Size)
' Draw the cursor in the image.
If includeMouse Then
Dim cursorSize As System.Drawing.Size = CType(f.Cursor.HotSpot, System.Drawing.Size)
cursorSize.Width -= ((f.Size.Width - f.ClientSize.Width) \ 2)
cursorSize.Height -= ((f.Size.Height - f.ClientSize.Height) - ((f.Size.Width - f.ClientSize.Width) \ 2))
Dim formPoint As Drawing.Point = f.PointToClient(Drawing.Point.Subtract(Control.MousePosition, cursorSize))
Cursors.Arrow.Draw(g, New Rectangle(formPoint.X, formPoint.Y, cursorSize.Width, cursorSize.Height))
End If
End Using
Return bmp
End Function
End Class
除了我使用的方法,您还可以使用 Control.DrawToBitmap() 方法在不可见时正确捕获表单(在屏幕上完全不可见,或被其他 windows) 覆盖,但生成的图像将不包含某些 "information",例如 TextBox 插入符号。
Dim bmp As New Bitmap(f.Bounds.Size.Width, f.Bounds.Size.Height, pixelFormat)
f.DrawToBitmap(bmp, New Rectangle(0, 0, f.Bounds.Size.Width, f.Bounds.Size.Height))
' ...
我这里有问题。 我有一个 form1,我用它来获取设置等等。 但是我也想使用一个面板来查看 form2 中的内容。
Form2 = 没有 formborderstyles 的全屏表单
这个 form2 在大屏幕上显示信息,我需要在我的 form1(在这个面板中)中看到 form2 的全部内容,就像屏幕截图一样。
有人可以帮忙吗?
一个Panel在WindowsForms中会产生闪烁的问题,建议你放置一个PictureBox ] 在 Panel 内设置了 PictureBox.Dock = Fill
属性(或者只使用 PictureBox 而不是 Panel) 然后你可以使用例如我的 TakeScreenshotFromForm() function from my ElektroKit Framework.
一个完整的工作示例:
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Public Class Form1 : Inherits Form
Friend WithEvents ScreenshotTimer As New System.Windows.Forms.Timer
Private Sub Test() Handles MyBase.Shown
Form2.Show()
With Me.ScreenshotTimer
.Interval = 100
.Enabled = True
.Start()
End With
Me.PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
End Sub
Private Sub ScreenshotTimer_Tick(sender As Object, e As EventArgs) Handles ScreenshotTimer.Tick
If Me.PictureBox1.BackgroundImage IsNot Nothing Then
Me.PictureBox1.BackgroundImage.Dispose()
End If
Me.PictureBox1.BackgroundImage = TakeScreenshotFromForm(Form2, includeMouse:=True)
End Sub
Public Shared Function TakeScreenshotFromForm(ByVal f As Form,
Optional ByVal includeMouse As Boolean = False,
Optional ByVal pixelFormat As PixelFormat = PixelFormat.Format24bppRgb) As Image
If Not f.Visible Then
Return Nothing
End If
Dim bmp As New Bitmap(f.Size.Width, f.Size.Height, pixelFormat)
Using g As Graphics = Graphics.FromImage(bmp)
g.InterpolationMode = InterpolationMode.Default
g.PixelOffsetMode = PixelOffsetMode.Default
g.CopyFromScreen(f.Location, New Drawing.Point(0, 0), f.Size)
' Draw the cursor in the image.
If includeMouse Then
Dim cursorSize As System.Drawing.Size = CType(f.Cursor.HotSpot, System.Drawing.Size)
cursorSize.Width -= ((f.Size.Width - f.ClientSize.Width) \ 2)
cursorSize.Height -= ((f.Size.Height - f.ClientSize.Height) - ((f.Size.Width - f.ClientSize.Width) \ 2))
Dim formPoint As Drawing.Point = f.PointToClient(Drawing.Point.Subtract(Control.MousePosition, cursorSize))
Cursors.Arrow.Draw(g, New Rectangle(formPoint.X, formPoint.Y, cursorSize.Width, cursorSize.Height))
End If
End Using
Return bmp
End Function
End Class
除了我使用的方法,您还可以使用 Control.DrawToBitmap() 方法在不可见时正确捕获表单(在屏幕上完全不可见,或被其他 windows) 覆盖,但生成的图像将不包含某些 "information",例如 TextBox 插入符号。
Dim bmp As New Bitmap(f.Bounds.Size.Width, f.Bounds.Size.Height, pixelFormat)
f.DrawToBitmap(bmp, New Rectangle(0, 0, f.Bounds.Size.Width, f.Bounds.Size.Height))
' ...