DelphiFAQ Home Search:

Load a bitmap and palette from a resource file

 

comments1 comments. Current rating: 5 stars (1 votes). Leave comments and/ or rate it.

This routine loads a bitmap from a resource file and gets its palette and bitmap handle:

procedure LoadBitmapPalette(szTitle : PChar; var hPal : HPalette;
                            var hBitmap : HBitmap);
type
  TMLogPalette = record
		   palVersion: Word;
		   palNumEntries: Word;
		   palPalEntry: array[0..255] of TPaletteEntry;
		 end;
var
  hOldPal : HPALETTE;
  pPal	  : TMLogPalette;
  dc	  : hDC;   { device context for palette }
  hRes	  : THandle; { resource handle for bitmap }
  pBits   : PBITMAPINFOHEADER; { pointer to bitmapinfoheader
                                 in resource }
  pRgb	  : ^TRGBQUAD;	{ Zeiger auf DIB-Palettendaten }
  i       : integer;
  Data	  : PChar;
begin
  hRes := FindResource(hInstance, szTitle, RT_BITMAP);
  if hRes <> 0 then
    hRes := LoadResource(hInstance, hRes);

  if hRes <> 0 then
  begin
    pBits := PBITMAPINFOHEADER (LockResource(hRes));

    { so: having 16 colors, we do not need a palette
      (LoadBitmap is allright)
      > 256 colors: no palette is needed ("hi-/direct/true color")  }

    if (pBits^.biBitCount <= 8) and { only for <= 256 color bitmaps }
       (pBits^.biSize =
	   sizeof(TBITMAPINFOHEADER)) { only Windows-bitmaps, not OS/2 }
      then
    begin
      pRgb := pointer (pBits);
      inc (PChar(pRgb), pBits^.biSize);

      pPal.palNumEntries := 1 shl pBits^.biBitCount;
      pPal.palVersion	 := $300;

      for i := 0 to pPal.palNumEntries-1 do
      begin
        pPal.palPalEntry[i].peRed   := pRgb^.rgbRed;
        pPal.palPalEntry[i].peGreen := pRgb^.rgbGreen;
        pPal.palPalEntry[i].peBlue  := pRgb^.rgbBlue;
        pPal.palPalEntry[i].peFlags := 0{PC_NOCOLLAPSE};
        inc (PChar(pRgb), 4);
      end;

      hPal := CreatePalette(PLogPalette(@pPal)^);

      DC := GetDC(0);

      hOldPal := SelectPalette(DC, hPal, false);
      RealizePalette(DC);

      with pBits^ do
      begin
        biClrImportant := 0;
        biClrUsed      := 0;
      end;

      Data := pointer(pBits);
      inc (Data, pBits^.biSize + pPal.palNumEntries * sizeof(TRGBQUAD));
      hBitmap := CreateDIBitmap(DC, pBits^, CBM_INIT,
                                Data, PBitmapInfo(pBits)^,
                                dib_RGB_Colors);

      SelectPalette(DC,hOldPal,FALSE);

      ReleaseDC(0, DC);
    end;

    UnlockResource(hRes);
  end
  else
    hBitmap := LoadBitmap(hInstance, szTitle);
end;
You don't like the formatting? Check out SourceCoder then!

Comments:

2007-04-02, 20:41:23
anonymous  
rating

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.