Results 1 to 3 of 3

Thread: [RESOLVED] How do I get an image from a DC into a stdPicture object?

  1. #1

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Resolved [RESOLVED] How do I get an image from a DC into a stdPicture object?

    I have a memory DC that has an image in it and I'm trying to put that image into a stdPicture object. Here is an example that illustrates.
    VB Code:
    1. Dim mypic As StdPicture
    2. Picture1.AutoRedraw = True
    3. Picture2.AutoRedraw = True
    4.  
    5. 'load the picture into the stdPicture object
    6. Set mypic = LoadPicture("whatever.gif")
    7. 'display the image in a picturebox.  So far so good
    8. Picture1.PaintPicture mypic, 0, 0
    9.  
    10. 'create a memory device context
    11. Dim DC As Long
    12. DC = CreateCompatibleDC(Picture1.hDc)
    13. 'put the bitmap image from our stdPicture object into the DC
    14. Call SelectObject(DC, mypic.Handle)
    15.  
    16. 'Now it is ruined, picture2 won't display it properly
    17. 'no matter what I've tried
    18. Set Picture2.Picture = mypic
    19. Picture2.Refresh
    20.  
    21. 'tried this too
    22. 'Picture2.PaintPicture mypic, 0, 0
    Anyone have any ideas?

  2. #2

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: How do I get an image from a DC into a stdPicture object?

    I may have found an API function that will help
    OleCreatePictureIndirect
    http://msdn.microsoft.com/library/de...b1b8d760c4.asp

    I'll check it out

  3. #3

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: How do I get an image from a DC into a stdPicture object?

    Well, I think I have an answer.
    It's a bit more complex than I would like, but since I don't understand these things too much I haven't been able to simplify it.
    This code is from AllAPI .NET
    VB Code:
    1. Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
    2.     Dim R As Long, Pic As PicBmp, IPic As IPicture, IID_IDispatch As GUID
    3.  
    4.     'Fill GUID info
    5.     With IID_IDispatch
    6.         .Data1 = &H20400
    7.         .Data4(0) = &HC0
    8.         .Data4(7) = &H46
    9.     End With
    10.  
    11.     'Fill picture info
    12.     With Pic
    13.         .Size = Len(Pic) ' Length of structure
    14.         .Type = vbPicTypeBitmap ' Type of Picture (bitmap)
    15.         .hBmp = hBmp ' Handle to bitmap
    16.         .hPal = hPal ' Handle to palette (may be null)
    17.     End With
    18.  
    19.     'Create the picture
    20.     R = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
    21.  
    22.     'Return the new picture
    23.     Set CreateBitmapPicture = IPic
    24. End Function
    25.  
    26. Function hDCToPicture(ByVal hDCSrc As Long, ByVal LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As Picture
    27.     Dim hDCMemory As Long, hBmp As Long, hBmpPrev As Long, R As Long
    28.     Dim hPal As Long, hPalPrev As Long, RasterCapsScrn As Long, HasPaletteScrn As Long
    29.     Dim PaletteSizeScrn As Long, LogPal As LOGPALETTE
    30.  
    31.     'Create a compatible device context
    32.     hDCMemory = CreateCompatibleDC(hDCSrc)
    33.     'Create a compatible bitmap
    34.     hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
    35.     'Select the compatible bitmap into our compatible device context
    36.     hBmpPrev = SelectObject(hDCMemory, hBmp)
    37.  
    38.     'Raster capabilities?
    39.     RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
    40.     'Does our picture use a palette?
    41.     HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
    42.     'What's the size of that palette?
    43.     PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of
    44.  
    45.     If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    46.         'Set the palette version
    47.         LogPal.palVersion = &H300
    48.         'Number of palette entries
    49.         LogPal.palNumEntries = 256
    50.         'Retrieve the system palette entries
    51.         R = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
    52.         'Create the palette
    53.         hPal = CreatePalette(LogPal)
    54.         'Select the palette
    55.         hPalPrev = SelectPalette(hDCMemory, hPal, 0)
    56.         'Realize the palette
    57.         R = RealizePalette(hDCMemory)
    58.     End If
    59.  
    60.     'Copy the source image to our compatible device context
    61.     R = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
    62.  
    63.     'Restore the old bitmap
    64.     hBmp = SelectObject(hDCMemory, hBmpPrev)
    65.  
    66.     If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    67.         'Select the palette
    68.         hPal = SelectPalette(hDCMemory, hPalPrev, 0)
    69.     End If
    70.  
    71.     'Delete our memory DC
    72.     R = DeleteDC(hDCMemory)
    73.  
    74.     Set hDCToPicture = CreateBitmapPicture(hBmp, hPal)
    75. End Function

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