Results 1 to 6 of 6

Thread: [Project Dropped]Get Picture Of Form

  1. #1

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475

    Resolved [Project Dropped]Get Picture Of Form

    Hello again everyone. This time Im trying to get a picture of the form in my project and display in in a picture box. Right now Im using this:

    Code:
    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 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
    
       ' Depending on the value of Client get the proper device context.
       If Client Then
          hDCSrc = GetDC(hWndSrc) ' Get device context for client area.
       Else
          hDCSrc = GetWindowDC(hWndSrc) ' Get device context for entire
                                        ' window.
       End If
    
       ' Create a memory device context for the copy process.
       hDCMemory = CreateCompatibleDC(hDCSrc)
       ' Create a bitmap and place it in the memory DC.
       hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
       hBmpPrev = SelectObject(hDCMemory, hBmp)
    
       ' Get screen properties.
       RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
                                                          ' capabilities.
       HasPaletteScrn = RasterCapsScrn And RC_PALETTE       ' Palette
                                                            ' support.
       PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of
                                                            ' palette.
    
       ' If the screen has a palette make a copy and realize it.
       If HasPaletteScrn And (PaletteSizeScrn = 256) Then
          ' Create a copy of the system palette.
          LogPal.palVersion = &H300
          LogPal.palNumEntries = 256
          r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
          hPal = CreatePalette(LogPal)
          ' Select the new palette into the memory DC and realize it.
          hPalPrev = SelectPalette(hDCMemory, hPal, 0)
          r = RealizePalette(hDCMemory)
       End If
    
       ' Copy the on-screen image into the memory DC.
       r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
    
    ' Remove the new copy of the  on-screen image.
       hBmp = SelectObject(hDCMemory, hBmpPrev)
    
       ' If the screen has a palette get back the palette that was
       ' selected in previously.
       If HasPaletteScrn And (PaletteSizeScrn = 256) Then
          hPal = SelectPalette(hDCMemory, hPalPrev, 0)
       End If
    
       ' Release the device context resources back to the system.
       r = DeleteDC(hDCMemory)
       r = ReleaseDC(hWndSrc, hDCSrc)
    
       ' Call CreateBitmapPicture to create a picture object from the
       ' bitmap and palette handles. Then return the resulting picture
       ' object.
       Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
    End Function
    
    Public Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
      Dim r As Long
    
       Dim Pic As PicBmp
       ' IPicture requires a reference to "Standard OLE Types."
       Dim IPic As IPicture
       Dim IID_IDispatch As GUID
    
       ' Fill in with IDispatch Interface ID.
       With IID_IDispatch
          .Data1 = &H20400
          .Data4(0) = &HC0
          .Data4(7) = &H46
       End With
    
       ' Fill Pic with necessary parts.
       With Pic
          .Size = Len(Pic)          ' Length of structure.
          .Type = vbPicTypeBitmap   ' Type of Picture (bitmap).
          .hBmp = hBmp              ' Handle to bitmap.
          .hPal = hPal              ' Handle to palette (may be null).
       End With
    
       ' Create Picture object.
       r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
    
       ' Return the new Picture object.
       Set CreateBitmapPicture = IPic
    End Function
    Which does the job like I want it to, but it will also get a picture of anything thats over top of the form. So if you have something like My Computer open and its corner is over top of the form in my project, it will also have that corner in the picture. Im trying to make it only get a picture of my form and nothing else.

    Also would this be able to be done if the form is minimized to the tray? As in getting a picture of it if its inivisible at the time? Im making a dynamic image uploader like SigX and trying diffrent ways to get it to work. This is probably the third one Im working on now.

    Thanks in advance.
    Last edited by wiccaan; Apr 1st, 2005 at 07:53 PM.
    If my post was helpful please rate it

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get Picture Of Form

    You would have to set your form to be the topmost form so no other windows overlap it. You can use this
    code to do that. also, if the window is minimized then you cant get a screenshot.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, _
    4. ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    5.  
    6. Private Const SWP_NOMOVE = 2
    7. Private Const SWP_NOSIZE = 1
    8. Private Const SWP_SHOWWINDOW = &H40
    9. Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW
    10. Private Const HWND_TOPMOST = -1
    11. Private Const HWND_NOTOPMOST = -2
    12.  
    13. Public Function SetTopMost(hwnd As Long, bTopmost As Boolean) As Long
    14.     If bTopmost = True Then
    15.         SetTopMost = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
    16.     Else
    17.         SetTopMost = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS)
    18.     End If
    19. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

  4. #4

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475

    Re: Get Picture Of Form

    Hmm.. I didnt want to have to take this route on making it always open and such.

    I guess Ill have to find another way around this. Thanks though Rob.
    If my post was helpful please rate it

  5. #5

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475

    Re: Get Picture Of Form

    Thats not what Im trying to accomplish. If you ever used SigX it sends your computer info to their server, which then makes a automaticly generated picture of that information.

    Then it uploads that picture to their server which you can use on forums as a signiture.

    Pressing the print screen button would take a picture of the whole screen which is not what Im trying to accomplish. You should have read my first post before responding.

    Also Martin, thanks for the post, but that also does the same thing as mine did, it captures whats over the form as well. Thanks anyway. Im just dropping the project anyways lol. Im to lazy to figure this out. Thanks anyway though guys.
    If my post was helpful please rate it

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: [Project Dropped]Get Picture Of Form

    I have this, which copies forms. Not sure if it is what you're after.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width