如何将 URL 中的图像添加到列表框
how to add images from URL to ListBox
我想从列表框项目的 URL 列表中加载图像,这是我检索 URLs
的代码
var
LJSONArray : TJSONArray;
LEntity: TBackendEntityValue;
I : integer;
begin
try
LJSONArray := TJSONArray.Create;
BackendStorage1.Storage.QueryObjects('list', [], LJSONArray);
for I := 0 to LJSONArray.Count-1 do
begin
ListBox4.Items.Add (LJSonArray.Items[I].GetValue<string>('Pictures'));
end;
finally
LJSONArray.Free;
end;
end;
更新 1
procedure TForm1.Button1Click(Sender: TObject);
var
LBItem : TListBoxItem;
i: integer;
HTTP : TIdHttp;
Stream : TMemoryStream;
begin
HTTP := TIdHttp.Create(nil);
try
for i := 0 to ListBox1.Items.Count-1 do
begin
LBItem := TListBoxItem.Create(nil);
LBItem.Parent := ListBox2;
LBItem.Height := 100;
Stream := TMemoryStream.Create;
HTTP.Get(ListBox1.Items.Strings[i], Stream);
LBItem.ItemData.Bitmap.LoadFromStream(Stream);
end;
finally
Stream.Free;
HTTP.Free;
end;
end;
我试过在另一个ListBox中加载图片,但是,添加了项目但没有图片!
在 TIdHTTP.Get()
将图像下载到您的 TMemoryStream
之后,您需要先将流搜索到位置 0,然后再对其进行任何操作,例如将其加载到您的 Bitmap
。并添加一个 try..except
块来处理下载错误。
此外,您应该使用第二个 try..finally
块来释放 TMemoryStream
。
procedure TForm1.Button1Click(Sender: TObject);
var
LBItem : TListBoxItem;
i: integer;
HTTP : TIdHttp;
Stream : TMemoryStream;
begin
HTTP := TIdHttp.Create(nil);
try
for i := 0 to ListBox1.Items.Count-1 do
begin
LBItem := TListBoxItem.Create(nil);
LBItem.Parent := ListBox2;
LBItem.Height := 100;
Stream := TMemoryStream.Create;
try
try
HTTP.Get(ListBox1.Items.Strings[i], Stream);
Stream.Position := 0; // <-- add this
LBItem.ItemData.Bitmap.LoadFromStream(Stream);
except
// do something else
end;
finally
Stream.Free;
end;
end;
finally
HTTP.Free;
end;
end;
我想从列表框项目的 URL 列表中加载图像,这是我检索 URLs
的代码var
LJSONArray : TJSONArray;
LEntity: TBackendEntityValue;
I : integer;
begin
try
LJSONArray := TJSONArray.Create;
BackendStorage1.Storage.QueryObjects('list', [], LJSONArray);
for I := 0 to LJSONArray.Count-1 do
begin
ListBox4.Items.Add (LJSonArray.Items[I].GetValue<string>('Pictures'));
end;
finally
LJSONArray.Free;
end;
end;
更新 1
procedure TForm1.Button1Click(Sender: TObject);
var
LBItem : TListBoxItem;
i: integer;
HTTP : TIdHttp;
Stream : TMemoryStream;
begin
HTTP := TIdHttp.Create(nil);
try
for i := 0 to ListBox1.Items.Count-1 do
begin
LBItem := TListBoxItem.Create(nil);
LBItem.Parent := ListBox2;
LBItem.Height := 100;
Stream := TMemoryStream.Create;
HTTP.Get(ListBox1.Items.Strings[i], Stream);
LBItem.ItemData.Bitmap.LoadFromStream(Stream);
end;
finally
Stream.Free;
HTTP.Free;
end;
end;
我试过在另一个ListBox中加载图片,但是,添加了项目但没有图片!
在 TIdHTTP.Get()
将图像下载到您的 TMemoryStream
之后,您需要先将流搜索到位置 0,然后再对其进行任何操作,例如将其加载到您的 Bitmap
。并添加一个 try..except
块来处理下载错误。
此外,您应该使用第二个 try..finally
块来释放 TMemoryStream
。
procedure TForm1.Button1Click(Sender: TObject);
var
LBItem : TListBoxItem;
i: integer;
HTTP : TIdHttp;
Stream : TMemoryStream;
begin
HTTP := TIdHttp.Create(nil);
try
for i := 0 to ListBox1.Items.Count-1 do
begin
LBItem := TListBoxItem.Create(nil);
LBItem.Parent := ListBox2;
LBItem.Height := 100;
Stream := TMemoryStream.Create;
try
try
HTTP.Get(ListBox1.Items.Strings[i], Stream);
Stream.Position := 0; // <-- add this
LBItem.ItemData.Bitmap.LoadFromStream(Stream);
except
// do something else
end;
finally
Stream.Free;
end;
end;
finally
HTTP.Free;
end;
end;