Results 1 to 5 of 5

Thread: replacing picturebox with device context

  1. #1

    Thread Starter
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    replacing picturebox with device context

    Hi, I'd like to be able to get rid of a picturebox I'm using on one of my forms.
    I know I'll have to use StretchBlt API to replace the paintpicture vb function in my code but how would I go about creating the device context to replace the picturebox itself and making all this work.
    I guess I should ask also... is loading an image to a device context much quicker than loading to a picturebox?

    Thanks in advance,

    what I have so far
    VB Code:
    1. Dim MyPic As StdPicture
    2. Set MyPic = LoadPicture(picturePath)
    3.           Call Picture1.PaintPicture(MyPic, 0, 0, Picture1.Width, Picture1.Height)
    4.           Set MyPic = Nothing
    5.           Image1.Picture = Picture1.Image
    6.           Set Picture1.Picture = Nothing
    Give your music collection a whole new life with PartyTime Jukebox

  2. #2
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    VB Code:
    1. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    2. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    3. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    4. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    5. 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
    6. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    7. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    8.  
    9. Private Sub Command1_Click()
    10.     Dim nDC As Long, nBitmap As Long
    11.    
    12.     'create the memory dc
    13.     nDC = CreateCompatibleDC(GetDC(0))
    14.     nBitmap = CreateCompatibleBitmap(GetDC(0), 100, 100)
    15.    
    16.     'select the bitmap into memory
    17.     SelectObject nDC, nBitmap
    18.    
    19.     'do stuff to it
    20.     BitBlt nDC, 0, 0, 100, 100, GetDC(0), 0, 0, vbSrcCopy
    21.     BitBlt Me.hdc, 0, 0, 100, 100, nDC, 0, 0, vbSrcCopy
    22.    
    23.     'and cleanup
    24.     DeleteObject nBitmap
    25.     DeleteDC nDC
    26. End Sub

  3. #3

    Thread Starter
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560
    Thanks BuggyProgrammer,

    In the end though I need to load that image to an image box.
    Like what I had
    Image1.Picture = Picture1.Image

    The reason I'm not loading directly to the image box is that in reality I may have as many as 500 image files to load into an array of imagebox (created at run time) and loading directly to them loads the entire image file content regardless if the imagebox is only 1 inch wide x 1 inch high! which is about the size of my imagebox controls.
    That is just a plain waste of memory.
    What I'm trying to achieve is to resize/scale down each image file prior to loading them onto the imagebox.

    Using the picturebox does the trick but I'd sure like to get rid of it if I can.
    Give your music collection a whole new life with PartyTime Jukebox

  4. #4

    Thread Starter
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Lightbulb

    Also I forgot to mention that these image files would not only be limited to bitmaps but could be jpg as well.
    Give your music collection a whole new life with PartyTime Jukebox

  5. #5
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    hmm.....

    you can change a hDC into a picture object:
    VB Code:
    1. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    2. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    3. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    4. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    5. 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
    6. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    7. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    8. Const RC_PALETTE As Long = &H100
    9. Const SIZEPALETTE As Long = 104
    10. Const RASTERCAPS As Long = 38
    11. Private Type PALETTEENTRY
    12.     peRed As Byte
    13.     peGreen As Byte
    14.     peBlue As Byte
    15.     peFlags As Byte
    16. End Type
    17. Private Type LOGPALETTE
    18.     palVersion As Integer
    19.     palNumEntries As Integer
    20.     palPalEntry(255) As PALETTEENTRY ' Enough for 256 colors
    21. End Type
    22. Private Type GUID
    23.     Data1 As Long
    24.     Data2 As Integer
    25.     Data3 As Integer
    26.     Data4(7) As Byte
    27. End Type
    28. Private Type PicBmp
    29.     Size As Long
    30.     Type As Long
    31.     hBmp As Long
    32.     hPal As Long
    33.     Reserved As Long
    34. End Type
    35. Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    36. Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal iCapabilitiy As Long) As Long
    37. Private Declare Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
    38. Private Declare Function CreatePalette Lib "gdi32" (lpLogPalette As LOGPALETTE) As Long
    39. Private Declare Function SelectPalette Lib "gdi32" (ByVal hdc As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
    40. Private Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long
    41.  
    42.  
    43.  
    44. Private Sub Command1_Click()
    45.     Dim nDC As Long, nBitmap As Long
    46.    
    47.     'create the memory dc
    48.     nDC = CreateCompatibleDC(GetDC(0))
    49.     nBitmap = CreateCompatibleBitmap(GetDC(0), 100, 100)
    50.    
    51.     'select the bitmap into memory
    52.     SelectObject nDC, nBitmap
    53.    
    54.     'do stuff to it
    55.     BitBlt nDC, 0, 0, 100, 100, GetDC(0), 0, 0, vbSrcCopy
    56.    
    57.     'set it as a picture
    58.     Me.Picture = hDCToPicture(nDC, 0, 0, 100, 100)
    59.    
    60.     'and cleanup
    61.     DeleteObject nBitmap
    62.     DeleteDC nDC
    63. End Sub
    64.  
    65.     'KPD-Team 1999
    66.     'URL: [url]http://www.allapi.net/[/url]
    67.     'E-Mail: [email][email protected][/email]
    68.     'Create a picture object from the screen
    69.    
    70. Function hDCToPicture(ByVal hDCSrc As Long, ByVal LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As Picture
    71.     Dim hDCMemory As Long, hBmp As Long, hBmpPrev As Long, R As Long
    72.     Dim hPal As Long, hPalPrev As Long, RasterCapsScrn As Long, HasPaletteScrn As Long
    73.     Dim PaletteSizeScrn As Long, LogPal As LOGPALETTE
    74.  
    75.     'Create a compatible device context
    76.     hDCMemory = CreateCompatibleDC(hDCSrc)
    77.     'Create a compatible bitmap
    78.     hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
    79.     'Select the compatible bitmap into our compatible device context
    80.     hBmpPrev = SelectObject(hDCMemory, hBmp)
    81.  
    82.     'Raster capabilities?
    83.     RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
    84.     'Does our picture use a palette?
    85.     HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
    86.     'What's the size of that palette?
    87.     PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of
    88.  
    89.     If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    90.         'Set the palette version
    91.         LogPal.palVersion = &H300
    92.         'Number of palette entries
    93.         LogPal.palNumEntries = 256
    94.         'Retrieve the system palette entries
    95.         R = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
    96.         'Create the palette
    97.         hPal = CreatePalette(LogPal)
    98.         'Select the palette
    99.         hPalPrev = SelectPalette(hDCMemory, hPal, 0)
    100.         'Realize the palette
    101.         R = RealizePalette(hDCMemory)
    102.     End If
    103.  
    104.     'Copy the source image to our compatible device context
    105.     R = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
    106.  
    107.     'Restore the old bitmap
    108.     hBmp = SelectObject(hDCMemory, hBmpPrev)
    109.  
    110.     If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    111.         'Select the palette
    112.         hPal = SelectPalette(hDCMemory, hPalPrev, 0)
    113.     End If
    114.  
    115.     'Delete our memory DC
    116.     R = DeleteDC(hDCMemory)
    117.  
    118.     Set hDCToPicture = CreateBitmapPicture(hBmp, hPal)
    119. End Function
    120. Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
    121.     Dim R As Long, Pic As PicBmp, IPic As IPicture, IID_IDispatch As GUID
    122.  
    123.     'Fill GUID info
    124.     With IID_IDispatch
    125.         .Data1 = &H20400
    126.         .Data4(0) = &HC0
    127.         .Data4(7) = &H46
    128.     End With
    129.  
    130.     'Fill picture info
    131.     With Pic
    132.         .Size = Len(Pic) ' Length of structure
    133.         .Type = vbPicTypeBitmap ' Type of Picture (bitmap)
    134.         .hBmp = hBmp ' Handle to bitmap
    135.         .hPal = hPal ' Handle to palette (may be null)
    136.     End With
    137.  
    138.     'Create the picture
    139.     R = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
    140.  
    141.     'Return the new picture
    142.     Set CreateBitmapPicture = IPic
    143. End Function

    most of the code was from the apiguide

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