【问题标题】:How do you add image file to a image list in code?如何在代码中将图像文件添加到图像列表中?
【发布时间】:2010-07-22 19:55:17
【问题描述】:

我有一个图像列表,并想在我的代码中将图像目录添加到图像列表中。我该怎么做?当我运行代码时:

'Load all of the items from the imagelist
For Each path As String In Directory.GetFiles("Images\LanguageIcons\")
     imlLanguagesIcons.Images.Add(Image.FromFile(path))
Next

我收到内存不足异常。目前只有 14 张图片,所以应该没有问题。有什么帮助吗?

【问题讨论】:

    标签: c# .net vb.net imagelist


    【解决方案1】:

    image.Dispose() 修复了内存不足异常。我使用的详细方法是(尽管对某些人来说有点矫枉过正):

            ImageList galleryList = new ImageList();
    
            string[] GalleryArray = System.IO.Directory.GetFiles(txtSourceDir.Text);    //create array of files in directory
            galleryList.ImageSize = new Size(96, 64);
            galleryList.ColorDepth = ColorDepth.Depth32Bit;
    
            for (int i = 0; i < GalleryArray.Length; i++)
            {
                if (GalleryArray[i].Contains(".jpg"))   //test if the file is an image
                {
                    var tempImage = Image.FromFile(GalleryArray[i]); //Load the image from directory location
                    Bitmap pic = new Bitmap(96, 64);
                    using (Graphics g = Graphics.FromImage(pic))
                    {
                        g.DrawImage(tempImage, new Rectangle(0, 0, pic.Width, pic.Height)); //redraw smaller image
                    }
                    galleryList.Images.Add(pic);    //add new image to imageList
                    tempImage.Dispose();    //after adding to the list, dispose image out of memory
                }
    
            }
    
            lstGallery.View = View.LargeIcon;  
            lstGallery.LargeImageList = galleryList;    //set imageList to listView
    
            for (int i = 0; i < galleryList.Images.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = i;
                lstGallery.Items.Add(item); //add images in sequential order
            }
    

    【讨论】:

      【解决方案2】:

      这样

      imageList.Images.Add(someImage);
      

      其中someImage 是一个Image 变量。

      编辑:像这样:

      For Each path As String In Directory.GetFiles("Images\LanguageIcons")
          imageList.Images.Add(Image.FromFile(path))
      Next
      

      【讨论】:

      • 但是文件可以有数量不等的图片文件。如何添加所有项目。我知道上面的怎么办
      • 您可能正在寻找AddStrip 方法。
      • System.IO.Directory.GetCurrentDirectory() & "Images\LanguageIcons"
      • 您的意思是您要尝试将每个图像文件添加到文件夹中吗?如果是这样,循环调用Directory.GetFiles
      • 不需要加Directory.GetCurrentDirectory()
      【解决方案3】:

      Image.FromFile 的文档(与您的 FromStream 相关) 说它会抛出 OutOfMemoryException 如果文件不是 有效的图像格式,或者如果 GDI+ 不支持像素格式。是吗 您可能正在尝试加载不受支持的图像类型?

      来源:Jim Mischel Out of memory exception while loading images

      这是我的方法:

      /// <summary>
      /// Loads every image from the folder specified as param.
      /// </summary>
      /// <param name="pDirectory">Path to the directory from which you want to load images.  
      /// NOTE: this method will throws exceptions if the argument causes 
      /// <code>Directory.GetFiles(path)</code> to throw an exception.</param>
      /// <returns>An ImageList, if no files are found, it'll be empty (not null).</returns>
      public static ImageList InitImageListFromDirectory(string pDirectory)
      {
          ImageList imageList = new ImageList();
      
          foreach (string f in System.IO.Directory.GetFiles(pDirectory))
          {
              try
              {
                  Image img = Image.FromFile(f);
                  imageList.Images.Add(img);
              }
              catch
              {
                  // Out of Memory Exceptions are thrown in Image.FromFile if you pass in a non-image file.
              }
          }
      
          return imageList;
      }
      

      【讨论】:

        【解决方案4】:

        您遇到内存不足错误,因为它试图在“For Each”结尾处加载 thumbs.db。 这为我解决了问题

        对于每个路径作为 Directory.GetFiles("\ImageServer\") 中的字符串 If InStr(path, "Thumbs") = 0 Then ImageList1.Images.Add(Image.FromFile(path)) 下一个

        可能还有更优雅的方式....

        【讨论】:

          【解决方案5】:

          对不起,我来晚了,请试试这个先生..

          Dim path_photo_member As String = "D:\image_data\member\"
          Me.ImageList_rs.Images.Clear()
          If System.IO.Directory.Exists(path_photo_member) Then
                  For Each path As String In System.IO.Directory.GetFiles(path_photo_member)
                       If System.IO.File.Exists(path) Then
                           If LCase(path).Contains(".png") Or LCase(path).Contains(".jpg") Or LCase(path).Contains(".jpeg") Then
                                Me.ImageList_rs.Images.Add(Image.FromFile(path))
                              End If
                       End If
                    Next
           End If
          

          【讨论】:

            猜你喜欢
            • 2011-02-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-02-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多