2014-02-19 60 views
1

我想从网络获取图像并将其显示为流(不保存)并在TImage上显示。 下面的代码产生一个错误:使用Synapse从网络获取图像并进行显示

response := TMemoryStream.Create; 
try 
    HttpGetBinary('http://www.example-url/example_image.jpg', response); 
    Image.Picture.LoadFromStream(response); 
finally 
    response.Free; 
end; 

项目-------引发的异常类“EReadError”与消息: 流读取错误

这是在突触库中的功能(在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; 
+0

将流位置设置为开头,然后将其加载到图像上。像'response.Position:= 0; Image.Picture.LoadFromStream ...' – TLama

+0

我在这里发布之前试过。结果是一样的。 – user3327194

回答

1

你必须从某个地方包括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; 
+1

我手边没有FPC,但我认为如果'TImage.Picture'有'LoadFromStream'方法,应该有机会直接从流中加载它。这是'TPicFileFormatsList.FindByStreamFormat'方法失败,所以在注册JPEG格式的缺失单元中不能解决问题(除了重置流位置)?这只是一个疯狂的猜测... – TLama

+1

+1 @TLama你是完全正确的...... FPC与Delphi的不同之处在于:o) –

+0

这是有效的。不需要使用JPEGLib和PNGcomn,调试器会提供消息,说明它们未在我的设备中使用。我应该提到我使用FPC而不是Delphi :)。谢谢。 – user3327194

相关问题