Algorithms, Blockchain and Cloud

How to Convert JPEG to BMP and BMP to JPEG in Object Pascal / Delphi ?


The BMP is uncompressed image file type, which is major supported in most devices (especially legacy devices). It contains almost raw image pixel data (e.g. 24-bit, RGB). The JPEG is widely supported because of its high compress ratio and simple format to read and write.

For Delphi classes such as TPicture, TGraphics, they can all access internal data through TBitmap class. Therefore, sometimes it is needed to convert between JPEG and BMP.

Find the straightforward implementation in Delphi/Object Pascal for the following two converter functions between JPEG file and BMP file.

JPEG to BMP

procedure Jpeg2Bmp(const BmpFileName, JpgFileName: string); // helloacm.com
var
  Bmp: TBitmap;
  Jpg: TJPEGImage;
begin
  Bmp := TBitmap.Create;
  Bmp.PixelFormat := pf32bit;
  Jpg := TJPEGImage.Create;
  try
    Jpg.LoadFromFile(JpgFileName);
    Bmp.Assign(Jpg);
    Bmp.SaveToFile(BmpFileName);
  finally
    Jpg.Free;
    Bmp.Free;
  end;
end;

BMP to JPEG

procedure Bmp2Jpeg(const BmpFileName, JpgFileName: string); // helloacm.com
var
  Bmp: TBitmap;
  Jpg: TJPEGImage;
begin
  Bmp := TBitmap.Create;
  Bmp.PixelFormat := pf32bit;
  Jpg := TJPEGImage.Create;
  try
    Bmp.LoadFromFile(BmpFileName);
    Jpg.Assign(Bmp);
    Jpg.SaveToFile(JpgFileName);
  finally
    Jpg.Free;
    Bmp.Free;
  end;
end;

–EOF (The Ultimate Computing & Technology Blog) —

223 words
Last Post: How to Get File Size in Bytes using Delphi / Object Pascal ?
Next Post: Using AT keyboard on modern PC using AT-PS2-USB connector

The Permanent URL is: How to Convert JPEG to BMP and BMP to JPEG in Object Pascal / Delphi ? (AMP Version)

Exit mobile version