各位大俠, 我想在程式啟動時,讀取上次運行時保存的bmp影像的路徑,再顯示出來。但總是
出現例外。 連 IDE 也崩潰了。 代碼如下,不知哪里錯了。 謝謝!
var
Form1: TForm1;
astr:Tstringlist;
picname:string;
procedure TForm1.FormCreate(Sender: TObject);
begin
astr:=Tstringlist.Create;
if not FileExists(extractFilePath(application.exeName)+ 'pic.txt') then exit;
astr.LoadFromFile('pic.txt');
panel1.Caption:= astr.Text;
picname:= astr.Text;
try
image1.Picture.LoadFromFile(astr.Text);
finally
showmessage(astr.Text);
end;
end;
運行到
image1.Picture.LoadFromFile(astr.Text); 就錯。 EInvalidgraphformat 暈
uj5u.com熱心網友回復:
代碼不是一般的爛小伙子好好加油!
錯誤串列:
1. extractFilePath(application.exeName)+ 'pic.txt') 不對
extractFilePath(ParamStr(0))+ 'pic.txt' 才對
2. image1.Picture.LoadFromFile(astr.Text);
哥, astr是一個文本list, 有可能不止一行吧?
image1.Picture.LoadFromFile一次是不是只能匯入一副圖片, 你這樣寫要哪樣?
image1.Picture.LoadFromFile(astr[0]); 這樣是匯入第一副圖片
uj5u.com熱心網友回復:
你的代碼中,有兩個主要錯誤1、指明全路徑檔案名錯誤:
extractFilePath(application.exeName)+ 'pic.txt' 缺少“\”
改為:extractFilePath(application.exeName)+ '\pic.txt'
2、檔案名存放在pic.txt檔案中,讀入aStr后,aStr.text含有非路徑字符
應該這樣,讀取首行檔案名:picname:= aStr.Strings[0];
修改代碼如下,試試:
procedure TForm1.FormCreate(Sender: TObject);
var aStr:TStringList;
FileName:string;
begin
aStr:=TStringlist.Create;
FileName:=extractFilePath(application.exeName)+ '\pic.txt';
if not FileExists(FileName) then exit;
aStr.LoadFromFile(FileName);
panel1.Caption:= astr.Text;
picname:= aStr.Strings[0];
try
image1.Picture.LoadFromFile(picname);
finally
showmessage(aStr.Text);
end;
end;
uj5u.com熱心網友回復:
報謙剛才回帖有錯。改下如下:1、指明全路徑檔案名 ExtractFilePath(application.exeName)+ 'pic.txt' 對的
2、在檔案名存放在pic.txt檔案中,讀入aStr后,aStr.text含有非路徑字符,你的錯就是將pic.txt當成檔案名了。
pic.txt檔案中,可以保存很多檔案名,一行可以保存一個檔案名。
讀取首行檔案名:picname:= aStr.Strings[0];
修改代碼如下,試試:
procedure TForm1.FormCreate(Sender: TObject);
var aStr:TStringList;
FileName:string;
begin
aStr:=TStringlist.Create;
FileName:=extractFilePath(application.exeName)+ 'pic.txt';
if not FileExists(FileName) then exit;
aStr.LoadFromFile(FileName);
panel1.Caption:= astr.Text;
picname:= aStr.Strings[0];
try
image1.Picture.LoadFromFile(picname);
finally
showmessage(aStr.Text);
end;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/81153.html
標籤:語言基礎/算法/系統設計
上一篇:delphi資料庫連接多次會報錯
