Click here to Skip to main content
12,512,068 members (50,243 online)
Click here to Skip to main content
Add your own
alternative version

Stats

92.3K views
3.4K downloads
17 bookmarked
Posted

VB6 Save Form Image To File

, 27 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Save Visual Basic 6 Form's image as seen on screen to a file

Introduction

This is sample code to save image of the VB 6.0 form to a file.

Background

Normally the Form.image property only provides the drawing and printed text in the bitmap image. However if there are image controls, buttons, icons etc on the form then this code pictures those too.

Using the code

There is one single procedure SaveFormImageToFile which is self explainatory. The API BitBlt is used to convert form image to a picture and assign it to picture box. Then the image property of the picture box is used to store the picture using SavePicture methode.

Add a separate Picture box and command button on the form with its properties as given below:

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

        

Otherwise the code will set it.

Points of Interest

Image of the form in VB 6 along with the controls and their images is new.

History

This code is my first upload

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralScreen dump to file Pin
Member 13347154-Aug-15 0:36
memberMember 13347154-Aug-15 0:36 
QuestionFull form Pin
Member 1125657522-Nov-14 22:24
memberMember 1125657522-Nov-14 22:24 
GeneralMy vote of 1 Pin
NickHallick15-May-14 1:59
groupNickHallick15-May-14 1:59 
QuestionThanks A Lot Pin
mpmehul2-Mar-14 2:16
membermpmehul2-Mar-14 2:16 
QuestionSave of images if the image falls below the screen bottom Pin
Christopher Ng 2223-Mar-12 5:22
memberChristopher Ng 2223-Mar-12 5:22 
GeneralSavePicture Function Pin
Garry Lowther27-Jan-09 5:51
memberGarry Lowther27-Jan-09 5:51 
AnswerRe: SavePicture Function Pin
Member 7283747-May-09 1:53
memberMember 7283747-May-09 1:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 27 Jan 2008
Article Copyright 2008 by Santosh Visal
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid