This code is meant to read in a folder directory from a text file I have stored, and using that ‘activeFilepath’ variable to read through each file in the folder and retrieve their names, which in turn can be used to load each image up in turn.
StreamReader textIn = new StreamReader(imageSet);
try
{
//Get the folder location by reading in the specific text file.
activeFilepath = textIn.ReadLine();
//If the user isn't choosing a password then go into the 'Blurred' directory of the folder.
if (choosePassword == false)
activeFilepath = activeFilepath + @"\Blurred";
int i = 0;
//Read in all of the image location strings, and put them in the pictureLocations list.
foreach (string filePath in Directory.EnumerateFiles(activeFilepath))
{
pictureLocations.Add(filePath);
//pictureLocations[i] = filePath;
// Display an error image if there is a blank entry in the picture location.
if (pictureLocations[i] == null)
pictureLocations[i] = @"C:\Users\Public\Pictures\Sample Pictures\error.png";
//Remove Thumbs.db from the list if read in.
if (pictureLocations[i] == activeFilepath + @"\Thumbs.db")
{
pictureLocations.Remove(pictureLocations[i]);
}
i++;
}
foreach (PictureBox imagePanel in pictureBoxList)
{
int rand = randomGenerator.Next(0, pictureLocations.Count);
//The images are then loaded in a random order from the picture location list.
imagePanel.Load(pictureLocations[rand]);
//Remove the picture just loaded out of the list so it won't repeat.
pictureLocations.Remove(pictureLocations[rand]);
imagePanel.SizeMode = PictureBoxSizeMode.StretchImage;
}
At the moment I’m receiving an index type error saying the Index is out of range when I remove Thumbs.db from my list (because I don’t want to load it into the picture boxes I have on my form), however when I simply let it read in it in, I receive a parameter-type error instead.
I’m assuming I get the latter because it’s trying to load Thumbs.db as an image, but I’m wondering if there’s another way of removing or stopping it from being read in the first place?
Is it something else entirely?
The crash is that you increment
ieven when you didn’t add anything. A simple fix would be simply not to add the item if you don’t intend to keep it. That way you don’t need the peskyivariable.In other words, rather than adding an item and then later realizing you didn’t mean to add it (and having to keep track of where it is), simply decide up front whether you want to add it or not (and if it needs to be edited), and then add it if you want to keep it.
This avoids the fragile relationship between
pictureLocationandi.