Little Helper
Little Helper

Reputation: 2456

How to disable transparency for PNG

How to completely disable transparency of given PNGObject? By the way I am using PNGImage unit of Version 1.564.

Upvotes: 3

Views: 2009

Answers (2)

asd
asd

Reputation: 29

For just drawing a TPNGObject (Delphi PNGComponents library) to some background color (in example: white) with alpha blending, try this:

uses 
  PNGImage, PNGFunctions;

procedure TForm1.Button1Click(Sender: TObject);
var png: TPNGObject;
    bmp: TBitmap;
begin
  try
    // load PNG
    png := TPNGObject.Create;
    png.LoadFromFile('MyPNG.png');

    // create Bitmap
    bmp := TBitmap.Create;
    bmp.Width  := png.Width;
    bmp.Height := png.Height;

    // set background color to whatever you want
    bmp.Canvas.Brush.Color := clWhite;
    bmp.Canvas.FillRect(Rect(0, 0, png.Width, png.Height));

    // draw PNG on Bitmap with alpha blending
    DrawPNG(png, bmp.Canvas, Rect(0, 0, png.Width, png.Height), []);

    // save Bitmap
    bmp.SaveToFile('MyBMP.bmp');

  finally
    FreeAndNil(png);
    FreeAndNil(bmp);
  end;
end;

To use the DrawPNG procedure you have to include the PNGFunctions unit.

Upvotes: 2

TLama
TLama

Reputation: 76663

I don't think it's possible to permanently disable TPNGObject image transparency. Or at least I couldn't find a property for doing this. And it should have been controlled by a property since when you assign or load an image, the TPNGObject takes the image parameters (including transparency) from the image file assigned.

So as a workaround I would prefer to use the RemoveTransparency procedure after when you load or assign the image:

uses
  PNGImage;

procedure TForm1.Button1Click(Sender: TObject);
var
  PNGObject: TPNGObject;
begin
  PNGObject := TPNGObject.Create;
  try
    PNGObject.LoadFromFile('C:\Image.png');
    PNGObject.RemoveTransparency;
    PNGObject.Draw(Canvas, Rect(0, 0, PNGObject.Width, PNGObject.Height));
  finally
    PNGObject.Free;
  end;
end;

Upvotes: 8

Related Questions