【问题标题】:How to insert images to TImagelist in FireMonkey programmaticaly?如何以编程方式将图像插入 FireMonkey 中的 Imagelist?
【发布时间】:2016-07-01 00:07:36
【问题描述】:

我尝试填充 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

【问题讨论】:

    标签: user-interface firemonkey delphi-xe8


    【解决方案1】:

    // 天哪!!谁创建了这个组件? 使用 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;
    

    【讨论】:

    • 如果你能添加声明就好了。
    • 使用 Ctrl+Shift+V 创建 var 。 Delphi 创建 var 并自动检测类型。
    【解决方案2】:

    这是 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);
    

    【讨论】:

      【解决方案3】:

      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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多