DelphianBoy
DelphianBoy

Reputation: 55

Setting Color Type of TPngImage

I'm using Delphi's TPngImage class to convert BMP images (TBitmap) to PNG, by assigning the respective TBitmap object with the bitmap image in it, to the freshly created TPngImage object.

I need to set the color type to COLOR_PALETTE to create an Indexed RGB PNG.

I didn't manage to find any property of the TPngImage class that can do it.

Can anyone help me?

Upvotes: 1

Views: 852

Answers (1)

Victoria
Victoria

Reputation: 7912

You can specify color type in the CreateBlank constructor and instead of assigment simply flush the bitmap on the PNG image canvas. For example:

var
  R: TRect;
  Bmp: TBitmap;
  Png: TPngImage;
begin
  Bmp := TBitmap.Create;
  try
    Bmp.LoadFromFile('C:\Source.bmp');
    Png := TPngImage.CreateBlank(COLOR_PALETTE, 8, Bmp.Width, Bmp.Height);
    try
      R := Rect(0, 0, Bmp.Width, Bmp.Height);
      Png.Canvas.CopyRect(R, Bmp.Canvas, R);
      Png.SaveToFile('C:\Target.png');
    finally
      Png.Free;
    end;
  finally
    Bmp.Free;
  end;
end;

Upvotes: 4

Related Questions