unit Unit1;interface
    uses  Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,
      StdCtrls,ExtCtrls,Math;
implementation
  type   PTBITMAPFILEHEADER   =   ^TBITMAPFILEHEADER;
         PBITMAPINFOHEADER   = ^BITMAPINFOHEADER;
         PBITMAPINFO = ^ BITMAPINFO;
function Helper_CreateBitmapFromBuffer(pbuffer:PByte;cdSize:DWORD):HBITMAP;
var
 hBmp:HBitMap;
 WHDC:HDC;
 hPal:HPalette;
 hOldpal:HPALETTE;
 pbmfHeader:PTBITMAPFILEHEADER;
 dwOffset:DWORD;
 pbmiHeader:PBITMAPINFOHEADER ;
 pbminfo:PBITMAPINFO;
 nColors :integer;
 lpDIBBits:pointer;
 nSize : longint;
 pLP:^LOGPALETTE;
 i:integer;
begin
    dwOffset := sizeof(BITMAPFILEHEADER);
    pbmfHeader :=PTBITMAPFILEHEADER(pbuffer);
//FIle type should be 'BM'
  pbmiHeader:=PBITMAPINFOHEADER(pchar(pBuffer)+dwOffset);
  pbminfo:=PBITMAPINFO(pchar(pBuffer)+dwOffset);
//if bmiHeader.biClrUsed is zero we have to infer the number
// of colors from the number of bits used to specify it.
  nColors := ifthen(Boolean(pbmiHeader^.biClrUsed),pbmiHeader^.biClrUsed,pbmiheader^.biBitCount shl 1);
//  just do logic comparionWHDC := GetDC(HWND(0));
//---------------------------------------------------------------------------------
if pbmfHeader^.bfType <> (WORD('M') shl 8 or WORD('B')) then result:= 0;if pbmInfo^.bmiHeader.biBitCount > 8 then
  begin
    lpDIBBits:=pointer(LPDWORD(pchar(pbminfo^.bmiColors) + pbmInfo^.bmiHeader.biClrUsed + ifthen(pbmInfo^.bmiHeader.biCompression = BI_BITFIELDS,3,0)))
  end
else
  begin
    lpDIBBits := pointer(pchar(pbmInfo^.bmiColors)+nColors)
end;
//---------------------------------------------------------------------------------
if nColors <= 256  then
begin
nSize := sizeof (LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
GetMem(pLP,nSize);
pLp^.palVersion := $300;
pLP^.palNumEntries := nColors;
for i:= 0 to   nColors - 1 do
begin
pLp^.palpalEntry[i].peRed := pbmInfo^.bmiColors[i].rgbRed;
pLp^.palPalEntry[i].peGreen := pbmInfo^.bmiColors[i].rgbGreen;
pLp^.palPalEntry[i].peBlue := pbmInfo^.bmiColors[i].rgbBlue;
pLp^.palpalEntry[i].peFlags := 0;
end;hPal := CreatePalette(pLp^);
freemem(pLP);
hOldpal := HPALETTE(SelectObject(hdc,hPal));
RealizePalette(hdc);end;
//-----------------------------------------------------------------------------------
hBmp:= CreateDIBitmap(hdc,pbmiHeader,CBM_INIT,lpDIBBits,pbmInfo,DIB_RGB_COLORS);
//-----------------------------------------------------------------------------------
if hPal then
begin SelectObject(hdc,hOldPal);
 DeleteObject(hPal);
end ;ReleaseDC(nil,hdc);
result:= hBmp;
end;
end.

解决方案 »

  1.   

    好的,我把代码简化了些,你看看.unit Unit1;interface
        uses  Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,
          StdCtrls,ExtCtrls,Math;
    implementation
      type   PTBITMAPFILEHEADER   =   ^TBITMAPFILEHEADER;
             PBITMAPINFOHEADER   = ^BITMAPINFOHEADER;
             PBITMAPINFO = ^ BITMAPINFO;
    function Helper_CreateBitmapFromBuffer(pbuffer:PByte;cdSize:DWORD):HBITMAP;
    var
     hBmp:HBitMap;
     WHDC:HDC;
     hPal:HPalette;
     hOldpal:HPALETTE;
     pbmfHeader:PTBITMAPFILEHEADER;
     dwOffset:DWORD;
     pbmiHeader:PBITMAPINFOHEADER ;
     pbminfo:PBITMAPINFO;
     nColors :integer;
     lpDIBBits:pointer;
     nSize : longint;
     pLP:^LOGPALETTE;
     i:integer;
    begin
        dwOffset := sizeof(BITMAPFILEHEADER);
        pbmfHeader :=PTBITMAPFILEHEADER(pbuffer);
    //FIle type should be 'BM'
      pbmiHeader:=PBITMAPINFOHEADER(pchar(pBuffer)+dwOffset);
      pbminfo:=PBITMAPINFO(pchar(pBuffer)+dwOffset);
    //if bmiHeader.biClrUsed is zero we have to infer the number
    // of colors from the number of bits used to specify it.
      nColors := ifthen(Boolean(pbmiHeader^.biClrUsed),pbmiHeader^.biClrUsed,pbmiheader^.biBitCount shl 1);
    //  just do logic comparionWHDC := GetDC(HWND(0));//---------------------------------------------------------------------------------
    if pbmfHeader^.bfType <> (WORD('M') shl 8 or WORD('B')) then result:= 0;
    if pbmInfo^.bmiHeader.biBitCount > 8 then
      begin
        lpDIBBits:=pointer(LPDWORD(pchar(pbminfo^.bmiColors) + pbmInfo^.bmiHeader.biClrUsed + ifthen(pbmInfo^.bmiHeader.biCompression = BI_BITFIELDS,3,0)))
      end
    else
      begin
        lpDIBBits := pointer(pchar(pbmInfo^.bmiColors)+nColors)
    end;//---------------------------------------------------------------------------------
    if nColors <= 256  then
    begin
    nSize := sizeof (LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
    GetMem(pLP,nSize);
    pLp^.palVersion := $300;
    pLP^.palNumEntries := nColors;
    //pLp^.palNumEntries := nColors;
    for i:= 0 to   nColors - 1 do
    begin
    pLp^.palpalEntry[i].peRed := pbmInfo^.bmiColors[i].rgbRed;
    pLp^.palPalEntry[i].peGreen := pbmInfo^.bmiColors[i].rgbGreen;
    pLp^.palPalEntry[i].peBlue := pbmInfo^.bmiColors[i].rgbBlue;
    pLp^.palpalEntry[i].peFlags := 0;
    end;hPal := CreatePalette(pLp^);
    FreeMem(plP);
    hOldPal:=SelectObject(hdc,hPal);   //有错误的地方,提示是 '(' expected but ',' foundend;
    //-----------------------------------------------------------------------------------
    end;                               //'.' expected but ';' found
    end.
    是在搞不懂了,在线等.
      

  2.   

    该为hOldPal:=SelectObject(WHDC,hPal);
      

  3.   

    你在书写时候把代码前后对整齐,然后看下哪里end有问题
    在这里看好难看出来