How to resize image using Skia4Delphi

651 views Asked by At

I can load and save image files using Skia4Delphi.

Here is my code:

var
  LImage: ISkImage;
  LSurface: ISkSurface;
  LPaint: ISkPaint;
begin
  LImage := TSkImage.MakeFromEncodedFile('C:\IMAGE-OLD.PNG');
  LPaint := TSkPaint.Create;
  LSurface := TSkSurface.MakeRaster(LImage.Width, LImage.Height);
  LSurface.Canvas.DrawImage(LImage, 0, 0, LPaint);
  LSurface.MakeImageSnapshot.EncodeToFile('C:\IMAGE-NEW.PNG');
end;

How can I resize the image to a defined size (width and height) before saving? (Delphi 10.3.3 VCL)

1

There are 1 answers

0
vfbb On BEST ANSWER

Here is the code for a simple (stretched) resize:

uses
  System.UITypes, Skia;

function GetResizedImage(const AImage: ISkImage; const ANewWidth, ANewHeight: Integer): ISkImage;
var
  LSurface: ISkSurface;
begin
  LSurface := TSkSurface.MakeRaster(ANewWidth, ANewHeight);
  LSurface.Canvas.Clear(TAlphaColors.Null);
  LSurface.Canvas.Scale(ANewWidth / AImage.Width, ANewHeight / AImage.Height);
  LSurface.Canvas.DrawImage(AImage, 0, 0, TSkSamplingOptions.High);
  Result := LSurface.MakeImageSnapshot;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  LImage: ISkImage;
begin
  LImage := TSkImage.MakeFromEncodedFile('a.png');
  LImage := GetResizedImage(LImage, 24, 24);
  LImage.EncodeToFile('a.png', 100);
end;