Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Fit image in control (https://www.delphipraxis.net/191598-fit-image-control.html)

WojTec 31. Jan 2017 19:51

Delphi-Version: 10 Berlin

Fit image in control
 
Delphi-Quellcode:
        ScaleX := AControlWidth / AMediaWidth;
        ScaleY := AControlHeight / AMediaHeight;

        if ScaleX >= ScaleY then
        begin
//          Result.Width := Round(AMediaWidth * ScaleY);
          Result.Width := Round((AMediaWidth / AMediaHeight) * AControlHeight);
          Result.Height := AControlHeight;
        end
        else
        begin
          Result.Width := AControlWidth;
          Result.Height := Round((AMediaHeight / AMediaWidth) * AControlWidth);
//          Result.Height := Round(AMediaHeight * ScaleX);
        end;
Samples:
1920, 1080, 1366, 768 = 1365, 768
1366, 768, 1920, 1080 = 1920, 1079

So, in both variants (commented and not) result is the same (code based on GR32.GetBitmapSize) - is one line not enough in width or height. How to fix it? Something like add one and check range?

Uwe Raabe 31. Jan 2017 22:28

AW: Fit image in control
 
Seems like you want to keep the aspect ratio of the media when stretching into the control. In that case you will most likely end up with the media not filling the control completely. As the aspect ratio of the media and the control differ slightly, there is not much you can do about it.

WojTec 1. Feb 2017 09:24

Re: Fit image in control
 
Yes, but in examples both ratios are 16:9, it shouldn't be streched (of course only in case if both has the same aspect ratio, for example HD/FullHD/UltraHD stream on wide screen)?

Rollo62 1. Feb 2017 10:35

AW: Fit image in control
 
Maybe this helps to make calculations easier, if this is what you are looking for.

FitInto and PlaceInto are nice functions already in the library, but not very popular.

Zitat:

class function Empty: TRectF; inline; static;
{ This method is to be deprecated. It stretches current rectangle into designated area similarly to FitInto,
but only when current rectangle is bigger than the area; otherwise, it only centers it. }
function Fit(const BoundsRect: TRectF): Single; // deprecated 'Please consider using FitInto instead.';
{ Stretches current rectangle into the designated area, preserving aspect ratio. Note that unlike Fit, when designated
area is bigger than current rectangle, the last one will be stretched to fill designated area (while Fit would only
center it). }
function FitInto(const ADesignatedArea: TRectF; out Ratio: Single): TRectF;
http://docwiki.embarcadero.com/Libra...m.Types.TRectF

Rollo

Uwe Raabe 1. Feb 2017 11:23

AW: Re: Fit image in control
 
Zitat:

Zitat von WojTec (Beitrag 1360522)
Yes, but in examples both ratios are 16:9, it shouldn't be streched (of course only in case if both has the same aspect ratio, for example HD/FullHD/UltraHD stream on wide screen)?

1366x768 is not precisely (as in floating precision of your CPU) 16:9. If it were, ScaleX and ScaleY would be equal in your example.

WojTec 1. Feb 2017 14:05

Re: Fit image in control
 
Ok, I understand, thank you :)

But another question is, if above code is oke, why eg. Photoshop has different result for this data?

mikhal 1. Feb 2017 14:45

AW: Fit image in control
 
Could be a rounding error. Try another formula:

Delphi-Quellcode:
Result.Width := Round(AMediaWidth * AControlHeight / AMediaHeight);

and
Delphi-Quellcode:
Result.Height := Round(AMediaHeight * AControlWidth / AMediaWidth);

Your Version defines a small result in your term to round, leading to a possible inaccuracy with your following multipication.

Second: Round uses Banker's Rounding. If your term to round is in the middle of two numbers it will give you the even number.

Gretings
Mikhal

WojTec 1. Feb 2017 15:28

Re: Fit image in control
 
Also returns one line not enough :(

Uwe Raabe 1. Feb 2017 16:01

AW: Re: Fit image in control
 
Zitat:

Zitat von WojTec (Beitrag 1360561)
But another question is, if above code is oke, why eg. Photoshop has different result for this data?

Probably because PS does it different.

WojTec 1. Feb 2017 16:33

Re: Fit image in control
 
Yeah, what is different way to do this?


Alle Zeitangaben in WEZ +1. Es ist jetzt 06:33 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz