1

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.
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
user2618164
  • 23
  • 2
  • 5
  • 1
    Well, crop might be understandable, but align... Align to what ? Could you please edit your question and elaborate on this ? – TLama Jul 31 '13 at 10:05
  • Please indent your code. Please also explain what you are trying to do, and show the code of your best attempt. – David Heffernan Jul 31 '13 at 10:07
  • sorry, I'm new on stackoverflow. I'm trying to paste a image/BMP from the clipboard. The problem is that the image I want to paste doensn't fit. So, besides resizing it the image must be cropped in it's width. – user2618164 Jul 31 '13 at 13:48

1 Answers1

4

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;
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58