user2618164
user2618164

Reputation: 23

crop and align inserted BMP in Delphi

I want to crop and align the inserted BMP from the clipboard. I'm trying for 2 days but still nothing workable...

procedure TForm1.act1Execute(Sender: TObject);
var
BMP : TBitmap;
begin
BMP := TBitmap.Create;
BMP.Assign(Clipboard);
BMP.SetSize(400,200);
Img1.picture.Graphic := BMP;
BMP.Free;
end;

procedure TForm1.act1Update(Sender: TObject);
begin
(Sender as TAction).Enabled := Clipboard.HasFormat(CF_BITMAP);
end;

end.

Upvotes: 1

Views: 2024

Answers (1)

Andrew Sergeev
Andrew Sergeev

Reputation: 51

If I understand you right, you need to center the bitmap in the Image control?

It's simple - set the Img1.Center := True

To crop the bitmap you need code like this:

    procedure CropBitmap(Bmp: TBitmap; const CropRect: TRect);
    var
      CropBmp: TBitmap;
    begin
      CropBmp := TBitmap.Create;
      try
        CropBmp.Width := CropRect.Right - CropRect.Left;
        CropBmp.Height := CropRect.Bottom - CropRect.Top;
        CropBmp.Canvas.CopyRect(
          Rect(0, 0, CropBmp.Width, CropBmp.Height),
          Bmp.Canvas,
          CropRect
        );
        Bmp.Assign(CropBmp);
      finally
        CropBmp.Free;
      end;
    end;

Upvotes: 4

Related Questions