Solved

Basic Bitmap Questions

Posted on 1998-03-06
6
516 Views
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(),
      "hglbDIBPacked
      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.

Alan
0
Comment
Question by:kumquat
6 Comments
 
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.

0
 

Author Comment

by:kumquat
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
      format?

      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.

Alan


0
 

Expert Comment

by:RLM
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:RLM
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.
0
 

Author Comment

by:kumquat
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
and www.codeguru.com.

I don't know what to think.

I would like to reopen the question to other experts.

Kumquat
0
 
LVL 1

Accepted Solution

by:
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:

 A. BITMAPINFO STRUCT
 B. COLORINFO
 C. PIXELDATA

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....
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now