Bitmap cropping: some misunderstandings and some help would be welcome

  

I’m working on a small project integrating bitmap croping but the result expected is not here.
The sample firemonkey project has a TImage with a picture loaded. I’m drawing a rectange to select what kind of the bitmap part should be "extracted". Here is the obtained result :

So, when I click on the "Crop" button here is what the result is :

As you could see, on the top and bottom, I’ve lost some bitpmap lines.
Here is the code behind the OnClick event :
procedure TForm1.Button1Click(Sender: TObject);
var
lBmp: TBitmap;
xScale, yScale: extended;
iRect: TRect;
begin
if Rectangle1.Visible then
begin
lBmp := TBitmap.Create;
try
xScale := Image1.Bitmap.Width / Image1.Width;
yScale := Image1.Bitmap.Height / Image1.Height;

lBmp.Width := round(Rectangle1.Width * xScale);
lBmp.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);

lBmp.CopyFromBitmap(Image1.Bitmap, iRect, 0, 0);

Image1.Bitmap.Clear(0);
Image1.Bitmap := lBmp;

Rectangle1.Visible := False;
finally
FreeAndNil(lBmp);
end;
end
else
begin
Rectangle1.Visible := True;
Rectangle1.Width := Round(Panel1.Width * 0.5);
Rectangle1.Height := Round(Rectangle1.Width * 1.41);
Rectangle1.Position.X := Round(Panel1.Width * 0.5)-(Rectangle1.Width * 0.5);
Rectangle1.Position.Y := Round(Panel1.Height * 0.5)-(Rectangle1.Height * 0.5);
end;
end;

If someone could help me on what’s wrong with my code, it could be very nice.
@Tom Brunberg here is the link where you could download the sample project
CropPicture.rar
Thank you

Comments are closed.