4

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.

Arnold
  • 4,578
  • 6
  • 52
  • 91

1 Answers1

6

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.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • Yes! This works. Your assumption about TImage is correct and your solution is a neat one.You might have guessed that in the process of trying to solve this problem I encountered the problem you also commented upon. By adopting this solution that question is not relevant anymore although it still baffles me what I could have done to cause that error. Anyways, thanks a lot for your time! – Arnold Sep 26 '17 at 07:54