Recent

Author Topic: [SOLVED]Resizing TImage from middle?  (Read 307 times)

osman.schltz

  • New member
  • *
  • Posts: 45
[SOLVED]Resizing TImage from middle?
« on: December 04, 2018, 02:01:50 pm »
Hi there!
I'm trying to make a 2D car game. But I'm stuck at one point. How can I resize the TImage from the middle. I mean, if i want to resize the TImage, I use
Code: Pascal  [Select]
  1. TImage.Width
Code: Pascal  [Select]
  1. TImage.Height
But that resizes the TImage to one side, how can I resize the TImage to both sides at the same time?  %)
« Last Edit: December 06, 2018, 05:05:33 pm by osman.schltz »
Windows 8.1
Linux Ubuntu 14.04 LTS

GetMem

  • Hero Member
  • *****
  • Posts: 3251
Re: Resizing TImage from middle?
« Reply #1 on: December 04, 2018, 02:11:58 pm »
By moving the Left, Top in oposite direction? See attached project.
« Last Edit: December 04, 2018, 02:28:12 pm by GetMem »

Segator

  • New member
  • *
  • Posts: 36
Re: Resizing TImage from middle?
« Reply #2 on: December 04, 2018, 02:13:39 pm »
Hi osman.schltz, you must to set the Strech and autosize to false of the TImage:

Code: Pascal  [Select]
  1. TImage.Strech:=false;
  2. TImage.Autosize:=false;
  3. TImage.Width:=your_size;
  4. TImage.Height:=your_size;
  5.  

Segator

  • New member
  • *
  • Posts: 36
Re: Resizing TImage from middle?
« Reply #3 on: December 04, 2018, 02:17:36 pm »

Code: Pascal  [Select]
  1. const
  2.        isize=100;
  3. begin
  4.   TImage.Strech:=false;
  5.   TImage.Autosize:=false;
  6.   TImage.Width:=isize;
  7.   TImage.Heoght:=isize;
  8.   TImage.Top:=isize div 2;
  9.   TImage.Left:=isize div 2;
  10. end;
  11.  
« Last Edit: December 04, 2018, 02:49:13 pm by Segator »

Handoko

  • Hero Member
  • *****
  • Posts: 2675
  • My goal: build my own game engine using Lazarus
Re: Resizing TImage from middle?
« Reply #4 on: December 04, 2018, 02:38:35 pm »
Try my code, it works:

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, Graphics, ExtCtrls,
  9.   ComCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Image1: TImage;
  17.     TrackBar1: TTrackBar;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure TrackBar1Change(Sender: TObject);
  20.   private
  21.     CenterX, CenterY, SizeX, SizeY: Integer;
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.TrackBar1Change(Sender: TObject);
  34. var
  35.   NewSizeX, NewSizeY: Integer;
  36. begin
  37.   // TrackBar.Position = percentage of the original size
  38.  
  39.   NewSizeX      := Round(TrackBar1.Position / 100 * SizeX);
  40.   NewSizeY      := Round(TrackBar1.Position / 100 * SizeY);
  41.   Image1.Left   := CenterX - (NewSizeX div 2);
  42.   Image1.Top    := CenterY - (NewSizeY div 2);
  43.   Image1.Width  := NewSizeX;
  44.   Image1.Height := NewSizeY;
  45. end;
  46.  
  47. procedure TForm1.FormCreate(Sender: TObject);
  48. begin
  49.   Image1.Stretch := True;
  50.  
  51.   SizeX   := Image1.Width;
  52.   SizeY   := Image1.Height;
  53.   CenterX := Image1.Left + (SizeX div 2);
  54.   CenterY := Image1.Top  + (SizeY div 2);
  55. end;
  56.  
  57. end.
« Last Edit: December 04, 2018, 06:38:39 pm by Handoko »

Handoko

  • Hero Member
  • *****
  • Posts: 2675
  • My goal: build my own game engine using Lazarus
Re: Resizing TImage from middle?
« Reply #5 on: December 04, 2018, 02:43:23 pm »
Just tested GetMem's code. It did not work correctly at least on my Lazarus 1.8.4 Linux GTK2.

-edit-
Actually, it works. But not proportionally on the size of both sides.
« Last Edit: December 04, 2018, 02:47:25 pm by Handoko »

osman.schltz

  • New member
  • *
  • Posts: 45
Re: Resizing TImage from middle?
« Reply #6 on: December 04, 2018, 10:30:41 pm »
Try my code, it works:

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, Graphics, ExtCtrls,
  9.   ComCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Image1: TImage;
  17.     TrackBar1: TTrackBar;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure TrackBar1Change(Sender: TObject);
  20.   private
  21.     CenterX, CenterY, SizeX, SizeY: Integer;
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.TrackBar1Change(Sender: TObject);
  34. var
  35.   NewSizeX, NewSizeY: Integer;
  36. begin
  37.   // TrackBar.Position = percentage of the original size
  38.  
  39.   NewSizeX      := Round(TrackBar1.Position / 100 * SizeX);
  40.   NewSizeY      := Round(TrackBar1.Position / 100 * SizeY);
  41.   Image1.Left   := CenterX - (NewSizeX div 2);
  42.   Image1.Top    := CenterY - (NewSizeY div 2);
  43.   Image1.Width  := NewSizeX;
  44.   Image1.Height := NewSizeY;
  45. end;
  46.  
  47. procedure TForm1.FormCreate(Sender: TObject);
  48. begin
  49.   Image1.Stretch := True;
  50.  
  51.   SizeX   := Image1.Width;
  52.   SizeY   := Image1.Height;
  53.   CenterX := Image1.Left + (SizeX div 2);
  54.   CenterY := Image1.Top  + (SizeY div 2);
  55. end;
  56.  
  57. end.
Thanks y'all for help! I'll use Handoko's and Segator's examples because GetMem's code is not cross-platform and I'll come back with results as soon as possible  :)
Windows 8.1
Linux Ubuntu 14.04 LTS

osman.schltz

  • New member
  • *
  • Posts: 45
Re: Resizing TImage from middle?
« Reply #7 on: December 06, 2018, 05:05:01 pm »
Try my code, it works:

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, Graphics, ExtCtrls,
  9.   ComCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Image1: TImage;
  17.     TrackBar1: TTrackBar;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure TrackBar1Change(Sender: TObject);
  20.   private
  21.     CenterX, CenterY, SizeX, SizeY: Integer;
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.TrackBar1Change(Sender: TObject);
  34. var
  35.   NewSizeX, NewSizeY: Integer;
  36. begin
  37.   // TrackBar.Position = percentage of the original size
  38.  
  39.   NewSizeX      := Round(TrackBar1.Position / 100 * SizeX);
  40.   NewSizeY      := Round(TrackBar1.Position / 100 * SizeY);
  41.   Image1.Left   := CenterX - (NewSizeX div 2);
  42.   Image1.Top    := CenterY - (NewSizeY div 2);
  43.   Image1.Width  := NewSizeX;
  44.   Image1.Height := NewSizeY;
  45. end;
  46.  
  47. procedure TForm1.FormCreate(Sender: TObject);
  48. begin
  49.   Image1.Stretch := True;
  50.  
  51.   SizeX   := Image1.Width;
  52.   SizeY   := Image1.Height;
  53.   CenterX := Image1.Left + (SizeX div 2);
  54.   CenterY := Image1.Top  + (SizeY div 2);
  55. end;
  56.  
  57. end.

Thanks everyone for help! Handoko's code works well.
Windows 8.1
Linux Ubuntu 14.04 LTS