How to stretch image in Firemonkey?

怎甘沉沦 提交于 2019-12-07 18:09:14

问题


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:


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.



来源:https://stackoverflow.com/questions/46398851/how-to-stretch-image-in-firemonkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!