Recent

Author Topic: BAGRABitmap load images proportions [SOLVED ]  (Read 2261 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 573
BAGRABitmap load images proportions [SOLVED ]
« on: June 22, 2024, 02:23:19 pm »
Hello, how can I load an image while maintaining its proportions to fit the window?
« Last Edit: June 26, 2024, 09:30:42 pm by Pe3s »

Handoko

  • Hero Member
  • *****
  • Posts: 5377
  • My goal: build my own game engine using Lazarus

Pe3s

  • Hero Member
  • *****
  • Posts: 573
Re: BAGRABitmap load images proportions
« Reply #2 on: June 23, 2024, 11:59:26 am »
 :) 
how to solve the problem, when scaling the window the image disappears, the invalidate and repaint instructions are not available

Roland57

  • Sr. Member
  • ****
  • Posts: 475
    • msegui.net
Re: BGRABitmap load images proportions
« Reply #3 on: June 24, 2024, 06:21:04 am »
Hello.

The same question was asked on the French-speaking Lazarus forum some days ago. I attach below the provided solution. Hope that you can adapt it to your needs.

Best regards.
« Last Edit: June 24, 2024, 06:26:22 am by Roland57 »
My projects are on Gitlab and on Codeberg.

Pe3s

  • Hero Member
  • *****
  • Posts: 573
Re: BAGRABitmap load images proportions
« Reply #4 on: June 24, 2024, 12:22:46 pm »
Hello, where did I make a mistake that the image is not displayed when it is opened?
Code: Pascal  [Select][+][-]
  1. private
  2.     image: TBGRABitmap;
  3.     imageLoaded: Boolean;
  4.  
  5.   public
  6.  
  7.   end;
  8.  
  9. var
  10.   Form1: TForm1;
  11.  
  12. implementation
  13.  
  14. {$R *.lfm}
  15.  
  16. { TForm1 }
  17.  
  18. procedure TForm1.FormDestroy(Sender: TObject);
  19. begin
  20.   image.Free;
  21. end;
  22.  
  23. procedure TForm1.FormCreate(Sender: TObject);
  24. begin
  25.   imageloaded := False;
  26.   image := TBGRABitmap.Create;
  27. end;
  28.  
  29. procedure TForm1.Panel1Paint(Sender: TObject);
  30. var
  31.   image2: TBGRABitmap;
  32.   hw, wh, w, h, x, y: Integer;
  33. begin
  34.   if not(imageLoaded) then Exit;
  35.  
  36.   hw := image.Height * Panel1.ClientWidth;
  37.   wh := image.Width * Panel1.ClientHeight;
  38.  
  39.   if wh > hw then
  40.   begin
  41.     w := Panel1.ClientWidth;
  42.     h := hw div image.Width;
  43.   end else
  44.   begin
  45.     h := Panel1.ClientHeight;
  46.     w := wh div image.Height;
  47.   end;
  48.   x := (Panel1.ClientWidth - w) div 2;
  49.   y := (Panel1.ClientHeight - h) div 2;
  50.  
  51.   image2 := image.Resample(w, h) as TBGRABitmap;
  52.   image2.Draw(Panel1.Canvas, x, y, True);
  53.   image2.Free;
  54. end;
  55.  
  56. procedure TForm1.ToolButton1Click(Sender: TObject);
  57. begin
  58.   if OpenPictureDialog1.Execute then
  59.   begin
  60.     image.LoadFromFile(OpenPictureDialog1.FileName);
  61.     imageLoaded:= True;
  62.   end;
  63. end;      
  64.  

Handoko

  • Hero Member
  • *****
  • Posts: 5377
  • My goal: build my own game engine using Lazarus
Re: BAGRABitmap load images proportions
« Reply #5 on: June 24, 2024, 02:23:29 pm »
Maybe you need to put this:

  Panel1.Invalidate;

between line #61 and #62.

Dzandaa

  • Sr. Member
  • ****
  • Posts: 392
  • From C# to Lazarus
Re: BAGRABitmap load images proportions
« Reply #6 on: June 25, 2024, 11:11:32 am »
Hi
@Pe3s

I don't know if this is what you need, but here is a template that create a resizable Form with BGRAPicture Load and save
with Zoom and Pan.

You need the BGRABitmap Pack.

https://wiki.freepascal.org/Project_Templates

Extract from the .zip and put the BGRAPicture in you Template Directory.

You'll have a new entry in File->New project from template->BGRA Picture Form
Fill the Dialog box with the name of your program, Form and Unit and select where you want to create your new program.

Feel free to modify.

B->
Regards,
Dzandaa

Pe3s

  • Hero Member
  • *****
  • Posts: 573
Re: BAGRABitmap load images proportions
« Reply #7 on: June 26, 2024, 09:30:21 pm »
Thank you  :) :)

 

TinyPortal © 2005-2018