0

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.

Xel Naga
  • 826
  • 11
  • 28
  • 3
    I think you should give skia4delphi a try. It is an open Source Framework and has tons of features for manipulating images. See here for some Samples: https://github.com/skia4delphi/skia4delphi/tree/main/Samples – fisi-pjm Nov 03 '22 at 11:05
  • @fisi-pjm Skia4Delphi looks awesome. However sample code of what I want to do isn't there. – Xel Naga Nov 03 '22 at 22:42

1 Answers1

3

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;
vfbb
  • 611
  • 7
  • 15