Delphi 从网络上获取图像并使用Synapse显示
我想把一张图片从网络上传到流中(不保存)并显示在TImage上。 以下代码产生错误:Delphi 从网络上获取图像并使用Synapse显示,delphi,lazarus,synapse,fpc,Delphi,Lazarus,Synapse,Fpc,我想把一张图片从网络上传到流中(不保存)并显示在TImage上。 以下代码产生错误: response := TMemoryStream.Create; try HttpGetBinary('http://www.example-url/example_image.jpg', response); Image.Picture.LoadFromStream(response); finally response.Free; end; 项目------引发异常类“EReadErr
response := TMemoryStream.Create;
try
HttpGetBinary('http://www.example-url/example_image.jpg', response);
Image.Picture.LoadFromStream(response);
finally
response.Free;
end;
项目------引发异常类“EReadError”,并显示消息:
流读取错误
这是Synapse library(在picture.inc中)中错误指向的函数:
function TPicFileFormatsList.FindByStreamFormat(Stream: TStream): TGraphicClass;
var
I: Integer;
begin
for I := Count - 1 downto 0 do
begin
Result := GetFormats(I)^.GraphicClass;
if Result.IsStreamFormatSupported(Stream) then // <<<<<< this is the error line
Exit;
end;
Result := nil;
end;
函数TPicFileFormatsList.FindByStreamFormat(Stream:TStream):tgraphic类;
变量
I:整数;
开始
对于I:=计数-1到0
开始
结果:=GetFormats(I)^.GraphicClass;
如果Result.IsStreamFormatSupported(Stream),那么//您必须在项目中的某个地方包含JPEGLib
单元,以便注册JPEG图形类
uses
JPEGLib, // to support JPEG
PNGcomn, // to support PNG
httpsend;
response := TMemoryStream.Create;
try
if HttpGetBinary('http://www.example-url/example_image.jpg', response) then
begin
response.Seek( 0, soFromBeginning );
Image.Picture.LoadFromStream( response );
end;
finally
response.Free;
end;
将流位置设置为开始位置,然后再将其加载到图像中。Likeresponse.Position:=0;Image.Picture.LoadFromStream…
我在这里发布之前就尝试过了。结果是一样的。我手上没有FPC,但我认为如果TImage.Picture
有LoadFromStream
方法,应该有机会直接从stream加载它。失败的是TPicFileFormatsList.FindByStreamFormat
方法,所以问题不可能是(重置流位置除外)缺少注册JPEG格式的单元吗?这只是一个猜测…+1@TLama你完全正确。。。FPC在这一点上与Delphi不同:o)这是有效的。JPEGLib和PNGcomn不是必需的,调试器会发出消息,说明我的单元中没有使用它们。我应该提到我使用FPC而不是Delphi:)。谢谢。好的,唯一的问题是你没有重置流位置?您告诉我,您尝试了该操作,得到了相同的结果…此:response.Position:=0无效。这是:response.Seek(0,sofromBeging)工作正常。不知道我承认我对stream不是很熟悉。