How to force a bitmap to be 8 bit

My program makes bitmaps which use a custom 256 color palette. I use the bitmap routines in Delphi for creating and saving the bitmaps. When Windows is set up in 24 bit color mode, the bitmaps are saved as 24 bits, making them 3 times bigger than necessary. How can force them to be 8 bit?
Who is Participating?
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.

What I did, is open a truely 256 color bitmap(check an example that comes with Delphi), and on it you draw yours, so it's palete is set to 256 colors.
I'm sure you can also play with the Palete property, but I had no patience to do it... Anyway, you can check the sources.

Good luck!

If you are using Delphi 3, then you can use the PixelFormat property of a bitmap:
  MyBitmap.PixelFormat := pf8Bit;

maybe interesting too, if you use D3 :
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

toreotAuthor Commented:
Matvey: By opening, do you mean loading a bitmap file? That is not useful in my case. I can't find the example you are refering to. Do you have the file name?

JimBob: I have bought D3, but not used it yet. Perhaps it is as easy as using the PixelFormat property. I will try as soon as possible. However, my program also ought to run under Win 3.11 so I would appreciate if anyone could tell me how to do it in D1. I have a feeling that it is necessary to use Windows API.

ZifNab: An interesting item, but I can't see how it might apply to my problem.
Well,you can find some files in the images\splash or smth like it.

Do you have D1? I think it also has some pictures...
Anyway, if you want, I can send some to email:
toreotAuthor Commented:
I misunderstood you. I thought you meant a coding example. But my program needs many bitmaps. You suggest I load a 256 color bitmap, erase its contents, change its palette, width and height and then draw on it each time I need a bitmap? It might work, but seems rather inelegant.

The cause and solution is as follows:

The Delphi, when creating a new bitmap, creates a COMPATIBLE bitmap image with the Screen (i.e. desktop image). Since Desktop in 24 bit is a 24 bit image, delphi auto create a 24 bit image. So we need to alter this after creating the TBitmap or program our TBitmapX which dono create compatible bitmap with screen.

Since, we cannot change the VCL, we should convert the bitmap to 8 bits uppon writting ONLY IF it is a 24 bit bitmap due to the screen. To overcome with minimum trouble we should guarantee that the actual colors in the image are < 256. Upon saving the image we do the following:

1 - We prepare bitmap file header by computing the BITMAPINFOHEADER with pallette table + Bitmap size
2 - We prepare the BITMAPINFOHEADER such that biBitCount = 8 even if the image is 24 bit
3 - We prepare a color pallete(or may be you have already one).
4 - We convert all pixels RGB values(24 bit images have R G B components for each pixel as you know) to pallete indexes and prepare a 8 bit image buffer.

Before doing so, what do you think about it?
(I am quite sure that, there is no other way if we want to use TBitmap, but there exists if we write our own TBitmap class.)
Smile, not that hard!
Look at GenPal v1.00 (freeware) :

you can use this source code to change to get it like you want.

And something very interesting, but not for 24 to 8 bit :

(also links to a very good info site, with ISDN's about 24 to 8 bits)

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
toreotAuthor Commented:
Sorry for the delay. I will award Inter the points if he can supply more details.
  The following may complicate things: My bitmaps are filled with a mathematical pattern which is copied from a form canvas using CopyRect. The canvas is 24 bits, but uses a 256 color palette. Some trials with PixelFormat pf8Bit in D3 suggests that the copying mangles the bitmap palette. Any comments on this?
Sorry I was out,

1 - Does speed important for you?
2 - In any case what are the dimensions of the region you are copying?

toreotAuthor Commented:
No, speed is not important. The dimension of the region varies, it may be the whole screen.
 I restate my problem: I draw a 256 color picture on an MDI form using my own palette. I wish to save this pattern as a 8-bit bitmap, regardless of the Windows color depth, without destroying the palette. I will be satisfied with a Delphi 3 solution, but it would be nice if it would work in D1 too.
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

From novice to tech pro — start learning today.