Why can't I assign directly a MemoryStream to a Picture? Below I post two methods of assign a MemoryStream to a TImage. The Method1 doen't work and Method2 works. Why? Thanks Sam
Method1 : This method returns an JPEG error #42
Var
ms1 : TMemoryStream;
J : TJPEGImage;
St : String;
begin
ms1 := TMemoryStream.Create;
try
try
St := 'somepath';
IdFTP1.Get(St, ms1);
if ms1.Size > 0 then
Begin
J := TJPEGImage.Create;
try
J.LoadFromStream(ms1);
Image4.Picture := nil;
Image4.Picture.Assign(J); // here, I got an error #42 JPEG
finally
J.Free;
end;
End;
except
on e:exception do ShowMessage(e.message);
end;
finally
ms1.Free;
End;
End;
end;
Method2 : This Method works
Var
ms1, ms2 : TMemoryStream;
J : TJPEGImage;
St : String;
begin
ms1 := TMemoryStream.Create;
ms2 := TMemoryStream.Create;
try
try
IdFTP1.Get(somepath, ms1);
if ms1.Size > 0 then
Begin
J := TJPEGImage.Create;
try
J.LoadFromStream(ms1);
ms1.SaveToFile('lixofoto.jpg');
ms2.LoadFromFile('lixofoto.jpg');
J.LoadFromStream(ms2);
ImgProd.Picture.Assign(J);
DeleteFile('lixofoto.jpg');
finally
J.Free;
end;
End;
except
end;
finally
ms1.Free;
ms2.Free;
End;