Recent

Author Topic: TStringList image view[SOLVED]  (Read 2678 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 573
TStringList image view[SOLVED]
« on: June 17, 2024, 11:16:22 am »
Hello, how can I improve the code so that it loads the next and previous images after clicking the buttons?
Regards
« Last Edit: June 24, 2024, 12:04:14 pm by Pe3s »

paweld

  • Hero Member
  • *****
  • Posts: 1268
Re: TStringList image view
« Reply #1 on: June 17, 2024, 12:14:23 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.    FImages := TStringList.Create;
  4. end;
  5.  
  6. procedure TForm1.FormDestroy(Sender: TObject);
  7. begin
  8.   FImages.Free;
  9. end;
  10.  
  11. procedure TForm1.SpeedButton1Click(Sender: TObject);
  12. begin
  13.   if OpenPictureDialog1.Execute then
  14.   begin
  15.     FImages.Clear;  //clear images list
  16.     try
  17.       FindAllFiles(FImages, ExtractFileDir(OpenPictureDialog1.FileName) + PathDelim, '*.jpg; *.jpeg', True);
  18.       Form1.Caption := IntToStr(FImages.IndexOf(OpenPictureDialog1.FileName)) +' / '+ IntToStr(FImages.Count);
  19.       //Image1.Picture.LoadFromFile(FImages[FImages.IndexOf(OpenPictureDialog1.FileName)]);
  20.       FIIndex := FImages.IndexOf(OpenPictureDialog1.FileName);
  21.       if FIIndex < 0 then  //if the selected file is not found (is not JPG) then select the first one in the list
  22.         FIIndex := 0;
  23.  
  24.       NextImage;
  25.     finally
  26.  
  27.     end;
  28.   end;
  29. end;
  30.  
  31. procedure TForm1.ShowImage(Sender: TObject);
  32.  var
  33.   ImageIndex: integer;
  34. begin
  35.   if FImages.Count = 0 then //protect from an empty list
  36.     exit;
  37.   ImageIndex := FIIndex;
  38.   FIIndex := (FIIndex + 1) mod FImages.Count;
  39.   Image1.Picture.LoadFromFile(FImages[ImageIndex]);
  40. end;      
  41.  
Best regards / Pozdrawiam
paweld

Pe3s

  • Hero Member
  • *****
  • Posts: 573
Re: TStringList image view
« Reply #2 on: June 17, 2024, 05:24:51 pm »
Thank you  :)

Pe3s

  • Hero Member
  • *****
  • Posts: 573
Re: TStringList image view
« Reply #3 on: June 20, 2024, 09:52:01 am »
Hello, what do I need to correct to make the program count the number of images correctly, to count from 1 and not from zero?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   FileUtil, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls,
  9.   ExtCtrls, Buttons, ExtDlgs, GR32_Image;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     ImageList1: TImageList;
  17.     ImgView32_1: TImgView32;
  18.     OpenPictureDialog1: TOpenPictureDialog;
  19.     StatusBar1: TStatusBar;
  20.     ToolBar1: TToolBar;
  21.     ToolButton1: TToolButton;
  22.     ToolButton2: TToolButton;
  23.     ToolButton3: TToolButton;
  24.     ToolButton4: TToolButton;
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure FormDestroy(Sender: TObject);
  27.     procedure ToolButton1Click(Sender: TObject);
  28.     procedure ToolButton3Click(Sender: TObject);
  29.     procedure ToolButton4Click(Sender: TObject);
  30.   private
  31.     FImages: TStringList;
  32.     FCurrentIndex: Integer;
  33.     procedure LoadImage(const APrev: Boolean);
  34.  
  35.   public
  36.  
  37.   end;
  38.  
  39. var
  40.   Form1: TForm1;
  41.  
  42. implementation
  43.  
  44. {$R *.lfm}
  45.  
  46. { TForm1 }
  47.  
  48. procedure TForm1.FormCreate(Sender: TObject);
  49. begin
  50.   FImages := TStringList.Create;
  51. end;
  52.  
  53. procedure TForm1.FormDestroy(Sender: TObject);
  54. begin
  55.   FImages.Free;
  56. end;
  57.  
  58. procedure TForm1.ToolButton1Click(Sender: TObject);
  59. begin
  60.   if OpenPictureDialog1.Execute then
  61.   begin
  62.       FImages.Clear;
  63.       FindAllFiles(FImages, ExtractFileDir(OpenPictureDialog1.FileName) + PathDelim, '*.jpg; *.jpeg', True);
  64.       FCurrentIndex := FImages.IndexOf(OpenPictureDialog1.FileName);
  65.  
  66.      LoadImage(False);
  67.   end;
  68. end;
  69.  
  70. procedure TForm1.ToolButton3Click(Sender: TObject);
  71. begin
  72.   LoadImage(True);
  73. end;
  74.  
  75. procedure TForm1.ToolButton4Click(Sender: TObject);
  76. begin
  77.   LoadImage(False);
  78. end;
  79.  
  80. procedure TForm1.LoadImage(const APrev: Boolean);
  81. var
  82.   ImageIndex: Integer;
  83. begin
  84.   ImageIndex := FCurrentIndex;
  85.  
  86.   if APrev then
  87.   begin
  88.      if FCurrentIndex > 0 then
  89.     Dec(FCurrentIndex)
  90.   end else
  91.   begin
  92.     if FCurrentIndex < FImages.Count -1 then
  93.     Inc(FCurrentIndex);
  94.   end;
  95.  
  96.   ImgView32_1.Bitmap.LoadFromFile(FImages[ImageIndex]);
  97.  
  98.   Form1.Caption := ExtractFileName(FImages[ImageIndex]);
  99.   StatusBar1.Panels[0].Text := IntToStr(ImgView32_1.Bitmap.Width) +'x'+ IntToStr(ImgView32_1.Bitmap.Height);
  100.   StatusBar1.Panels[1].Text := IntToStr(FCurrentIndex) +' / '+ IntToStr(FImages.Count);
  101. end;
  102.  
  103. end.
  104.  
  105.  

alpine

  • Hero Member
  • *****
  • Posts: 1289
Re: TStringList image view
« Reply #4 on: June 20, 2024, 10:15:13 am »
Code: Pascal  [Select][+][-]
  1. StatusBar1.Panels[1].Text := IntToStr(FCurrentIndex + 1) +' / '+ IntToStr(FImages.Count);
?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Pe3s

  • Hero Member
  • *****
  • Posts: 573
Re: TStringList image view
« Reply #5 on: June 20, 2024, 10:33:22 am »
Now he counts from 2

alpine

  • Hero Member
  • *****
  • Posts: 1289
Re: TStringList image view
« Reply #6 on: June 20, 2024, 10:48:36 am »
Now he counts from 2
It does not.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

paweld

  • Hero Member
  • *****
  • Posts: 1268
Re: TStringList image view
« Reply #7 on: June 20, 2024, 02:11:40 pm »
@alpine provided the correct code, but it is misplaced in your procedure code. Since you immediately change the current index when you press the button, you need to place this line of code at the beginning of the procedure.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.LoadImage(const APrev: Boolean);
  2. var
  3.   ImageIndex: Integer;
  4. begin
  5.   ImageIndex := FCurrentIndex;
  6.   StatusBar1.Panels[1].Text := IntToStr(FCurrentIndex + 1) +' / '+ IntToStr(FImages.Count);
  7.  
  8.   if APrev then
  9.   begin
  10.      if FCurrentIndex > 0 then
  11.     Dec(FCurrentIndex)
  12.   end else
  13.   begin
  14.     if FCurrentIndex < FImages.Count -1 then
  15.     Inc(FCurrentIndex);
  16.   end;
  17.  
  18.   ImgView32_1.Bitmap.LoadFromFile(FImages[ImageIndex]);
  19.  
  20.   Form1.Caption := ExtractFileName(FImages[ImageIndex]);
  21.   StatusBar1.Panels[0].Text := IntToStr(ImgView32_1.Bitmap.Width) +'x'+ IntToStr(ImgView32_1.Bitmap.Height);
  22. end;
Best regards / Pozdrawiam
paweld

alpine

  • Hero Member
  • *****
  • Posts: 1289
Re: TStringList image view
« Reply #8 on: June 20, 2024, 03:05:25 pm »
And I bet there will be a little surprise when the direction is changed  8)

Edit: To be a bit more clear - modifying FCurrentIndex after the loading of the image file will have the (unexpected) effect of showing the result of the previous movement.
« Last Edit: June 20, 2024, 05:43:23 pm by alpine »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Pe3s

  • Hero Member
  • *****
  • Posts: 573
Re: TStringList image view
« Reply #9 on: June 20, 2024, 09:45:28 pm »
exactly the other way around doesn't work :)

alpine

  • Hero Member
  • *****
  • Posts: 1289
Re: TStringList image view
« Reply #10 on: June 21, 2024, 10:00:44 am »
exactly the other way around doesn't work :)
I don't know what do you mean by "the other way around", perhaps it is changing FCurrentIndex before the loading, but actually that's the way it should work if you don't want your current picture to lag one step behind.
One simple way to do it:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.LoadImage(const AInc: Integer);
  2. var
  3.   ImageIndex: Integer;
  4. begin
  5.   ImageIndex := FCurrentIndex + AInc;
  6.   if (ImageIndex < 0) or (ImageIndex >= FImages.Count) then
  7.     Exit;
  8.   ImgView32_1.Bitmap.LoadFromFile(FImages[ImageIndex]);
  9.   Form1.Caption := ExtractFileName(FImages[ImageIndex]);
  10.   StatusBar1.Panels[0].Text := IntToStr(ImgView32_1.Bitmap.Width) +'x'+ IntToStr(ImgView32_1.Bitmap.Height);
  11.   StatusBar1.Panels[1].Text := IntToStr(ImageIndex + 1) +' / '+ IntToStr(FImages.Count);
  12.   FCurrentIndex := ImageIndex;
  13. end;

Then call LoadImage(0) on initial loading, call LoadImage(1) when 'Next' clicked and call LoadImage(-1) when 'Prev' clicked.

Another way would be not to modify FCurrentIndex into the LoadImage(), i.e. just load and show texts, and to do incrementing/decrementing into Next/Prev handlers.

It's pretty trivial and I don't know why you expect someone to do it for you.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Pe3s

  • Hero Member
  • *****
  • Posts: 573
Re: TStringList image view
« Reply #11 on: June 24, 2024, 12:04:00 pm »
Thank you

 

TinyPortal © 2005-2018