如何以编程方式将图像插入 FireMonkey 中的图像列表?

How to insert images to TImagelist in FireMonkey programmaticaly?

我尝试填充 TListView

当前代码

MyList.Items.Clear;
 for I := 0 to List.Count-1 do
  begin
  Item:=  MyList.Items.Add;
  Item.Text:= List[i];

  si:=ImageList.Source.Add;
  src:='https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg';
  ms:=LoadWebImage(src);
  si. MultiResBitmap.LoadItemFromStream( ms,100);

  Item.ImageIndex := i;

  end;

图像加载无误

 function LoadWebImage(url: string) : TMemoryStream;
        var
          idhttp : TIdHTTP;
        begin
          IdHTTP := TIdHTTP.Create(nil);
          Result := TMemoryStream.Create;
          try
            idhttp.Get (url, Result);
            Result.Position := 0;
          finally
            idhttp.Free;
          end;
        end;

结果我手动添加测试时只看到文本和一张图片

ItemAppeariance 设置为 ImageListItem

//天啊!!谁创建了这个组件? 使用 IdHTTP;

var
  I: Integer;
  Item: TListViewItem;
  src: string;
  ms: TMemoryStream;
  ItemImageIndex: Integer;
  list:TStringList
//...
    if Assigned(List) then
     begin
     MyList.Items.Clear;
     for I := 0 to List.Count-1 do
      begin
      // Create list view item
      Item:=  MyList.Items.Add;
      Item.Text:= List[i];

      // Load image
      src:='http://www.w3schools.com/html/pic_mountain.jpg';
      ms:= LoadWebImage(src);

      // Source
      si:=ImageList.Source.Add;
      si.Name:= 'Source'+inttostr(i);

      scale:=1;
      si.MultiResBitmap. LoadItemFromStream(ms,scale);


      W:=si.MultiResBitmap.Bitmaps[scale].Width;    //Get width from scale
      H:=si.MultiResBitmap.Bitmaps[scale].Height;   //Get height from scale

      // Destination
      d:=imageList.Destination.Add;
      Layer := d.Layers.Add;
      Layer.SourceRect.Rect := TRectF.Create(0, 0, W , H);   // Create rect W x H
      Layer.Name := si.name;

      Item.ImageIndex := i;

      end;
     end;

//加载网页图片

function LoadWebImage(Url: string): TMemoryStream;
var
  IdHTTP: TIdHTTP;
begin
  try

    IdHTTP := TIdHTTP.Create(nil);
    result := TMemoryStream.Create;
    try
      IdHTTP.Get(Url, result);
      result.Position := 0;
    finally
      IdHTTP.Free;
    end;
  except
    result := nil;
  end;
end;

See example

另见 TSourceCollection.AddOrSetTCustomImageList.AddOrSet

function AddOrSet(const SourceName: string;
  const Scales: array of Single; 
  const FileNames: array of string;
  const TransparentColor: TColor = TColors.SysNone; 
  const Width: Integer = 0;
  const Height: Integer = 0): TCustomSourceItem;

这是 TImageList 的简单助手

type
  TImageListHelper = class helper for TImageList
    function Add(aBitmap: TBitmap): integer;
  end;



function TImageListHelper.Add(aBitmap: TBitmap): integer;
const
  SCALE = 1;
var
  vSource: TCustomSourceItem;
  vBitmapItem: TCustomBitmapItem;
  vDest: TCustomDestinationItem;
  vLayer: TLayer;
begin
  Result := -1;
  if (aBitmap.Width = 0) or (aBitmap.Height = 0) then exit;

  // add source bitmap
  vSource := Source.Add;
  vSource.MultiResBitmap.TransparentColor := TColorRec.Fuchsia;
  vSource.MultiResBitmap.SizeKind := TSizeKind.Source;
  vSource.MultiResBitmap.Width := Round(aBitmap.Width / SCALE);
  vSource.MultiResBitmap.Height := Round(aBitmap.Height / SCALE);
  vBitmapItem := vSource.MultiResBitmap.ItemByScale(SCALE, True, True);
  if vBitmapItem = nil then
  begin
    vBitmapItem := vSource.MultiResBitmap.Add;
    vBitmapItem.Scale := Scale;
  end;
  vBitmapItem.Bitmap.Assign(aBitmap);

  vDest := Destination.Add;
  vLayer := vDest.Layers.Add;
  vLayer.SourceRect.Rect := TRectF.Create(TPoint.Zero, vSource.MultiResBitmap.Width,
      vSource.MultiResBitmap.Height);
  vLayer.Name := vSource.Name;
  Result := vDest.Index;
end;

然后您可以轻松地在图像列表中添加图像:

ImageList1.Add(MyBitmap);