Thread: About windows bitmaps

  1. #1
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158

    About windows bitmaps

    I have two questions:

    • If I give the CreateBitmap() function a pointer to the pixel data I read from some file, will this data be copied? In other words, can I deallocate the pixel data after I created a bitmap?
    • I am loading a TGA file, type 2, 32 bpp. Now I have the pixel data but what's the difference between using CreateDIBSection() to make it into a windows bitmap, or CreateBitmap()?
      Or should I maybe use CreateCompatibleBitmap? But then how do I set the bpp to 32?
      I don't know which function to use. CreateBitmap() works but I don't know if it's the best way.


    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Win32 API specific questions on the Windows board please - moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    http://msdn.microsoft.com/library/de...tmaps_3ak0.asp
    http://msdn.microsoft.com/library/en...asp?frame=true
    http://msdn.microsoft.com/library/en...asp?frame=true

    1) There seems to be absolutely no information available on whether CreateBitmap stores its own information. Simply guessing from the way CreateBitmap, CreateBitmapIndirect and the BITMAP struct work, I would say it is not safe to delete the data.
    (However, there's a rather easy way to test the behaviour: create an image and afterwards change the data. See if the image changes, too. Remember that relying on undocumented behaviour is a bad thing. Better safe than sorry - keep the image data around.)
    On the upside, the docs recommend that you don't use CreateBitmap anyway. Instead, either use CreateDIBitmap (pass NULL as the hSection parameter and the system will allocate memory you can copy into), or you can use CreateCompatibleBitmap and use SetDIBits to fill the bitmap with data.

    2) Hard to explain. There's a reason Charles Petzold dedicates about 150 pages in "Programming Windows" to bitmaps in their various forms.

    However, note this: the thing about CreateCompatibleBitmap is that it creates a bitmap that is compatible to the DC you give it - typically, a DC for the screen and thus bound to the graphics settings of your system. There is not way to make CCB create anything else - it wouldn't be compatible anymore. That's why you use CCB (which, by the way, allocates memory) and then SetDIBits to translate the custom pixel format you have to the device format.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    The data is copied. I did the following:

    Code:
            BITMAPINFO bmi = 
            {
                {
                    sizeof (BITMAPINFOHEADER),
                    this->nWidth, this->nHeight,
                    1, 32,
                }
            };    
            this->hBmp = CreateDIBitmap(hDC, &bmi.bmiHeader, CBM_INIT, pPixelData, 
                                        &bmi, DIB_RGB_COLORS);
            if(!hBmp)
                MessageBox(NULL, "CreateDIBitmap failed!", NULL, 0);
                
            memset(pPixelData, 128, this->nWidth*this->nHeight*4);
            delete pPixelData;
    And the image displayed well - except that it has been flipped vertically, thanks to CreateDIBitmap. How do I get around this?

  5. #5
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158

    How to flip the bitmap in memory?

    As I said the bitmap created by CreateDIBitmap isfor some reason bottom-up, and therefore drawn bottom-up. What's the solution to this?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Give it a negative height?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Flip it using StretchBlt?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Maybe I could use CreateCompatibleBitmap to create another bitmap, draw the bitmap created by CreateDIBitmap flipped vertically on the new one, and return the new bitmap handle.
    Thanks for the tip.
    The problem is (I think) that CreateDIBitmap assumes that I've loaded a bottom-up DIB and want to create a DDB. But a TGA file is not bottom-up. Maybe there is some way to tell CreateDIBitmap whether the data is top-down or not?

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    From http://msdn.microsoft.com/library/de...tmaps_5d4g.asp

    Note that a positive value for the height indicates a bottom-up DIB while a negative value for the height indicates a top-down DIB.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Also, some functions will fail if the height is set to a negative value so be careful with that. I just thought I'd point this out.

  11. #11
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158

    Smile

    Note that a positive value for the height indicates a bottom-up DIB while a negative value for the height indicates a top-down DIB.
    Oh, thanks. Guess that's the solution

  12. #12
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Yep, it works. Much simpler than I thought lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  2. Windows Rant followed by installation question
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 06-21-2003, 04:42 PM
  3. Codec Bitrates?
    By gvector1 in forum C# Programming
    Replies: 2
    Last Post: 06-16-2003, 08:39 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. How come this only works in Windows nt/2000?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 08-30-2002, 06:54 PM