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.
Anwendungsbeispiele:
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.
VB.NET-Quellcode
- Imports System.Runtime.InteropServices
- Public Class ScreenFromWindow
- <DllImport("user32.dll")> _
- Private Shared Function PrintWindow(ByVal hwnd As IntPtr, ByVal hdcBlt As IntPtr, ByVal nFlags As UInteger) As Boolean
- End Function
- <DllImport("user32.dll")> _
- Private Shared Function GetWindowRect(ByVal hwnd As IntPtr, ByRef rc As RECT) As Integer
- End Function
- 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
- Dim b As New Bitmap(Width, Height)
- Dim gg As Graphics = Graphics.FromImage(b)
- Dim bm As Bitmap = GetFromAll(WindowProcess)
- gg.DrawImage(bm, New Rectangle(0, 0, Width, Height), xPosition, yPosition, Width, Height, GraphicsUnit.Pixel)
- Return b
- End Function
- Public Function GetFromAll(ByVal WindowProcess As Process) As Bitmap
- Dim p As IntPtr = WindowProcess.MainWindowHandle
- Dim rc As RECT
- GetWindowRect(p, rc)
- Dim bm As New Bitmap(rc.Right - rc.Left, rc.Bottom - rc.Top)
- Dim g As Graphics = Graphics.FromImage(bm)
- Dim hdc As IntPtr = g.GetHdc()
- PrintWindow(p, hdc, 0)
- g.ReleaseHdc(hdc)
- g.Flush()
- g.Dispose()
- Return bm
- End Function
- End Class
- <StructLayout(LayoutKind.Sequential)> _
- Public Structure RECT
- Public Left As Integer
- Public Top As Integer
- Public Right As Integer
- Public Bottom As Integer
- End Structure
Anwendungsbeispiele:
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.