简体   繁体   中英

How to resize image using Skia4Delphi

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)

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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