Recent

Author Topic: How to Scale a image propotionally?  (Read 4496 times)

asdlinux

  • New Member
  • *
  • Posts: 17
How to Scale a image propotionally?
« on: February 11, 2019, 11:31:19 am »
Hi Everyone,

I am making a small(so far) filemanager to learn Lazarus/FPC. In my fileManager I want to have a simple Image viewer, I managed to get that to work but when the image is bigger than the screen I want it to scale down to about 85% of the screen resolution. My current problem is how to scale the Image propotionally, I got it to scale to 85% of current screen resolution but when i do this it is not propotional.

At the moment I got this:
Code: Pascal  [Select][+][-]
  1.      if (ImageFile.Width>Screen.Width) or (ImageFile.Height>Screen.Height) then
  2.      begin
  3.        WProc:=round(Screen.Width div 100);
  4.        HProc:=round(Screen.Height div 100);
  5.        WScale:=Screen.Width div WProc;
  6.        HScale:=Screen.Height div HProc;
  7.        WScale:=WScale-75;
  8.        HScale:=HScale-90;
  9.        WScale:=WScale*WProc;
  10.        HScale:=HScale*HProc;
  11.        SW:=Round(Screen.Width-WScale);
  12.        SH:=Round(Screen.Height-HScale);
  13.        R.Right:=SW;
  14.        R.Bottom:=SH;
  15.        ImageViewer.Image.Canvas.StretchDraw(R, ImageFile.Graphic);
  16.      end;


How can I calculate the resolution of the image to be propotional? (Sorry for the beginner code ::))

Thank you !

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: How to Scale a image propotionally?
« Reply #1 on: February 11, 2019, 11:36:25 am »
To be able to scale image proportionally, you need to calculate the aspect ratio first.

You can learn from this post:
https://forum.lazarus.freepascal.org/index.php/topic,43823.msg307918.html#msg307918

Try it yourself. If you still can't understand, I'm willing to help.

asdlinux

  • New Member
  • *
  • Posts: 17
Re: How to Scale a image propotionally?
« Reply #2 on: February 11, 2019, 11:41:56 am »
Thank you for the hint, I will try myself first so I will learn. If I run into trouble I will ask you   :)

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: How to Scale a image propotionally?
« Reply #3 on: February 11, 2019, 11:47:20 am »
What is ImageViewer.Image? A TImage? In this case you can get proportional scaling done automatically by setting property "Proportional" to true.

Another comment on your code: Which procedure is calling this? If you are doing this outside a Paint routine you will have trouble because painting on a Canvas is not persistent in such a case (the OS may request your application to redraw the image but your routine will not notice it).

asdlinux

  • New Member
  • *
  • Posts: 17
Re: How to Scale a image propotionally?
« Reply #4 on: February 11, 2019, 02:16:31 pm »
ImageViewer.Image is a TImage, I set the propotional Property But Can't see any difference. Using The link from Handoko, I managed to get a better result but still Something is missing. Now I got this:
Code: Pascal  [Select][+][-]
  1.      if (ImageFile.Width>Screen.Width) or (ImageFile.Height>Screen.Height) then
  2.      begin
  3.        AspectImage:=ImageFile.Width div ImageFile.Height;
  4.        WProc:=round(Screen.Width div 100);
  5.        HProc:=round(Screen.Height div 100);
  6.        WScale:=Screen.Width div WProc;
  7.        HScale:=Screen.Height div HProc;
  8.        WScale:=WScale-85;
  9.        HScale:=HScale-87;
  10.        WScale:=WScale*WProc;
  11.        HScale:=HScale*HProc;
  12.        SW:=Round(Screen.Width-WScale);
  13.        SH:=Round(Screen.Height-HScale);
  14.        if ImageFile.Width>ImageFile.Height then
  15.        begin
  16.           SH:=Round(SH*AspectImage);
  17.        end else
  18.        Begin
  19.          SW:=Round(SW/AspectImage);
  20.        end;
  21.        R.Right:=SW;
  22.        R.Bottom:=SH;
  23.        ImageViewer.Width:=SW+6;
  24.        ImageViewer.Height:=SH+6;
  25.        ImageViewer.Image.Width:=SW;
  26.        ImageViewer.Image.Height:=SH;
  27.        ImageViewer.Image.Canvas.StretchDraw(R, ImageFile.Graphic);  
  28.      end;
  29.  

Any Hints on where i mess up? Maybe I have wrong logic in my thinking.

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: How to Scale a image propotionally?
« Reply #5 on: February 11, 2019, 03:11:53 pm »
I don't know why you do all this, subtracting constant values, etc. See at the attached demo to learn how scaled drawing works. It also shows that setting the Image's Proportional to true automatically scales the image without any lines of code. i don't know why it is not working for you.

asdlinux

  • New Member
  • *
  • Posts: 17
Re: How to Scale a image propotionally?
« Reply #6 on: February 11, 2019, 03:44:02 pm »
Thank you Very much, With the example I was able to solve it. Now the next problem is Look in the exif to see if it is landscape/Portrait mode and then rotate it to show correct. Is there any unit I can use to extract What mode it was taken with ? Or should I put that in a new question?

:)

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: How to Scale a image propotionally?
« Reply #7 on: February 11, 2019, 03:48:17 pm »
You should start a new thread. Because I don't know how and I am interested too.

 

TinyPortal © 2005-2018