如何使用Skia4Delphi调整图像大小

qlvxas9a  于 2022-12-12  发布在  其他
关注(0)|答案(1)|浏览(271)

我可以使用Skia4Delphi加载和保存图像文件。
下面是我的代码:

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;

如何在保存之前将图像调整到定义的大小(宽度和高度)?( Delphi 10.3.3 VCL)

umuewwlo

umuewwlo1#

下面是简单(拉伸)调整大小的代码:

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;

相关问题