开发者

How do you add image file to a image list in code?

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?


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
        }


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


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


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....


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜