2006. november 25., szombat

How to load a bitmap from a resource file without losing its palette


Problem/Question/Abstract:

How to load a bitmap from a resource file without losing its palette

Answer:

procedure TForm1.Button1Click(Sender: TObject);
var
  Bmp: TBitmap;
  HResInfo: THandle;
  BMF: TBitmapFileHeader;
  MemHandle: THandle;
  Stream: TMemoryStream;
  ResPtr: PByte;
  ResSize: Longint;
begin
  BMF.bfType := $4D42;
  HResInfo := FindResource(HInstance, 'BITMAP_2', RT_Bitmap);
  ResSize := SizeofResource(HInstance, HResInfo);
  MemHandle := LoadResource(HInstance, HResInfo);
  try
    ResPtr := LockResource(MemHandle);
    Stream := TMemoryStream.Create;
    try
      Stream.SetSize(ResSize + SizeOf(BMF));
      Stream.Write(BMF, SizeOf(BMF));
      Stream.Write(ResPtr^, ResSize);
      Stream.Seek(0, 0);
      Bmp := TBitmap.Create;
      try
        Bmp.LoadFromStream(Stream);
        Canvas.Draw(0, 0, Bmp);
      finally
        Bmp.Free;
      end;
    finally
      Stream.Free;
    end;
  finally
    FreeResource(MemHandle);
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése