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

How crop bitmap in selected area using firemonkey?

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

这是一个适合我的示例:

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;

我在这里假设 Rectangle1Image1 作为它的 Parent:

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

这是程序运行的结果:

另一种方法:

var
  Tmp: TBitmap;
  Bmp: TBitmap;
  iRect: TRect;
begin
  Tmp := TBitmap.Create;
  Tmp := Image1.MakeScreenshot; //ignore the scale, so will F*ck the resolution, but resolve the scale issues
  Bmp := TBitmap.Create;
  try
    Bmp.Width := round(Rectangle1.Width);
    Bmp.Height := round(Rectangle1.Height);
    iRect.Left := round(Rectangle1.Position.X);
    iRect.Top := round(Rectangle1.Position.Y);
    iRect.Width := round(Rectangle1.Width);
    iRect.Height := round(Rectangle1.Height);
    Bmp.CopyFromBitmap(tmp, iRect, 0, 0);
    Image2.Bitmap := Bmp
  finally
    Tmp.Free;
    Bmp.Free;
  end;