Basic Bitmap Questions

I'm trying to learn how to program bitmaps.
using Petzold and VC5 documentation.
I am having lots of trouble understanding things. I hope someone can help.
Here are a few of the questions I have:

1) The HBITMAP type.
Is it a handle to a DIBitmap, a GDI bitmap, neither, both?

2) LoadBitmap().
Does it create, in memory, a DIBitmap or a GDI bitmap?
Petzold hints that it's a DIB, but on p.201 he uses it with
CreatePatternBrush() rather than CreateDIBPatternBrush().
Also, the M$ doc says that the bitmap returned, must be
deleted with DeleteObject(), which is only true for GDI objects.

If it's a GDI bitmap, then how so you load a bitmap resource
into what Petzold calls "Packed DIB Format"?

3 )GetDIBits()
Petzold says this gets bits from a DIB, and to use GetBitmapBits()
for getting bits from a GDI bitmap.
The M$ doc says GetBitmapBits() is obsolete, use GetDIBits().
Can you get bits from a GDI bitmap using a funtion for a DIBitmap?

More confusing is that GetDIBits() uses HBITMAP for its bitmap,
while functions like CreateDIBPatternBrush(), which handle DIBs,
uses HGLOBAL for their bitmap.

4) Petzold says "Packed DIB Format" (and I quote) "contains
everything in the DIB file except the BITMAPFILEHEADER structure".
But the M$ doc says, in its doc for CreateDIBPatternBrush(),
      Identifies a global memory object containing a packed DIB,
      which consists of a BITMAPINFO structure immediately followed
      by an array of bytes defining the pixels of the bitmap. "
which leaves out the color table and BITMAPV4HEADER.

5) Some M$ functions refer to DIBSection.   What's that?
It looks a lot like what Petzold calls a "Packed DIB Format"

I have more questions, but the answers to these might resolve them.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tommy HuiEngineerCommented:
1. HBITMAP is a handle to a bitmap. It is usually device dependent.

2. LoadBitmap will return to you an HBITMAP. The LoadBitmap internally converts your DIB into a DDB.

3. You have to realize that Petzold wrote his book during 16-bit programming and Microsoft now wants to you program in 32-bits. A device dependent bitmap (DDB) was the way to do things in 16-bits because they are much faster than DIB. A DIB is usually just some memory that is formatted in a certain way.

4. BITMAPV4HEADER is only part of the latest and greatest bitmaps which no one to my knowledge is using yet. Everybody still uses only DDBs and DIBs.

5. A DIBSection is something that can be treated as a DDB (hence it is fast) and it is device independent. This is only available in the new 32-bit world.

kumquatAuthor Commented:
I have to find Thui's answer unsatisfactory, as I hardly know more now then when I asked
the questions.

I guess I should have mentioned that I am working under W95, and am using Petzold's
"Programming Windows 95" book.  Maybe then Thui could have answered the questions better.

1) The HBITMAP type.
    Is it a handle to a DIBitmap, a GDI bitmap, neither, both?

      Thui answers that it is a handle to a bitmap, but, as I indicate, it's my
      understanding that there are 2 kinds of bitmaps.  Which one is it a handle to?
      I can guess that Thui means to answer "both", but I'm not sure.

2) LoadBitmap().
    Does it create, in memory, a DIBitmap or a GDI bitmap?

      Thui's answer is:
      LoadBitmap will return to you an HBITMAP.
      The LoadBitmap internally converts your DIB into a DDB.

      It sounds like Thui is saying that LoadBitmap() reads a DIBitmap
      from a resource, and converts it to a DDB (i.e. a GDI bitmap), and
      then returns a handle to the DDB.

      If this is what he means, it answers my questions well, if rather tersely.
      It explains the M$ doc., which says the bitmap must be deleted, which
      is true for DDBs.
      It also could explain what Petzold wrote as really sloppy writing.
      Perhaps when Petzold writes that "you can read a DIB file directly
      into a block of allocated memory", he means with fscanf(), and then
      doesn't bother to tell you that LoadBitmap() loads and converts it
      to a DDB.

      Would Thui either confirm or correct my thinking?

3 )GetDIBits()

      Thui doesn't address my confusion, rather, he makes it worse.
      In his answer, is he implying that using DDBs is old-fashioned for W95,
      and one should only use DIBs?  If so, this contradicts the LoadBitmap()
      function, which returns a DDB.

      Let me re-phrase what I said.
      Does the function get bits from a DIB, a BBD, or both?
      If from a DDB, why the contradictory name?
      There are lots of functions with "DIB" in their names. How does one tell
      what kind of bitmaps they refer to?

      Also, is there any sense to M$ refering to the bitmap in
      CreateDIBPatternBrush() as HGLOBAL, instead of HBITMAP?

4) "Packed DIB Format"

      Thui doesn't address what happened to the color table.
      Also, based on what I guess Thui is saying in answer (2), I don't see
      where this format fits into things.  Why would anyone use it if you
      read a resource directly into a DDB format?  But yet Thui says DDB format
      is just a carryover from the 16-bit days.  So should a good programmer
      convert the DDB to this format somehow, and use it instead of the DDB

      I just don't get it.  I'm as much in the dark about what's going on
      as before.

5) Some M$ functions refer to DIBSection.   What's that?

      Thui says: A DIBSection is something that can be treated as a DDB (hence
      it is fast) and it is device independent.

      But this doesn't explain much to me.  Why would there be such a thing?
      Just to get away from using the old-fashioned DDB format?
      In that case, why wouldn't LoadBitmap() convert a DIB to a DIBSection
      instead of a DDB?

This still just isn't making any sense to me!

Perhaps someone could refer me to a book or site which explains bitmaps well,

I will change my rating of Thui's answer to "average" if he will clarify the
first three answers, and to "excellent" if he can refer me to a book or site
which addresses all this stuff well.


A DIB bitmap is only converted to DDB when it's selected into a device context -not when you load it. This is the magic of a DIB. You can select the same bitmap into incompatible contexts such as printer and screen without having to do any conversions.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Another note: a bitmap is a GDI object. Just like a brush and a pen, only one bitmap can be selected into any one device context. That's why SelectObject/DeleteObject funcs are related to bitmaps as well.
kumquatAuthor Commented:
The reply not only didn't address my questions, problems and concerns,
but it's confusing and seems to contradict the bitmap code published in Petzold's book

I don't know what to think.

I would like to reopen the question to other experts.

Hi... I hope that this help you....

1.HBITMAP is pointer to BITMAPINFO struct. There are size, bpp, ans some next, but be carefully with this. It may be handle to block of memory.

2.LoadBitmap create in memory:


ad a:this is API defined struct
   b:color info is 256 pallete entries
     !there're ONLY ColorImportant entries from BITMAPINFOSTRUCT
   c:bitmap data, pixel prer pixel, from bottom of the
     screen (original file)
3. GetDIBits lets you acces to bitmap pixel data, which are dependent on BPP in BITMAPINFOSTRUCT

So, if you wont to use bitmaps, load them throw load bitmap, pass pointer to them. There are BITMAP...COLORS...DATA. I'm using bitmaps in all my applications (such as Pulse), so, if you have any problem, tell me. But i'm using bitmaps with own routines, because WINDOWS sometimes ****S pallete (especially if you have 16 BPP bitmap and 24 bpp resolution....). Is verry simple to work with them (easily and much faster then throw system I thing). I'm only using BeginPaint,MemoryDC and BITBLT....

Seeeaa ya and if you'll reject this, sorry.... :-)

ps:sorry for bad english....

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.