如何使用 firemonkey 在选定区域裁剪位图?

Die*_*urt 2 delphi firemonkey delphi-10.2-tokyo

我需要为我的应用创建裁剪效果。我在 TImage 上有一个 TRectangle,我需要当用户按下保存按钮时,只复制 TRectangle 使用的区域。有什么方法可以从 Image1.Bitmap 中剪切特定区域?我打印了一张图像以更好地说明我需要的内容:

在此处输入图片说明

asd*_*-tm 5

这是一个对我有用的示例:

procedure TForm1.Button1Click(Sender: TObject);
var
  Bmp: TBitmap;
  xScale, yScale: extended;
  iRect: TRect;
begin

  Bmp := TBitmap.Create;
  xScale := Image1.Bitmap.Width / Image1.Width;
  yScale := Image1.Bitmap.Height / Image1.Height;
  try
    Bmp.Width := round(Rectangle1.Width * xScale);
    Bmp.Height := round(Rectangle1.Height * yScale);
    iRect.Left := round(Rectangle1.Position.X * xScale);
    iRect.Top := round(Rectangle1.Position.Y * yScale);
    iRect.Width := round(Rectangle1.Width * xScale);
    iRect.Height := round(Rectangle1.Height * yScale);
    Bmp.CopyFromBitmap(Image1.Bitmap, iRect, 0, 0);
    Image2.Bitmap := Bmp
  finally
    Bmp.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

我假设这里Rectangle1Image1它的Parent

在此处输入图片说明

否则,您将需要考虑Position.XPosition.Y属性的偏移量。

这是程序运行的结果: 在此处输入图片说明

  • 非常感谢,正是我需要的!! (2认同)