Click here to Skip to main content
15,896,285 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to retrieve an image to a picture box from an image list.
I am using c# code and then executed this error comes:
'System.Collections.Generic.List<string>' does not contain a definition for 'Images' and no extension method 'Images' accepting a first argument of type 'System.Collections.Generic.List<string>' could be found (are you missing a using directive or an assembly reference?


here is my code

What I have tried:

C#
private void button1_Click(object sender, EventArgs e)
        {

            List<string> imglist = new List<string>();

              imglist=GetImagesPath("C:/outputDirectory");

               for (int x = 0; x < imglist.Count; ++x)
                {
                    Image temp = imglist.Images[x];
                    pictureBox1.Image = temp;
                }
           
        } 


public List<string> GetImagesPath(String folderName)
        {

            DirectoryInfo Folder;
            FileInfo[] Images;

            Folder = new DirectoryInfo(folderName);
            Images = Folder.GetFiles();
            List<string> imagesList = new List<string>();

            for (int i = 0; i < Images.Length; i++)
            {
                imagesList.Add(String.Format(@"{0}/{1}", folderName, Images[i].Name));
               
            }

            double a = imagesList.Count();
            textBox1.Text = a.ToString();


            return imagesList;
            
        }
Posted
Updated 31-May-16 20:36pm
v2
Comments
Sergey Alexandrovich Kryukov 1-Jun-16 2:17am    
Excuse me, what makes you thinking that a list of string could possibly contain images?
—SA
Debasis.logica 1-Jun-16 2:20am    
Have you included the using statement for System.Collections.Generic like:
using System.Collections.Generic;
at the top of your code?

There are a couple of things here:
Firstly, don't use the name "Image List" - there is a control of that name ImageList Class (System.Windows.Forms)[^] which can be used to provide images to a PictureBox, so you will confuse people if you use the name inapropriately.
Second, don't use string.Format to join a path together - use Path.Combine as it sorts out spearators intelligently. In this case however, it's much simpler to use the FileInfo.FullName property instead:
C#
for (int i = 0; i < Images.Length; i++)
{
    imagesList.Add(Images[i].FullName);
}

But you need to be careful - particularly with images, Windows will add files to the folder for indexing and thumbnails, so all the files you find with DirectoryInfo.GetFiles will not be images!
If you use this code:
C#
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
List<string> imageExtensions = new List<string>();
foreach (ImageCodecInfo codec in codecs)
    {
    string[] parts = codec.FilenameExtension.Split(';');
    foreach (string extension in parts)
        {
        imageExtensions.Add(extension);
        }
    }

If will give you a list of all the image file extensions that are known on your system, and you can use Directory.GetFiles with a search string in a loop to fetch all the images (and just the images) in the path:
C#
List<string> files = new List<string>();
foreach (string ext in imageExtensions)
    {
    string[] paths = Directory.GetFiles(@"D:\Temp\", ext);
    files.AddRange(paths);
    }

But that fetches the path to the image - you can't load that into a PictureBox directly. Instead, you have to load the image from the disk:
C#
myPictureBox.Image = Image.FromFile(files[0]);

And don't do that in a loop as you are trying or you will load each and every image in there and overwrite them all - so you end up with just the last one!
 
Share this answer
 
Please see my comment to the question. I have no idea what kind of help you may need, except the advice to look around and think logically.

Perhaps this link can get you some ideas: ImageList Class (System.Windows.Forms)[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900