如何在代码中将图像文件添加到图像列表?

时间:2010-07-22 19:55:18

标签: c# .net vb.net imagelist

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

'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张图片,所以真的不应该有问题。任何帮助?

5 个答案:

答案 0 :(得分:2)

喜欢这个

imageList.Images.Add(someImage);

其中someImageImage变量。

编辑:像这样:

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

答案 1 :(得分:2)

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 :(得分:1)

  

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;
}

答案 3 :(得分:0)

您遇到了内存不足的错误,因为它试图在“ For Each”的末尾加载thumbs.db。 这为我照顾了

对于每个路径,如Directory.GetFiles(“ \ ImageServer \”)中的字符串      如果InStr(path,“ Thumbs”)= 0,则ImageList1.Images.Add(Image.FromFile(path))  下一个

可能有一种更优雅的方式。...

答案 4 :(得分:0)

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

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