2010-07-22 87 views
4

我有一个图像列表,并希望将图像的目录添加到我的代码中的图像列表。我将如何做到这一点?当我运行代码:如何将图像文件添加到代码中的图像列表中?

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

回答

2

像这样

imageList.Images.Add(someImage); 

someImageImage变量。

编辑:像这样:

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

但是该文件可以有可变数量的图像文件。我如何添加所有项目。我知道怎么做 – muckdog12 2010-07-22 19:57:53

+0

什么是文件? – SLaks 2010-07-22 20:00:40

+0

您可能正在寻找'AddStrip'方法。 – SLaks 2010-07-22 20:00:59

1

的文档Image.FromFile(这是关系到你的FromStream) 说,这将引发OutOfMemoryException异常如果文件不是 有效的图像格式或者如果GDI +不支持像素格式。您是否可能尝试加载不受支持的图片类型为 ?

来源:吉姆·米契尔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; 
} 
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 
     } 
相关问题