Results 1 to 9 of 9

Thread: Bitmaps, APIs, PictureBoxes = Frustrating

  1. #1
    Destined Soul
    Guest

    Bitmaps, APIs, PictureBoxes = Frustrating

    Hey all. I've been beating this thing over and over, and I still don't know what's occuring.

    I'm trying to get my function to load up an entire bitmap image within the space of the picturebox (ie: a thumbnail.) So far, I've been able to get the image into the box, but I can't figure a way around the color appearance. When I load a 24-bit bmp, it still displays it like a 8-bit! How do I fix this?

    Here's the code I'm using (at the moment):
    VB Code:
    1. Public Function DisplayBMP(picBox As PictureBox, fileName As String) As Boolean
    2.     Dim hDC As Long, hBMP As Long
    3.     Dim fileNo As Integer, counter As Integer
    4.     Dim imgSize As POINT
    5.            
    6.     DisplayBMP = False
    7.    
    8.     fileNo = FreeFile
    9.     Open fileName For Binary As fileNo
    10.         Get #fileNo, 19, imgSize
    11.     Close fileNo
    12.        
    13.     counter = 0
    14.     Do
    15.         hDC = CreateCompatibleDC(picBox.hDC)
    16.         counter = counter + 1
    17.     Loop While hDC < 1 Or counter < 15
    18.    
    19.     If hDC < 1 Then
    20.         MsgBox "Failed on Device Context creation"
    21.         Exit Function
    22.     End If
    23.    
    24.     hBMP = LoadImage(0, fileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE Or LR_DEFAULTSIZE)
    25.     If hBMP = 0 Then
    26.         DeleteDC hDC
    27.         MsgBox "Failed to load the image"
    28.         Exit Function
    29.     End If
    30.    
    31.     SelectObject hDC, hBMP
    32.     DeleteObject hBMP
    33.            
    34.     StretchBlt picBox.hDC, 0, 0, picBox.Width, picBox.Height, hDC, 0, 0, imgSize.X * 15, imgSize.Y * 15, vbSrcCopy
    35.    
    36.     picBox.Refresh
    37.  
    38.     DeleteDC hDC
    39.  
    40. End Sub
    Any help on how to fix my color problem, or on how to better implement this, is greatly appreciated.

    Destined

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Why don't you just load the image straight into an IPictureDisp and then use StretchBlt straight from there ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    If you want a simplier way, you can use an Image control, it has a property that automatically stretches the image to its own size.

    As to your code, everything seems to be fine with it, I have no idea why it shows up in 256 colors.
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    We are of course assuming you're using a resolution of greater than 256 colours in windows
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5
    Addicted Member Janus's Avatar
    Join Date
    Aug 2001
    Location
    California
    Posts
    221
    You need to use SetStretchBltMode and pass it COLORONCOLOR, I believe - otherwise it looks 256-color. You could also just use LoadImage, that function can do resizing at load.
    "1 4m 4 1337 #4xz0r!'
    Janus

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    As I said, he should just get the image into an IPictureDisp and Blit from there.
    Use a generateDC() function.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7
    Addicted Member Janus's Avatar
    Join Date
    Aug 2001
    Location
    California
    Posts
    221
    LoadImage uses bi-linear filtering
    "1 4m 4 1337 #4xz0r!'
    Janus

  8. #8
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Whatever

    If you use a GenerateDC() function though you end up with a reusable device context, and its very easy to use once you have a working GenerateDC()
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9
    Destined Soul
    Guest
    Thanx everyone for the help.

    plenderj
    I think I just want to use pure API calls to get it working. For what it's worth, that was how I originally did it, but the colors didn't seem to come out right. And, yes, I am running in more than 256 colors.

    Janus
    Excellent, this worked great. I just added the following code:
    VB Code:
    1. ...
    2.     DeleteObject hBMP
    3.    
    4.     SetStretchBltMode picBox.hDC, COLORONCOLOR  ' This works!
    5.     StretchBlt picBox.hDC, 0, 0, picBox.Width, picBox.Height, hDC, 0, 0, imgSize.x * 15, imgSize.y * 15, vbSrcCopy
    6.    
    7.     picBox.Refresh
    8.     ...

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