• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

Create an image of the form

Hi, is it possible to create an image of a form and save it as an image to go in a picture box or image control.
I was thinking there must be a priunt screen sort of command that could be sent a forms handle as an argument.

Hope it's as easy as that but I doubt it

Cheers
0
cymru_cowboy
Asked:
cymru_cowboy
1 Solution
 
JNSTAUBCommented:


please find thereafter MICROSOFT CODE:
 
Option Explicit
Option Base 0

Type PALETTEENTRY
        peRed As Byte
        peGreen As Byte
        peBlue As Byte
        peFlags As Byte
End Type
Type guid
Data1 As Long
data2 As Integer
data3 As Integer
data4(7) As Byte
End Type
Type LOGPALETTE
        palVersion As Integer
        palNumEntries As Integer
        palPalEntry(255) As PALETTEENTRY
End Type

Private Const RASTERCAPS = 38

Private Const RC_PALETTE = &H100

Private Const SIZEPALETTE = 104

Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long

Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long

Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long

Declare Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long

Declare Function CreatePalette Lib "gdi32" (lpLogPalette As LOGPALETTE) As Long

Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long

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

Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long

Declare Function GetForegroundWindow Lib "user32" () As Long

Declare Function SelectPalette Lib "gdi32" (ByVal hdc As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long

Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long

Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long

Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

Declare Function GetDesktopWindow Lib "user32" () As Long
Private Type PicBMp
    size As Long
    type As Long
    hbmp As Long
    hpal As Long
    reserved As Long
End Type
Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBMp, RefIID As guid, ByVal fPictureOwnsHandle As Long, IPIc As IPicture) As Long

Public Function CreateBitmapPicture(ByVal hbmp As Long, ByVal hpal As Long) As Picture
Dim r As Long
Dim Pic As PicBMp
Dim IPIc As IPicture
Dim IID_Idispatch As guid
With IID_Idispatch
.Data1 = &H20400
.data4(0) = &HC0
.data4(7) = &H46
End With
With Pic
.size = Len(Pic)
.hbmp = hbmp
.type = vbPicTypeBitmap
.hpal = hpal
End With

r = OleCreatePictureIndirect(Pic, IID_Idispatch, 1, IPIc)
Set CreateBitmapPicture = IPIc
End Function

Public Function captureWindow(ByVal hwndsrc As Long, ByVal client As Boolean, ByVal leftsrc As Long, ByVal topsrc As Long, ByVal widthsrc As Long, ByVal heightsrc As Long) As Picture
Dim hdcmemory As Long
Dim hbmp As Long
Dim hbmpprev As Long
Dim r As Long
Dim hdcsrc As Long
Dim hpal As Long
Dim hpalprev As Long
Dim rastercapsscrn As Long
Dim HasPaletteScrn As Long
Dim PaletteSizeScrn As Long
Dim logpal As LOGPALETTE
If client Then
hdcsrc = GetDC(hwndsrc)
Else
hdcsrc = GetWindowDC(hwndsrc)
End If
hdcmemory = CreateCompatibleDC(hdcsrc)
hbmp = CreateCompatibleBitmap(hdcsrc, widthsrc, heightsrc)
hbmpprev = SelectObject(hdcmemory, hbmp)
rastercapsscrn = GetDeviceCaps(hdcsrc, RASTERCAPS)
HasPaletteScrn = rastercapsscrn And RC_PALETTE
PaletteSizeScrn = GetDeviceCaps(hdcsrc, SIZEPALETTE)
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
logpal.palVersion = &H300
logpal.palNumEntries = 256
r = GetSystemPaletteEntries(hdcsrc, 0, 256, logpal.palPalEntry(0))
hpal = CreatePalette(logpal)
hpalprev = SelectPalette(hdcmemory, hpal, 0)
r = RealizePalette(hdcmemory)
End If
r = BitBlt(hdcmemory, 0, 0, widthsrc, heightsrc, hdcsrc, leftsrc, topsrc, vbSrcCopy)
hbmp = SelectObject(hdcmemory, hbmpprev)
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
hpal = SelectPalette(hdcmemory, hpalprev, 0)
End If
r = DeleteDC(hdcmemory)
r = ReleaseDC(hwndsrc, hdcsrc)
Set captureWindow = CreateBitmapPicture(hbmp, hpal)
End Function

Public Function capturescreen() As Picture
Dim hwndscreen As Long
hwndscreen = GetDesktopWindow()
Set capturescreen = captureWindow(hwndscreen, False, 0, 0, Screen.Width \ Screen.TwipsPerPixelX, Screen.Height \ Screen.TwipsPerPixelY)

End Function

Public Function captureform(frmsrc As Form) As Picture
Set captureform = captureWindow(frmsrc.hwnd, False, 0, 0, frmsrc.ScaleX(frmsrc.Width, vbTwips, vbPixels), frmsrc.ScaleY(frmsrc.Height, vbTwips, vbPixels))
End Function
Public Function captureclient(frmsrc As Form) As Picture
Set captureclient = captureWindow(frmsrc.hwnd, True, 0, 0, frmsrc.ScaleX(frmsrc.ScaleWidth, frmsrc.ScaleMode, vbPixels), frmsrc.ScaleY(frmsrc.ScaleHeight, frmsrc.ScaleMode, vbPixels))
End Function
Public Function captureActiveWindow() As Picture
Dim hwndactive As Long
Dim r As Long
Dim rectactive As RECT
hwndactive = GetForegroundWindow()
r = GetWindowRect(hwndactive, rectactive)
Set captureActiveWindow = captureWindow(hwndactive, False, 0, 0, rectactive.Right - rectactive.Left, rectactive.Bottom - rectactive.Top)
End Function





End Function

********************
best regards
0
 
bobbit31Commented:
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 Const SRCCOPY = &HCC0020 ' (DWORD) dest = source


Private Sub Command1_Click()

Dim retVal As Integer
Picture1.Visible = False
Picture1.AutoRedraw = True
Picture1.ScaleMode = 3
Picture1.Width = Me.ScaleWidth
Picture1.Height = Me.ScaleHeight

'' now write to Picture1r hdc
retVal = BitBlt(Picture1.hDC, 0, 0, Me.ScaleWidth, Me.ScaleHeight, _
        Me.hDC, 0, 0, SRCCOPY)

Picture1 now holds the image of the form (make it visible if you want to see)
0
 
cymru_cowboyAuthor Commented:
Really sorry I haven't had a chance to test this code, I will be using it shortly however given half a chance. Until then I'll award the points to JNSTAUB for getting in their first.
But thanks to bobbit31 for the help as well
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now