Xel Naga
Xel Naga

Reputation: 956

Convert PNG image to grayscale without losing transparency using Delphi

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.

Upvotes: 0

Views: 380

Answers (1)

viniciusfbb
viniciusfbb

Reputation: 681

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;

Upvotes: 3

Related Questions