AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

Fit image in control

Ein Thema von WojTec · begonnen am 31. Jan 2017 · letzter Beitrag vom 1. Feb 2017
Antwort Antwort
Seite 1 von 2  1 2   
WojTec

Registriert seit: 17. Mai 2007
477 Beiträge
 
Delphi XE6 Professional
 
#1

Fit image in control

  Alt 31. Jan 2017, 19:51
Delphi-Version: 10 Berlin
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?
  Mit Zitat antworten Zitat
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
9.406 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: Fit image in control

  Alt 31. Jan 2017, 22:28
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.
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
477 Beiträge
 
Delphi XE6 Professional
 
#3

Re: Fit image in control

  Alt 1. Feb 2017, 09:24
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)?
  Mit Zitat antworten Zitat
Rollo62

Registriert seit: 15. Mär 2007
3.441 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: Fit image in control

  Alt 1. Feb 2017, 10:35
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
  Mit Zitat antworten Zitat
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
9.406 Beiträge
 
Delphi 11 Alexandria
 
#5

AW: Re: Fit image in control

  Alt 1. Feb 2017, 11:23
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.
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
477 Beiträge
 
Delphi XE6 Professional
 
#6

Re: Fit image in control

  Alt 1. Feb 2017, 14:05
Ok, I understand, thank you

But another question is, if above code is oke, why eg. Photoshop has different result for this data?
  Mit Zitat antworten Zitat
Benutzerbild von mikhal
mikhal

Registriert seit: 11. Sep 2003
Ort: Linz am Rhein
787 Beiträge
 
Delphi 10.3 Rio
 
#7

AW: Fit image in control

  Alt 1. Feb 2017, 14:45
Could be a rounding error. Try another formula:

Result.Width := Round(AMediaWidth * AControlHeight / AMediaHeight);
and
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
Michael Kraemer
Computer erleichtern die Arbeit...
...und die Erde ist eine Scheibe!
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
477 Beiträge
 
Delphi XE6 Professional
 
#8

Re: Fit image in control

  Alt 1. Feb 2017, 15:28
Also returns one line not enough
  Mit Zitat antworten Zitat
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
9.406 Beiträge
 
Delphi 11 Alexandria
 
#9

AW: Re: Fit image in control

  Alt 1. Feb 2017, 16:01
But another question is, if above code is oke, why eg. Photoshop has different result for this data?
Probably because PS does it different.
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
477 Beiträge
 
Delphi XE6 Professional
 
#10

Re: Fit image in control

  Alt 1. Feb 2017, 16:33
Yeah, what is different way to do this?
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2   

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 07:55 Uhr.
Powered by vBulletin® Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2021 by Daniel R. Wolf