Rubrik: Grafik und Font · Sonstiges | VB-Versionen: VB5, VB6 | 19.08.04 |
Bildschirmausschnitt fotografieren und abspeichern Hier wird eine Funktion vorgestellt, die einen bestimmten Bildschirmausschnitt "fotografiert" und als Bitmap abspeichert. | ||
Autor: Dieter Otter | Bewertung: | Views: 24.993 |
www.tools4vb.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Nachfolgende Funktion erstellt ein Screenshot eines festlegbaren Bildschirmausschnitts und speichert das Bild dann als Bitmap in die angegebene Datei.
Option Explicit ' Benötigte API-Deklarationen Private Declare Function BitBlt Lib "gdi32" ( _ ByVal hDestDC As Long, _ ByVal x As Long, _ ByVal y As Long, _ ByVal nWidth As Long, _ ByVal nHeight As Long, _ ByVal hSrcDC As Long, _ ByVal xSrc As Long, _ ByVal ySrc As Long, _ ByVal dwRop As Long) As Long Private Declare Function GetDC Lib "user32" ( _ ByVal hWnd As Long) As Long Private Declare Function ReleaseDC Lib "user32" ( _ ByVal hWnd As Long, _ ByVal hdc As Long) As Long Private Declare Function GetDesktopWindow Lib "user32" () As Long
' Screenshot eines bestimmten Bildschirmausschnitts erstellen ' und als Bitmap in einer Datei abspeichern Public Sub Snapshot(oForm As Form, ByVal sFile As String, _ Optional ByVal x As Variant, _ Optional ByVal y As Variant, _ Optional ByVal nWidth As Variant, _ Optional ByVal nHeight As Variant) Dim hWnd As Long Dim nDC As Long Dim oPicBox As Control ' Handle des Windows-Desktop hWnd = GetDesktopWindow() ' Zugang zum Device-Context nDC = GetDC(hWnd) ' Position und Größe des Bildschirmausschnitts If IsMissing(x) Then x = 0 If IsMissing(y) Then y = 0 If IsMissing(nWidth) Then nWidth = Screen.Width / Screen.TwipsPerPixelX If IsMissing(nHeight) Then nHeight = Screen.Height / Screen.TwipsPerPixelY ' temporäre PictureBox erstellen Set oPicBox = oForm.Controls.Add("VB.PictureBox", "myPicTemp") With oPicBox .ScaleMode = vbPixels .Width = oForm.ScaleX(nWidth, vbPixels, oForm.ScaleMode) .Height = oForm.ScaleY(nHeight, vbPixels, oForm.ScaleMode) ' Snapshot in PictureBox blitten .AutoRedraw = True BitBlt .hDC, 0, 0, nWidth, nHeight, nDC, x, y, vbSrcCopy ' Bild abspeichern SavePicture .Image, sFile ' DC wieder freigeben ReleaseDC hWnd, nDC End With ' Control wieder entfernen oForm.Controls.Remove "myPicTemp" End Sub
Achtung! Die Angabe der Position und Größe des Bildschirmausschnitts erfolgt in Pixel!
Werden keine Parameter angegeben, wird der gesamte Bildschirm fotografiert.
Beispiele für den Aufruf:
' gesamten Bildschirmbereich speichern Snapshot Me, "d:\bild.bmp"
' Bildschirmausschnitt 100,100 - 500,500 speichern Snapshot Me, "d:\bild.bmp", 100, 100, 400, 400