TPNGImage help

TPNGImage

Example 3: Drawing png over other formats


As a TGraphic descendent, TPngObject supports assigning from and to TBitmap. As an intermediary class, TBitmap talks to most (if not all) other formats such as metafiles, icons, jpgs among others.

The example bellow loads a jpg, a png and draws the png over the jpg. The png image may use alpha transparency normally.

{Draws a PNG over a JPG and saves again}
procedure PNGOverJPG(InJPG, InPNG: String; OutJPG: String);
var
  JPG: TJPEGImage;
  BMP: TBitmap;
  PNG: TPNGObject;
begin
  {Creates and loads the input images}
  JPG := TJPEGImage.Create;
  JPG.LoadFromFile(InJPG);
  BMP := TBitmap.Create;
  BMP.Assign(JPG);
  PNG := TPNGObject.Create;
  PNG.LoadFromFile(InPNG);
  {Draws over the bitmap (containing the JPG)}
  BMP.Canvas.Draw(0, 0, PNG);
  {Assigns back to the JPG}
  JPG.Assign(BMP);
  {Saves the JPG}
  JPG.SaveToFile(OutJPG);
  {Free the images}
  JPG.Free;
  BMP.Free;
  PNG.Free;
end;