2014-02-19 61 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

相關問題