2010-07-22 14 views
4

J'ai une liste d'images et je voudrais ajouter un répertoire d'images à la liste d'images dans mon code. Comment ferais-je cela? Lorsque j'exécute le code:Comment ajouter un fichier image à une liste d'images dans le 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 

Je reçois une exception de mémoire insuffisante. Actuellement, il n'y a que 14 images donc il ne devrait vraiment pas y avoir de problème. De l'aide?

Répondre

2

Vous aimez cette

imageList.Images.Add(someImage); 

someImage est une variable Image.

EDIT: Comme ceci:

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

Mais le fichier peut avoir une quantité variable de fichiers image. Comment puis-je ajouter tous les éléments? Je sais comment faire ce qui est ci-dessus – muckdog12

+0

Quel est le fichier? – SLaks

+0

Vous recherchez peut-être la méthode 'AddStrip'. – SLaks

1

Documentation pour Image.FromFile (qui est lié à votre FromStream) dit qu'il jette OutOfMemoryException si le fichier n'est pas une image valide format ou si GDI + ne prend pas en charge le format de pixel. Est-il possible que vous essayez de charger un type d'image non pris en charge?

Source: Jim Mischel Out of memory exception while loading images

Voici ma méthode:

/// <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() fixe l'exception de la mémoire. L'approche détaillée que j'ai utilisée est (bien qu'excessive pour certains):

 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 
     }