Screenshot von verdecken Programm (via Programmhandler)

    • VB.NET

      Screenshot von verdecken Programm (via Programmhandler)

      Hier ist ein Code um einen Screenshot von einem Programm zu machen, welches sich im Hintergrund befindet, heißt es muss nicht den Focus haben, so wie es z.B. bei CopyFromScreen() der fall sein muss.

      VB.NET-Quellcode

      1. Imports System.Runtime.InteropServices
      2. Public Class ScreenFromWindow
      3. <DllImport("user32.dll")> _
      4. Private Shared Function PrintWindow(ByVal hwnd As IntPtr, ByVal hdcBlt As IntPtr, ByVal nFlags As UInteger) As Boolean
      5. End Function
      6. <DllImport("user32.dll")> _
      7. Private Shared Function GetWindowRect(ByVal hwnd As IntPtr, ByRef rc As RECT) As Integer
      8. End Function
      9. Public Function GetFromRect(ByVal WindowProcess As Process, ByVal xPosition As Integer, ByVal yPosition As Integer, ByVal Width As Integer, ByVal Height As Integer) As Bitmap
      10. Dim b As New Bitmap(Width, Height)
      11. Dim gg As Graphics = Graphics.FromImage(b)
      12. Dim bm As Bitmap = GetFromAll(WindowProcess)
      13. gg.DrawImage(bm, New Rectangle(0, 0, Width, Height), xPosition, yPosition, Width, Height, GraphicsUnit.Pixel)
      14. Return b
      15. End Function
      16. Public Function GetFromAll(ByVal WindowProcess As Process) As Bitmap
      17. Dim p As IntPtr = WindowProcess.MainWindowHandle
      18. Dim rc As RECT
      19. GetWindowRect(p, rc)
      20. Dim bm As New Bitmap(rc.Right - rc.Left, rc.Bottom - rc.Top)
      21. Dim g As Graphics = Graphics.FromImage(bm)
      22. Dim hdc As IntPtr = g.GetHdc()
      23. PrintWindow(p, hdc, 0)
      24. g.ReleaseHdc(hdc)
      25. g.Flush()
      26. g.Dispose()
      27. Return bm
      28. End Function
      29. End Class
      30. <StructLayout(LayoutKind.Sequential)> _
      31. Public Structure RECT
      32. Public Left As Integer
      33. Public Top As Integer
      34. Public Right As Integer
      35. Public Bottom As Integer
      36. End Structure


      Anwendungsbeispiele:

      VB.NET-Quellcode

      1. Dim snap As New ScreenFromWindow
      2. ' Bild des gesamten Fensters
      3. PirctureBox1.Image = snap.GetFromAll(Process.GetProcessesByName("notepad")(0))
      4. ' Teilbereich des Fensters
      5. PirctureBox1.Image = snap.GetFromRect(Process.GetProcessesByName("notepad")(0), 212, 300, 50, 23)


      Die Parameter für den Teilbereich sind einfach xPosition (Pixelabstand von der linken Kante), yPosition (Pixelabstand von der Oberkante), Breite des Bereichs, Höhe des Bereichs.

      Das ganz funktioniert NICHT wenn das Fenster Minimiert ist und bei MDI Fenstern gibt es wohl auch probleme, konnte ich bisher noch nicht testen.