CreatePaletteFromBitmap老是有问题。(100分)

  • 主题发起人 主题发起人 江南草
  • 开始时间 开始时间

江南草

Unregistered / Unconfirmed
GUEST, unregistred user!
在改写directx8sdk的windowed mode这个例子时,需要从一个bitmap创建一个palette,本想简单的问题,但是在setpalette时老出错:Invalid pixel format as specified.
我试了好几种方法,其中之一如下:
lpDD : IDirectDraw7;
lpSF : IDirectDrawSurface7;
lpPal : IDirectDrawPalette;
dret : HResult;
bmp : TBitMap;
hbmp : HBitmap;
palsize : integer;
pal : TMaxLogPalette;
begin
lpPal := nil;
bmp := TBitmap.create;
bmp.LoadFromResourceName(0,'directx');
hbmp := bmp.Handle;
if GetObject(bmp.palette,sizeof(palsize),@palsize) = 0 then
begin messagebox(0,'Get Bmp Pal size error','Information',mb_ok+mb_iconinformation);
bmp.free; exit;
end;
with pal do
begin
palNumEntries := palsize;
palVersion := $300;
GetPaletteEntries(bmp.Palette,0,palsize,palPalEntry);
end;
ByteSwapColors(pal.palPalEntry,pal.palNumEntries);
dret := lpDD.CreatePalette(DDCAPS_8BIT,@pal.palPalEntry,lpPla,nil);
//Here,dret = DD_OK
dret := lpSF.SetPalette(lpPal);
//这里出错。"The pixel format was invalid as specified."
是不是窗口模式下的palette有特殊要求?
 
建议你看一下delphi安装目录里的例子
.../Borland/Delphi6/Help/Examples/Bitmap
 
function LoadPaletteFromBitmap(AFile: string): HPalette;
var
AStream: TFileStream;
BmpHeader: TBitmapFileHeader; //文件头;
BmpInfo: TBitmapInfoHeader; //信息头;
BmpColor: array[0..255] of TRGBQuad; //颜色表;
APal: PLogPalette;
I: Integer;
begin
Result := 0;
if (Trim(AFile) = '') or not FileExists(AFile) then Exit;
AStream := TFileStream.Create(AFile, fmOpenRead);
try
AStream.Seek(0, soFromBeginning);
AStream.Read(BmpHeader, SizeOf(BmpHeader));
if BmpHeader.bfType <> $4D42 then Exit;
AStream.Read(BmpInfo, SizeOf(BmpInfo));
if not (BmpInfo.biBitCount in [1, 4, 8]) then Exit;
AStream.Read(BmpColor, SizeOf(BmpColor));
ShowMessage(IntToStr(SizeOf(BmpColor)));
finally
AStream.Free;
end;
// GetPaletteEntries

//创建逻辑调色板;
GetMem(APal, 4 + 256 * SizeOf(TRGBQuad));
try
APal.palVersion:=$300;
APal.palNumEntries:=256;
for I := 0 to 255 do
begin
APal.palPalEntry.peRed := BmpColor.rgbRed;
APal.palPalEntry.peGreen := BmpColor.rgbGreen;
APal.palPalEntry.peBlue := BmpColor.rgbBlue;
APal.palPalEntry.peFlags := BmpColor.rgbReserved;
end;
Result := CreatePalette(APal^);
finally
FreeMem(APal);
end;
end;
 
to thx1180:
创建总是返回dd_ok,但IDirectDrawSurface.SetPalette(lpPal)出错。
 
我这几天也被这个BMP搞的头晕,也查了一些关于BMP格式的资料和这里的老帖子,对于你的Directx问题我了解不多,看提示的意思似乎是无效的象素格式,TBitMap有个PixelFormat属性就是管这个的。
这里是一些相关的老帖子:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=561446
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1416014
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1147405
 
自己找到答案了。
var
pal : array[0..255]of TTrueColor;
lpPal : IDirectDrawPalette;
tb : TBitmap;
begin
tb:=TBitmap.Create;
tb.LoadFromFile('C:/abc.bmp');
GetPaletteEntries(tb.Palette,0,255,pal);
lpDD.CreatePalette(DDPCAPS_8BIT,@pal,lpPal,nil); //lpDD:IDirectDraw
lpSF.SetPalette(lpPal); //lpSF:IDirectDrawSurface
tb.free;
end;

关键问题是没搞清楚TableColor及palette的意义,呵呵。
 
后退
顶部