分配时出现JPEG错误#42 [英] JPEG Error #42 on assign
本文介绍了分配时出现JPEG错误#42的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么不能直接将MemoryStream分配给图片?
下面我发布两种将MemoryStream分配给TImage的方法。方法1不起作用,方法2有效。为什么?
谢谢
Sam
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
方法1:此方法返回JPEG错误#42
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;
方法2:此方法有效
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;
推荐答案
在<$之后,您不会重置内存流c $ c> IdFTP.Get 调用。这意味着 LoadFromStream
调用收到0个字节,因此出现#42错误:
You are not resetting the memory stream after the IdFTP.Get
call. This means that the LoadFromStream
call receives 0 bytes and hence the #42 error:
var
ms1 : TMemoryStream;
J : TJPEGImage;
St : String;
begin
try
ms1 := TMemoryStream.Create;
try
St := 'somepath';
IdFTP1.Get(St, ms1);
if ms1.Size > 0 then
begin
ms1.Position := 0; // <-- add this
J := TJPEGImage.Create;
try
J.LoadFromStream(ms1);
Image4.Picture.Graphic := J;
finally
J.Free;
end;
end;
finally
ms1.Free;
end;
except
on E: Exception do
ShowMessage(E.Message);
end;
end;
这篇关于分配时出现JPEG错误#42的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文