?
Solved

Create an image of the form

Posted on 2003-02-19
3
Medium Priority
?
165 Views
Last Modified: 2010-05-01
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
Comment
Question by:cymru_cowboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Accepted Solution

by:
JNSTAUB earned 200 total points
ID: 7980615


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
 
LVL 18

Expert Comment

by:bobbit31
ID: 7995379
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
 

Author Comment

by:cymru_cowboy
ID: 8119988
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question