Home > front end >  Convert PNG image to grayscale without losing transparency using Delphi
Convert PNG image to grayscale without losing transparency using Delphi

Time:11-04

I want to convert a PNG image to grayscale with transparency (not losing alpha channel). Probably using TBitmap and TPngImage components or Skia4Delphi.

From transparent colored PNG to transparent gaeyscale PNG

But how? I'm using Delphi 10.3.3 VCL.

CodePudding user response:

Using Skia4Delphi library:

uses
  System.UITypes, Skia;

procedure TForm1.FormCreate(Sender: TObject);
var
  LImage: ISkImage;
  LSurface: ISkSurface;
  LPaint: ISkPaint;
begin
  LImage := TSkImage.MakeFromEncodedFile('..\..\delphi.png');
  LPaint := TSkPaint.Create;
  LPaint.ColorFilter := TSkColorFilter.MakeHighContrast(TSkHighContrastConfig.Create(True, TSkContrastInvertStyle.NoInvert, 0));
  LSurface := TSkSurface.MakeRaster(LImage.Width, LImage.Height);
  LSurface.Canvas.Clear(TAlphaColors.Null);
  LSurface.Canvas.DrawImage(LImage, 0, 0, LPaint);
  LSurface.MakeImageSnapshot.EncodeToFile('..\..\delphi-grayscale.png');
end;
  • Related