Rubrik: Grafik und Font · Sonstiges | VB-Versionen: VB4, VB5, VB6 | 22.09.03 |
Form-/PictureBox-Inhalt als Bild speichern Diesmal verraten wir Ihnen, wie man den Inhalt einer Form oder einer PictureBox einschl. aller enthaltenen Objekte als Bild speichern kann. | ||
Autor: Zardoz | Bewertung: | Views: 35.370 |
ohne Homepage | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Diesmal verraten wir Ihnen, wie man den Inhalt einer Form oder einer PictureBox einschl. aller enthaltenen Objekte als Bild speichern kann.
' Benötigte API-Deklaration 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)
Private Sub BildSpeichern(oContainer As Object, ByVal sFile As String) ' Inhalt einer Form/PictureBox als Bild speichern Dim FrmOldScaleMode As Integer Dim PicOldScaleMode As Integer Dim OldAutoRedraw As Boolean ' Scale-Mode auf Pixel setzen FrmOldScaleMode = Me.ScaleMode Me.ScaleMode = vbPixels ' Eigenschaften der 2. PictureBox, die als ' Zwischenspeicher dient With Picture2 .Visible = False .BorderStyle = 0 .AutoRedraw = True .ScaleMode = vbPixels ' 2. PictureBox über das Container-Objekt legen With oContainer PicOldScaleMode = .ScaleMode .ScaleMode = vbPixels Picture2.Move 0, 0, .ScaleWidth, .ScaleHeight Me.ScaleMode = FrmOldScaleMode OldAutoRedraw = .AutoRedraw ' Inhalt des Containers in die 2. PictureBox kopieren .AutoRedraw = False BitBlt Picture2.hDC, 0, 0, .ScaleWidth, .ScaleHeight, _ .hDC, 0, 0, vbSrcCopy .AutoRedraw = OldAutoRedraw .ScaleMode = PicOldScaleMode End With ' Inhalt der 2. PictureBox als Bitmap speichern SavePicture .Image, sFile .Cls .AutoRedraw = False End With End Sub
Erstellen Sie ein neues Projekt und platzieren Sie auf die Form 2 PictureBox-Controls (Picture1 und Picture2). Laden Sie ein Bild in die 1. PictureBox und setzen zusätzlich noch ein paar Objekte (Controls) in die PictureBox. Fügen Sie der Form noch einen CommandButton hinzu und kopieren obigen Code in den Codeteil der Form. Beim Klick auf den CommandButton wird der komplette Inhalt der 1. PictureBox in eine Datei gespeichert, sowohl das Bild selbst, als auch die enthaltenen Objekte (Controls).
Private Sub Command1_Click() BildSpeichern Picture1, "c:\bild1.bmp" End Sub
Soll der Inhalt der gesamten Form als Bild gespeichert werden, müssen Sie als 1. Parameter einfach nur das Form-Objekt angeben:
BildSpeichern Form1, "c:\bild1.bmp"