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:
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:
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:
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:
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!