2
votes

I need create a crop effect to my app. I have a TRectangle over a TImage, and I need when the user press the save button, copy just the area that the TRectangle uses. There is some way that I can cut just a specific area from the Image1.Bitmap? I printed a image to better ilustrate what I need:

enter image description here

1
You better use a canvas which you can control the scale. Don't just use any random image control. If it were me, I'd write a custom control for this. But it wouldn't be a very trivial one.Jerry Dodge
What is the WrapMode that you use? What if it's Tile and your selector covers more than one tile of the displayed image?Victoria
The crop procedure would be like this: 1) calculate the scaling of your image and convert the rectangles bounds to the image scale. 2) create a TBitmap and use CopyFromBitmap with the calculated rectangle. This copies a section from your TImage bitmap to the new bitmap. 3) assign the bitmap to your TImage bitmap (if that's what you want).Hans
Can you please give me a code example on how to do the step number 2?Diego Bittencourt
The concept is very trivial: Draw your image on a controlled canvas (one which you have 100% control over), and do simple math to determine scale. Surely there are plenty of resources how to extract a portion of an image in Firemonkey. I used them to write my own vector signature control. Only much simpler for you. But please don't expect us to write it for you.Jerry Dodge

1 Answers

5
votes

Here is a sample that works for me:

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;

I assume here that Rectangle1 has Image1 as its Parent:

enter image description here

Otherwise you will need to consider the offset of Position.X and Position.Y properties.

Here is a result of procedure functioning: enter image description here