Load image from URL in Firemonkey

  

I have this construction:

image.bitmap.Assign(nil);

TTask.Run(procedure
begin
TThread.Synchronize(nil, procedure
begin
label_loader.visible:=true;
end);

ReadImageFromWeb(image,url); //load image from URL

TThread.Synchronize(nil, procedure
begin
label_loader.visible:= false;
end);
end);

procedure ReadImageFromWeb(image:TImage;url: string);
var
memStream : TMemoryStream;
IdHTTP: TIdHTTP;
begin
IdHTTP := TIdHTTP.Create;
memStream := TMemoryStream.Create;

try
IdHTTP.Request.UserAgent := ‘Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0’;
IdHTTP.Request.Connection := ‘keep-alive’;
IdHTTP.Request.ContentType := ‘application/x-www-form-urlencoded’;
IdHTTP.Request.Charset := ‘utf-8’;
IdHTTP.Request.Pragma := ‘no-cache’;

IdHTTP.Get(url,memStream);
except
ShowMessage(‘Image not found’);
memStream.Free;
IdHTTP.Free;
exit;
end;
try
memStream.Position:=0;
image.Bitmap.LoadFromStream(memStream);
finally
memStream.Free;
IdHTTP.Free;
end;
end;

Every now and again, despite the fact that the picture has been downloaded, it is not displayed in the TImage object.
This only happens if procedure ReadImageFromWeb() is under construction with TTask. If I delete TTask, there is no such problem.

Can anyone help me?

Comments are closed.