6

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

'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 张图片,所以应该没有问题。有什么帮助吗?

4

5 回答 5

3

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
        }
于 2014-01-09T08:50:32.373 回答
2

像这样

imageList.Images.Add(someImage);

哪里someImageImage变量。

编辑:像这样:

For Each path As String In Directory.GetFiles("Images\LanguageIcons")
    imageList.Images.Add(Image.FromFile(path))
Next
于 2010-07-22T19:56:26.293 回答
1

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

来源:Jim Mischel加载图像时出现内存不足异常

这是我的方法:

/// <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;
}
于 2012-01-11T04:53:06.433 回答
0

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

For Each path As String In Directory.GetFiles("\ImageServer\") If InStr(path, "Thumbs") = 0 Then ImageList1.Images.Add(Image.FromFile(path)) Next

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

于 2019-09-20T19:32:33.367 回答
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
于 2020-07-08T16:42:57.447 回答