2007. június 24., vasárnap

How to display a 24 bit True Color bitmap image on a 256 color display


Problem/Question/Abstract:

How to display a 24 bit True Color bitmap image on a 256 color display

Answer:

You can take advantage of the new graphics capabilities of the TBitmap and TJPEGImage components of Delphi 3/4. When Delphi 3/4 loads a bitmap image, it keeps a copy of the device independent bitmap image it loads from a file in the background. The TJPEGImage component is very good at color reducing a full color image down to 256 colors. By Loading the bitmap, then assigning the image to a Jpeg and saving it to a temporary ".JPG" file, you can then load the temporary file back into a TImage with much better results than simply loading the bitmap file unconverted. The following example demonstrates the necessary steps to achieve these results.

uses
  JPEG;

procedure TForm1.Button1Click(Sender: TObject);
var
  JP: TJPEGImage;
  IM: TImage;
  TempFileName: string;
begin
  {Pop up an Open Dialog}
  OpenDialog1.Options := [ofNoChangeDir, ofFileMustExist];
  OpenDialog1.Filter := 'Bitmap Files (*.bmp)|*.bmp';
  if OpenDialog1.Execute then
  begin
    {Create a temporary TImage}
    IM := TImage.Create(nil);
    {Load the bitmap file}
    IM.Picture.LoadFromFile(OpenDialog1.FileName);
    {Create a temporary TJPEGImage}
    JP := TJPEGImage.Create;
    {Priority on quality}
    JP.Performance := jpBestQuality;
    {Assign the bitmap to the JPEG}
    JP.Assign(IM.Picture.Graphic);
    {Free the temp image}
    IM.Free;
    {Make a temp file name with the extension of .jpg}
    TempFileName := 'test.jpg';
    {Save the JPEG to a temp file}
    JP.SaveToFile(TempFileName);
    {Free the JPEG}
    JP.Free;
    {Load the temp file to an image on the form}
    Image1.Picture.LoadFromFile(TempFileName);
    {Delete the temp file}
    DeleteFile(TempFileName);
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése