我有一个 TImage,它显示文件夹中的 JPG 图像。每个 JPG 图像名称都是数字(例如 5968.jpg)。如果 JPG 图像不存在,我当前加载模板 JPG。
但是,我想在检查本地文件夹后再检查互联网
我知道 URL,我只需将图像编号和扩展名连接起来(例如“http://www.myurl.com/images/”+ Tags.Names[Index] +“.jpg”)
如果本地不存在该图像,我想下载该图像。
if FileExists(TagPath + Tags.Names[Index] + '.jpg') then
imgTag.Picture.LoadFromFile(TagPath + Tags.Names[Index] + '.jpg')
else
imgTag.Picture.LoadFromFile(ExtractFilePath(Application.ExeName) + 'Template.jpg');
如何将图像(使用 idHTTP?)下载到 TImage (imgTag)。我将如何处理任何异常。
流程应该是这样,没有任何错误。
if FileExists locally, load image
else
if fileEists on the internet load Image
else
Load the template
谢谢
最佳答案
您可以使用Indy的TIdHTTP
组件,例如:
uses
..., IdHTTP;
var
FileName: string;
Strm: TMemoryStream;
begin
FileName := TagPath + Tags.Names[Index] + '.jpg';
if not FileExists(FileName) then
begin
try
Strm := TMemoryStream.Create;
try
HTTP := TIdHTTP.Create(nil);
try
HTTP.Get('http://www.myurl.com/images/' + Tags.Names[Index] + '.jpg', Strm);
finally
HTTP.Free;
end;
Strm.Position := 0;
Strm.SaveToFile(FileName);
finally
Strm.Free;
end;
except
FileName := ExtractFilePath(Application.ExeName) + 'Template.jpg';
end;
end;
imgTag.Picture.LoadFromFile(FileName);
end;
关于delphi - 从互联网下载 jpg,没有错误,否则加载 template.jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15375201/