How to stretch image in Firemonkey?

Viewed 3607

I would like to resize an image to a predefined bitmap of 64 * 64, regardless of its current dimensions and aspect ratio. I tried Bitmap.ReSize but that keeps the aspect ratio. I tried TImage and set the WrapMode to iwStretch. That works to a certain extent in that it indeed rescales the image as I want it, but I can't find a way to get that image out of the TImage. The Bitmap property of TImage still contains the original bitmap.

Does anybody know how to fetch the Image from a TImage as it is shown on the screen? Or even better: point me to a function that does this kind of resizing and stretching? If there is one, I have missed it.

Thanks for your time.

1 Answers

To stretch an image in Fmx you don't need to use a TImage. I understand you do not really want to use a TImage, and the solution is as follows:

var
  bmpA, bmpB: TBitmap;
  src, trg: TRectF;
begin
  bmpA := nil;
  bmpB := nil;
  try
    bmpA := TBitmap.Create;
    bmpA.LoadFromFile('C:\tmp\Imgs\149265645.jpg');

    bmpB:= TBitmap.Create;
    bmpB.SetSize(64, 64);

    src := RectF(0, 0, bmpA.Width, bmpA.Height);
    trg := RectF(0, 0, 64, 64);

    bmpB.Canvas.BeginScene;
    bmpB.Canvas.DrawBitmap(bmpA, src, trg, 1);
    bmpB.Canvas.EndScene;

    bmpB.SaveToFile('C:\tmp\Imgs\149265645_take_two.bmp');
  finally
    bmpA.Free;
    bmpB.Free;
  end;
end;

You let the bmpB.Canvas draw the bmpA bitmap and resizing the image at the same time according src and trg rectangles.

Related