【问题标题】:Convert Image list to Image array将图像列表转换为图像数组
【发布时间】:2013-03-03 11:33:21
【问题描述】:

看了几个例子,按照这些例子,我的代码现在看起来像这样(见下文),但不幸的是我收到了错误 “‘System.Windows.Forms.ImageList’不包含‘toArray’的定义,并且找不到接受‘System.Windows.Forms.ImageList’类型的第一个参数的扩展方法‘toArray’(您是否缺少使用指令还是程序集引用?)”

有什么想法吗?我可能错过了其他帖子中显示的部分,但我不这么认为

ImageList Move_list = new ImageList();
.
.
.
//Gather the images
string        path          = "C:/Pictures/Movements/User";
string[]      filter        = { ".jpg", ".jpeg"};
DirectoryInfo directoryInfo = new DirectoryInfo(path);
FileInfo[]    fileInfo      = directoryInfo.GetFiles();
ArrayList     arrayList     = new ArrayList();

foreach (FileInfo fi in fileInfo)
  foreach (string s in filter)
    if (s == fi.Extension)
      arrayList.Add(fi.FullName);

//adding files to image list:
for (i = 0; i < arrayList.Count; i++)
{
  System.Drawing.Image img = System.Drawing.Image.FromFile(arrayList[i].ToString());
  Move_list.Images.Add(img);
}

User_moves[0] =  Move_list.toArray();

【问题讨论】:

  • 请详细说明。您还没有告诉我们Move_listMove_list.Images 的数据类型,很难告诉您如何将其转换为数组。它看起来像你的班级Move_list 有某种Images 集合。如果是IEnumerable&lt;Image&gt;,你可以直接说Image[] x = Move_list.ToArray() ;
  • 对不起,移动列表是:ImageList Move_list = new ImageList();

标签: c# arrays image list


【解决方案1】:

Move_list.Images 是您的列表,而不是 ImageList。这是一个命名错误的类,但它映射到 Win32 中的 ImageList 概念(这是一个长序列图像位图的句柄)。无论如何:

Move_list.Images.Cast<Image>().ToArray();

【讨论】:

  • 如果你不使用 Cast,你会得到一个 object[] 而不是 Image[]。
  • 不走运,这样做会引发此错误:'System.Data.EnumerableRowCollectionExtensions.Cast(System.Data.EnumerableRowCollection)' 是一种'方法',在给定的上下文中无效
  • 我的错,我忘记了 Cast() 调用中的括号。
【解决方案2】:

据我所知,Move_listImageListImageList 没有实现 IQueryableIEnumerableToArraySystem.Linq 中定义为这两个接口上的扩展方法。

您应该可以使用Move_list.Images.ToArray(),因为Images 是一个ImageCollection,它确实实现了IEnumerable

【讨论】:

    猜你喜欢
    • 2018-01-03
    • 2012-10-28
    • 2022-01-03
    • 2016-06-19
    • 1970-01-01
    • 2018-07-21
    • 2012-09-22
    • 1970-01-01
    • 2019-08-07
    相关资源
    最近更新 更多