Home > Back-end >  Image1. Picture. LoadFromFile problem
Image1. Picture. LoadFromFile problem

Time:09-26

Each master, I want to application starts, read the last run time save the path of the BMP image, displayed again, but it is always
Abnormal, even collapse of the IDE, the code is as follows, I don't know where is wrong, thank you!


Var
Form1: TForm1;
Astr: Tstringlist;
Picname: string;



Procedure TForm1. FormCreate (Sender: TObject);
The begin
Astr:=Tstringlist. Create;
If not FileExists (extractFilePath (application. ExeName) + 'PIC. TXT') then the exit;


Astr. LoadFromFile (' PIC. TXT);

Panel1. Caption:=astr. Text;

Picname:=astr. Text;

Try
Image1. Picture. LoadFromFile (astr. Text);

The finally
Showmessage (astr. Text);
The end;

The end;

Run to the
Image1. Picture. LoadFromFile (astr. Text); Is wrong, EInvalidgraphformat dizzy

CodePudding user response:

Code is not the general bad
Young man good refueling!
Error list:
1. ExtractFilePath (application. ExeName) + 'PIC. TXT') not
ExtractFilePath (ParamStr (0)) + 'PIC. TXT' to

2. Image1. Picture. LoadFromFile (astr. Text);
Elder brother, astr is a text list, probably more than one line?
Image1. Picture. LoadFromFile is a can only import a Picture, you wrote to which?
Image1. Picture. LoadFromFile (astr [0]). This is to import the first picture

CodePudding user response:

Your code, there are two main error
1, specify the full path to the file name error:
ExtractFilePath (application. ExeName) + 'PIC. TXT' lack of ""
To: extractFilePath (application. ExeName) + '\ PIC. TXT'

2, the name of the file is stored in the PIC. TXT file, read after aStr aStr. The text contains the path characters
First line should be read in this way, the file name: picname:=aStr. Strings [0].

Modify the code as follows, try:
 
Procedure TForm1. FormCreate (Sender: TObject);
Var aStr: TStringList;
FileName: the string;
The begin
AStr:=TStringlist. Create;
FileName:=extractFilePath (application. ExeName) + '\ PIC. TXT';
If not FileExists (FileName) then the exit;
AStr. LoadFromFile (FileName);
Panel1. Caption:=astr. Text;
Picname:=aStr Strings [0];
Try
Image1. Picture. LoadFromFile (picname);
The finally
Showmessage (aStr. Text);
The end;
The end;

CodePudding user response:

The modest just reply is wrong, modification is as follows:


1, specify the full path to the file name ExtractFilePath (application. ExeName) + 'PIC. TXT' right


2, in the name of the file stored in PIC. TXT file, read after aStr aStr. The text contains the path to the characters, your fault is the PIC. TXT as file name,
PIC. TXT file, can save a lot of file name, a line can save a file name,

Read the first line of the file name: picname:=aStr Strings [0].

Modify the code as follows, try:
 
Procedure TForm1. FormCreate (Sender: TObject);
Var aStr: TStringList;
FileName: the string;
The begin
AStr:=TStringlist. Create;
FileName:=extractFilePath (application. ExeName) + 'PIC. TXT';
If not FileExists (FileName) then the exit;
AStr. LoadFromFile (FileName);
Panel1. Caption:=astr. Text;
Picname:=aStr Strings [0];
Try
Image1. Picture. LoadFromFile (picname);
The finally
Showmessage (aStr. Text);
The end;
The end;
  • Related