很难吗?看看葵花宝典怎么写的
procedure TForm1.Button1Click(Sender: TObject);
var
lpPalette : PLogPalette;
hPal : hPalette;
i : integer;
begin
{Allocate the memory used by the palette}
GetMem(lpPalette,
sizeof(TLogPalette) + (255 * sizeof(TPaletteEntry)));
{Fill out the palette header}
lpPalette^.palVersion := $300;
lpPalette^.palNumEntries := 256;
{Turn range checking off if it is on and}
{remember the range checking state}
{$IFOPT R+}
{$DEFINE CKRANGE}
{$R-}
{$ENDIF}
{Fill in the palette structure color table with shades of gray}
for i := 0 to 255 do begin
lpPalette^.PalPalEntry.peRed := i;
lpPalette^.PalPalEntry.peGreen := i;
lpPalette^.PalPalEntry.peBlue := i;
end;
{Turn range checking back on if it was on when we started}
{$IFDEF CKRANGE}
{$UNDEF CKRANGE}
{$R+}
{$ENDIF}
{Create a palette handle}
hPal := CreatePalette(lpPalette^);
{Free the memory use by the palette structure}
FreeMem(lpPalette,
sizeof(TLogPalette) + (255 * sizeof(TPaletteEntry)));
{Do something with the palette here}
{Delete the palette handle after use}
DeleteObject(hPal);
end;
var
Bitmap: TBitmap;
begin
Bitmap:=TBitmap.Create;
Bitmap.LoadfromFile({'Whatever.bmp'});
With Image2.Picture.bitmap do
Begin
Width:=Bitmap.Width;
height:=Bitmap.Height;
Palette:=Bitmap.Palette;
Canvas.draw(0,0,bitmap);
Refresh;
end;
end;
如果要往窗体上画
Canvas.Draw(0,0,Bitmap);
SelectPalette(Form1.Canvas.handle,Bitmap.Palette,True);
RealizePalette(Form1.Canvas.Handle);