我可以使用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)
下面是一个简单的(拉伸)调整大小的代码:
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;