3

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

Little Helper
  • 2,419
  • 9
  • 37
  • 67
  • 3
    What does it mean to disable transparency? Do you want to set the color format / bit depth to Truecolor? That is 24 bits per pixel, no alpha. – David Heffernan Jun 11 '12 at 06:57
  • @Roberts, why do you want the PNG without transparency? I guess if you want it, you can assign the TPngImage object to a new TBitmap object, and then assign the TBitmap object back to the TPngImage object again. I'm not 100% sure that this will work, but I think it should. – bjaastad_e Jun 11 '12 at 07:28
  • @Elling I already tried that :D. Didn't work :D. – Little Helper Jun 11 '12 at 07:41
  • @Roberts Ok. I do not have Delphi at hand right now, so I cannot help you to investigate it further. But, I see that a similar question has been asked earlier on stackoverflow. Maybe there's some help in the answers to that question: http://stackoverflow.com/questions/1141561/ – bjaastad_e Jun 11 '12 at 08:05
  • @Elling TBitmap supports partial transparency – David Heffernan Jun 11 '12 at 08:53

2 Answers2

8

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;
TLama
  • 75,147
  • 17
  • 214
  • 392
2

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.

asd
  • 29
  • 1