2016-03-21 40 views
1

我在這裏有一個問題。 我有一個form1,我用它來獲取設置等等。 但也有一個面板,我想用來看看form2內什麼。vb.net窗體2與窗體1的屏幕捕獲

窗體2 =無formborderstyles

這個窗口2顯示在大屏幕上的信息,我需要在我的Form1上看到(在此面板)窗口2的像抓屏事情的全部內容全屏形式。

有人可以幫忙嗎?

回答

1

一個面板將產生WindowsForms閃爍的問題,我建議你把一個的PictureBox面板內與PictureBox.Dock = Fill屬性集(或只使用僅的PictureBox的代替面板)那麼你可以使用例如我的TakeScreenshotFromForm()函數從我的ElektroKit框架

enter image description here

一個完整的工作示例:

enter image description here

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()方法是什麼時候properlly捕捉表不可見(屏幕上完全不可見,或被其他窗口覆蓋),但生成的圖像將不包含某些「信息」,例如例如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)) 
' ... 
+0

是不是會在一段時間後超載我的記憶?這是一個正常的截圖相同,還是別的東西? @elektroStudios –

+0

@Thomas Dutoit不​​,每次都會丟棄一次性對象,但是也許您可以注意到每次調用後進程是如何保留更多內存空間的,因爲.Net管理內存的方式(無論如何,您可以強制任何垃圾收集時間來嘗試看到更合適的內存消耗值,因爲不是很推薦強制它)。 「正常截圖」的含義是什麼?我顯示的方法是指定窗體的圖像(截圖),是不是你要求的?,我希望這有助於! – ElektroStudios

+0

在我的答案底部解釋的另一種方法可能是更多的「原始圖像」比截圖,因爲我不直接從屏幕上覆制,因爲我說使用該替代方法,你會失去圖像信息,如焦點矩形或一個插入符號,但另一方面,在屏幕上不可見時捕獲窗體非常有用。 – ElektroStudios