Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
User interface 如何在FireMonkey程序中将图像插入TImagelist?_User Interface_Firemonkey_Delphi Xe8 - Fatal编程技术网

User interface 如何在FireMonkey程序中将图像插入TImagelist?

User interface 如何在FireMonkey程序中将图像插入TImagelist?,user-interface,firemonkey,delphi-xe8,User Interface,Firemonkey,Delphi Xe8,我尝试填充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_9

我尝试填充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;
结果,当我为测试手动添加时,我只看到文本和一个图像


ItemAppariance设置为ImageListItem

//OMG!!谁创建了这个组件? 使用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;
//加载web图像

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;

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

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;

另请参见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;

另请参见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);

下面是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);

如果可以添加声明,那就太好了。使用Ctrl+Shift+V创建var。Delphi创建var和automaticaly检测类型。如果可以添加声明,那就太好了。使用Ctrl+Shift+V创建var。Delphi创建变量并自动检测类型。