Saturday, 7 September 2013

Fetching particular count of images from folder

Fetching particular count of images from folder

I am using this code to fetch images and display them in pictureboxes of
particular form ,same thing I am going to do for 4 5 more forms ,but I
don't want these images to get repeated in any of the forms or picture
boxes,how can i do that ? and on each form I have 3 pictureboxes
public partial class MainForm : Form
{
string LoginName;
List<PictureBox> pictureBoxes;
List<String> filesToShow = new List<String>();
public MainForm(string lognName)
{
InitializeComponent();
this.LoginName = lognName;
pictureBoxes = new List<PictureBox> {
pictureBox1,
pictureBox2,
pictureBox3
};
ShowImages();
}
private List<String> GetFilesToShow()
{
String str =
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string path = str + "\\Image\\";
return Directory.GetFiles(path, "*.jpg",
SearchOption.TopDirectoryOnly).ToList();
}
private void ShowImages()
{
List<int> selectedImages = new List<int>();
foreach (var pictureBox in pictureBoxes)
{
if (filesToShow != null && !filesToShow.Any())
{
filesToShow = GetFilesToShow();
}
if (filesToShow != null && filesToShow.Any()) // If any
files then allow the code to delete the shown images
{
if (filesToShow.Count >= pictureBoxes.Count)
{
filesToShow.Reverse();
for (int i = 1; i < 4; i++)
{
pictureBox.ImageLocation = filesToShow[i];
}
}
}
}
}
}

No comments:

Post a Comment