VB6 将窗体图像保存到文件






2.40/5 (11投票s)
将 Visual Basic 6 窗体在屏幕上看到的图像保存到文件
引言
这是将 VB 6.0 窗体图像保存到文件的示例代码。
背景
通常,Form.image 属性仅提供位图图像中的绘制和打印文本。但是,如果窗体上有图像控件、按钮、图标等,则此代码也会捕捉这些图像。
使用代码
只有一个名为 SaveFormImageToFile 的过程,其功能自明。API BitBlt 用于将窗体图像转换为图片并将其分配给 Picture Box。然后使用 SavePicture 方法使用 Picture Box 的 image 属性来存储图片。
在窗体上添加一个单独的 Picture Box 和命令按钮,其属性如下
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 Public Sub SaveFormImageToFile(ByRef ContainerForm As Form, _ ByRef PictureBoxControl As PictureBox, _ ByVal ImageFileName As String) Dim FormInsideWidth As Long Dim FormInsideHeight As Long Dim PictureBoxLeft As Long Dim PictureBoxTop As Long Dim PictureBoxWidth As Long Dim PictureBoxHeight As Long Dim FormAutoRedrawValue As Boolean With PictureBoxControl 'Set PictureBox properties .Visible = False .AutoRedraw = True .Appearance = 0 ' Flat .AutoSize = False .BorderStyle = 0 'No border 'Store PictureBox Original Size and location Values PictureBoxHeight = .Height: PictureBoxWidth = .Width PictureBoxLeft = .Left: PictureBoxTop = .Top 'Make PictureBox to size to inside of form. .Align = vbAlignTop: .Align = vbAlignLeft DoEvents FormInsideHeight = .Height: FormInsideWidth = .Width 'Restore PictureBox Original Size and location Values .Align = vbAlignNone .Height = FormInsideHeight: .Width = FormInsideWidth .Left = PictureBoxLeft: .Top = PictureBoxTop FormAutoRedrawValue = ContainerForm.AutoRedraw ContainerForm.AutoRedraw = False DoEvents 'Copy Form Image to Picture Box BitBlt .hDC, 0, 0, _ FormInsideWidth / Screen.TwipsPerPixelX, _ FormInsideHeight / Screen.TwipsPerPixelY, _ ContainerForm.hDC, 0, 0, _ vbSrcCopy DoEvents SavePicture .Image, ImageFileName DoEvents ContainerForm.AutoRedraw = FormAutoRedrawValue DoEvents End With End Sub Private Sub Command1_Click() SaveFormImageToFile frmSaveFormImageToFile, Picture1, "C:\Temp.bmp" End Sub
否则,代码将进行设置。
关注点
VB 6 中包含控件及其图像的窗体图像是新的。
历史
这是我的第一次上传