Assuming I have a TImage component placed on a form along with a TButton. When I click the button I run the code that follows. Ignoring the palette (I know how to create a grayscale palette and assign it) the following code works:
BYTE bBits[] = { 0x00, 0x22, 0x44, 0x88, 0x00, 0x22, 0x44, 0x88,
0x22, 0x44, 0x88, 0x00, 0x22, 0x44, 0x88, 0x00,
0x44, 0x88, 0x00, 0x22, 0x44, 0x88, 0x00, 0x22,
0x88, 0x00, 0x22, 0x44, 0x88, 0x00, 0x22, 0x44 };
Image1->Picture->Bitmap->Width=32;
Image1->Picture->Bitmap->Height=32;
Image1->Picture->Bitmap->PixelFormat = pf8bit;
SetBitmapBits(Image1->Picture->Bitmap->Handle,32*32,&bBits);
Now if I try this:
BYTE bBits[] = { 0x00, 0x22, 0x44, 0x88, 0x00, 0x22, 0x44, 0x88,
0x22, 0x44, 0x88, 0x00, 0x22, 0x44, 0x88, 0x00,
0x44, 0x88, 0x00, 0x22, 0x44, 0x88, 0x00, 0x22,
0x88, 0x00, 0x22, 0x44, 0x88, 0x00, 0x22, 0x44 };
HBITMAP HBmp = CreateBitmap(32, 32, 1, 8, bBits);
Image1->Picture->Bitmap->Handle=HBmp;
It does not work - it displays nothing. But if I change the CreateBitmap code to:
HBITMAP HBmp = CreateBitmap(32, 32, 1, 8, bBits);
it displays however a monochrome image which is incorrect anyway.
The real "weirdness" though is this:
BYTE bBits[] = { 0x00, 0x22, 0x44, 0x88, 0x00, 0x22, 0x44, 0x88,
0x22, 0x44, 0x88, 0x00, 0x22, 0x44, 0x88, 0x00,
0x44, 0x88, 0x00, 0x22, 0x44, 0x88, 0x00, 0x22,
0x88, 0x00, 0x22, 0x44, 0x88, 0x00, 0x22, 0x44 };
Image1->Picture->Bitmap->Width=32;
Image1->Picture->Bitmap->Height=32;
Image1->Picture->Bitmap->PixelFormat = pf8bit;
HBITMAP HBmp = CreateBitmap(32, 32, 1, 8, bBits);
Image1->Picture->Bitmap->Handle=HBmp;
Again nothing displays but if I click the button twice, I get an out of resources exception so it appears there is a memory leak.
Finally if I change the order:
HBITMAP HBmp = CreateBitmap(32, 32, 1, 8, bBits);
Image1->Picture->Bitmap->Handle=HBmp;
Image1->Picture->Bitmap->Width=32;
Image1->Picture->Bitmap->Height=32;
Image1->Picture->Bitmap->PixelFormat = pf8bit;
results in an instant exception (i.e. only need click the button once).
Note this memory leak has nothing to do with the original bitmap handle of the TImage i.e. Image1->Picture->Bitmap->Handle is NULL to begin with so there is no exisitng HBITMAP to release and DeleteObject etc. I am assuming when HBITMAP is assigned to TImage1's handle that TImage takes ownership.
Any explanation? Thank you in advance.
P.S: For reference sake this is how I did it with SetBitmapBits which worked:
BYTE bBits[] = { 128, 128, 128, 128, 0x00, 0x22, 0x44, 0x88,
0x22, 0x44, 0x88, 0x00, 0x22, 0x44, 0x88, 0x00,
0x44, 0x88, 0x00, 0x22, 0x44, 0x88, 0x00, 0x22,
0x88, 0x00, 0x22, 0x44, 0x88, 0x00, 0x22, 0x44 };
TMaxLogPalette lp;
lp.palVersion = 0x0300;
lp.palNumEntries = 256;
for (int i = 0; i < 256; i++) {
lp.palPalEntry[i].peRed = i;
lp.palPalEntry[i].peGreen = i;
lp.palPalEntry[i].peBlue = i;
lp.palPalEntry[i].peFlags = PC_RESERVED;
}
HPALETTE pal= CreatePalette((LOGPALETTE*)&lp);
Image1->Picture->Bitmap->Width=32;
Image1->Picture->Bitmap->Height=32;
Image1->Picture->Bitmap->PixelFormat = pf8bit;
Image1->Picture->Bitmap->Palette = pal;
SetBitmapBits(Image1->Picture->Bitmap->Handle,32*32,&bBits);