Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Basic Bitmap Questions

Posted on 1998-03-06
Medium Priority
Last Modified: 2013-12-03
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.

Question by:kumquat
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Expert Comment

by:Tommy Hui
ID: 1411783
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.


Author Comment

ID: 1411784
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.



Expert Comment

ID: 1411785
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.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 1411786
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.

Author Comment

ID: 1411787
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.


Accepted Solution

rayofunreal earned 100 total points
ID: 1411788
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....

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (, we saw how to put the Delete button back there where it belongs.  "Delete" is …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question