简体   繁体   English

如何禁用PNG的透明度

[英]How to disable transparency for PNG

How to completely disable transparency of given PNGObject ? 如何完全禁用给定PNGObject透明度? By the way I am using PNGImage unit of Version 1.564. 顺便说一下,我正在使用1.564版的PNGImage单元。

I don't think it's possible to permanently disable TPNGObject image transparency. 我认为不可能永久禁用TPNGObject图像透明度。 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. 并且它应该已经由属性控制,因为当您分配或加载图像时, TPNGObject从分配的图像文件中获取图像参数(包括透明度)。

So as a workaround I would prefer to use the RemoveTransparency procedure after when you load or assign the image: 因此,作为一种解决方法,我宁愿在加载或分配图像后使用RemoveTransparency过程:

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;

For just drawing a TPNGObject (Delphi PNGComponents library) to some background color (in example: white) with alpha blending, try this: 对于仅通过alpha混合将TPNGObject(Delphi PNGComponents库)绘制为某种背景颜色(例如:白色),请尝试以下操作:

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. 要使用DrawPNG过程,您必须包含PNGFunctions单元。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM