<snip>Quote:> I need a guru to help me understand why an image im creating in memory
> (loaded from a custom image format into bitmapinfo structures) is
> with incorrect colors. Please dont just post a chunk of code with other
> ways to display an image. I need help learning and understanding WHY
> im doing is wrong. I've been trying for a week now, probably 100 times
> (seriously) various incantations to get this to work. Obviously Im not
> understanding something, so that is why I don't just want another piece of
> code to compare to.
> I am trying to display a custom image file format. My code can correctly
> convert the custom format to a bitmap file. I don't wish to create a file
> though (was merely for testing), but instead wish to display the image
> memory into a Picturebox, or any other control.
> The custom format is :
> A palette file made up of sets of 3 bytes each, RGB order, 256 colors in
> all. I am loading this into BITMAPINFO bmiColors().
> The raw image byte data, where each byte contains the index of a palette
> entry. The data is in top to bottom format, so in loading I am reversing
> to the bitmap standard of bottom to top, into a byte array.
> I can't get the image to display with the correct colors (for example what
> should be the white background gets displayed as a mint green)
> I've included two different versions of DisplayBitmapinfo, they both yeild
> the same result. I can break inside either function, and check my
> structures, all have the correct data. Screenshot attached.
> If you see a problem with this code, please... help me figure it out.
First up, you've got a possible resource leak in both methods with this kind
hMemDC = CreateCompatibleDC(pb.hDC)
hBitmap = CreateCompatibleBitmap(pb.hDC, width, height)
hOldBitmap = SelectObject(hMemDC, hBitmap)
SetDIBits hMemDC, hBitmap, 0, height, bBits(0), lpbi, DIB_RGB_COLORS
If hMemDC <> 0 And hBitmap <> 0 Then
BitBlt pb.hDC, 0, 0, width, height, hMemDC, 0, 0, SRCCOPY
RC = SelectObject(hMemDC, hOldBitmap)
RC = DeleteObject(hBitmap)
RC = DeleteDC(hMemDC)
The problem here is that only if _everything_ succeeds will _anything_ be
destroyed or de-selected - You could easily have the DC and Bitmap be
created properly then the selection fails for instance - In this case both
the DC and Bitmap would be left in memory. Instead, use something like
DC = CreateDC(...)
If (DC) Then
BMP = CreateBMP(...)
If (BMP) Then
Old = Select(DC, BMP)
If (Old) Then
' Good to go
You're also not checking the return of SetDIBits() which could be failing.
Unfortunately you've not given us enough information about what could be
going wrong (I suspect it's in your declarations or how you're setting up
the palette and/or bitmap data arrays - neither of which we can see..)
I'd suggest generating a DIBSection rather than a DDB though since this will
be make the resulting image independent of that users hardware which could
be causing a role in the colour degradation you're experiencing. Also, make
sure that your palette array has been declared and passed properly and
you're not trying to pass in the array header rather than a pointer to the
first entry (A reasonably common mistake.)
If you still get no luck, then post some more code, specifically the
declares you're using and the way you're creating (And filling?) the data
and palette arrays.
If you _do_ decide you want an example of how to create and assign a bitmap
with palette then here's an example:
Here's another, it's in C++ but the steps are similar:
Hope this helps,
- Microsoft Visual Basic MVP -