6

I have an image list and would like to add a directory of images to the image list in my code. How would I do this? When I run the code:

'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

I get an out of memory exception. Currently there is only 14 images so there really shouldn't be a problem. Any Help?

blak3r
  • 16,066
  • 16
  • 78
  • 98
muckdog12
  • 83
  • 1
  • 1
  • 6

5 Answers5

3

image.Dispose() fixes the out of memory exception. The detailed approach which I used is (although overkill for some):

        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
        }
Paul
  • 756
  • 1
  • 8
  • 22
2

Like this

imageList.Images.Add(someImage);

Where someImage is an Image variable.

EDIT: Like this:

For Each path As String In Directory.GetFiles("Images\LanguageIcons")
    imageList.Images.Add(Image.FromFile(path))
Next
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • But the file has can have a variable amount of image files. How do I add all of the items. I know how do what is above – muckdog12 Jul 22 '10 at 19:57
  • You may be looking for the `AddStrip` method. – SLaks Jul 22 '10 at 20:00
  • System.IO.Directory.GetCurrentDirectory() & "Images\LanguageIcons" – muckdog12 Jul 22 '10 at 20:03
  • Do you mean that you're trying to add every image file in a folder? If so, loop over a call to `Directory.GetFiles` – SLaks Jul 22 '10 at 20:06
  • You don't need to add `Directory.GetCurrentDirectory()`. – SLaks Jul 22 '10 at 20:10
  • I am not sure where the file is other that the fact that it should be in the current directory. If it is not it will throw and error – muckdog12 Jul 22 '10 at 20:11
  • It threw a outofmemory exception – muckdog12 Jul 22 '10 at 20:17
  • You said there are 14 images. What are their sizes and color depths? If I'm not mistaken, images loaded into an ImageList are uncompressed. So a 640x480 24bpp file will consume 921600 bytes. If your images are large, they could consume a large amount of memory. – Chris Dunaway Jul 22 '10 at 20:37
  • The largest size is 124x124. All of these images were already in the image list, but I deleted them because I want to do it in code because I want the user to be able to add thier own images into the file. – muckdog12 Jul 22 '10 at 20:41
1

Documentation for Image.FromFile (which is related to your FromStream) says that it will throw OutOfMemoryException if the file is not a valid image format or if GDI+ doesn't support the pixel format. Is it possible you're trying to load an unsupported image type?

Source: Jim Mischel Out of memory exception while loading images

Here's my method:

/// <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;
}
Community
  • 1
  • 1
blak3r
  • 16,066
  • 16
  • 78
  • 98
0

You are getting an out of memory error because it is trying to load thumbs.db at the end of your "For Each". This took care of it for me

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

There is probably a more elegant way....

Jim
  • 1
0

sorry I'm late, Please Try this sir..

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
Fajarsoft
  • 41
  • 4