delphi 从网络获取图像并使用Synapse显示

pvcm50d1  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(201)

我想从Web上获取一个图像并将其显示在TImage上(不保存)。下面的代码产生了一个错误:

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

项目-------引发了异常类“EReadError”,消息为:流读取错误
这是错误指向的Synapse库(位于www.example.com)中的函数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;
7d7tgy0s

7d7tgy0s1#

您必须将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;

相关问题