Recent

Author Topic: image in Timage in the form opening  (Read 209 times)

ReinaldoSergioPTBR

  • New Member
  • *
  • Posts: 10
image in Timage in the form opening
« on: August 14, 2019, 04:49:36 pm »
I am trying to place an image in Timage in the form opening, as the code below:

procedure TfArrastarSoltar.FormShow(Sender: TObject);
Begin
  img.Picture.LoadFromFile('edit');
End;

It's just giving the following error:
Unable to open file ''edit''

can anyone solve

winni

  • Hero Member
  • *****
  • Posts: 562
Re: image in Timage in the form opening
« Reply #1 on: August 14, 2019, 05:32:39 pm »
Hi!

First you should set the path to your file correct:

/your/path/to/file/example.png  (linux) or
Y:\your\path\to\file\exmple.png (window)

Next: every graphic file has an ending like .png, .jpg, .tif ....
Often this endings are used to identify the type of file.

And in case of linux of upper and lower case letter: "Edit" is different from "edit"

And last: you should not put the loading of the file in FormShow:

If you minimise your app and put it again into the normal size, the file is loaded again.

Put it in FormCreate or FormActivate.

Winni



« Last Edit: August 14, 2019, 07:18:46 pm by winni »

ReinaldoSergioPTBR

  • New Member
  • *
  • Posts: 10
Re: image in Timage in the form opening
« Reply #2 on: August 14, 2019, 07:46:51 pm »
I did, thank you very much for the tips

RAW

  • Hero Member
  • *****
  • Posts: 794
Re: image in Timage in the form opening
« Reply #3 on: August 14, 2019, 08:31:50 pm »
Something like this ?
Code: Pascal  [Select]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3.  
  4. interface
  5.  
  6. uses
  7.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Image1 : TImage;
  12.     Button1: TButton;
  13.     Edit1  : TEdit;
  14.     procedure Button1Click (Sender: TObject);
  15.    private
  16.   end;
  17.  
  18. var
  19.   Form1: TForm1;
  20.  
  21. implementation
  22. {$R *.lfm}
  23.  
  24. procedure TForm1.Button1Click(Sender: TObject);
  25. var
  26.  strEdit: String;
  27. begin
  28.   strEdit:= Edit1.Text;
  29.   If FileExists(strEdit)
  30.   Then Image1.Picture.LoadFromFile(strEdit);
  31. end;
  32.  
  33. end.
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.4 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1