-1

I have two controls for displaying images from a folder, they are: ImageList and ListView

Now I can to display images using the above controls simply without any criteria.

  • But I want to:

Show Images based on the date of image created. For this purpose I have one DateTimePicker control to select the date , one ComboBox to select an Option and one Button to show the result as shown in the following snapshot.

For Example: Show image before the date 15/1/2015, date from the image tag.

enter image description here

This is the code which I used to display the image in to the ListView

   private void btn_Show_Click(object sender, EventArgs e)
   {

          lstView_un.Items.Clear();

          DirectoryInfo dir = new DirectoryInfo(@"D:\Images");
          foreach (FileInfo file in dir.GetFiles())
          {
              try
              {
                 this.imageList1.Images.Add(Image.FromFile(file.FullName));
              }
              catch
              {
                 Console.WriteLine("This is not an image");
              }
          }

   this.imageList1.ImageSize = new Size(256, 250);
   this.lstView_un.LargeImageList = this.imageList1;
   for (int j = 0; j < this.imageList1.Images.Count; j++)
   {
          ListViewItem item = new ListViewItem();
          item.ImageIndex = j;
          this.lstView_un.Items.Add(item);
   }             

}
TaW
  • 53,122
  • 8
  • 69
  • 111
usminuru
  • 335
  • 4
  • 8
  • 19

1 Answers1

0

Play with this:

var allfiles = Directory.GetFiles(yoursearchparams..);
files = new List<FileInfo>();
foreach(string f in projects) files.Add(new FileInfo(f));
var filesSorted = files.OrderByDescending(x=>x.CreationTime);

This is descending and using creation date. There is also OrderBy and LastWriteTime.

Here is the full example, including: Setting colorDepth, a text and multiple extensions and using a date to limit the result..

    private void button1_Click(object sender, EventArgs e)
    {   // last 2 weeks only:
        DateTime dt = DateTime.Now.AddDays(-14);  // use your own way to set it!

        string imagesPath = @"D:\scrape\SOusers";
        string[] extensions = new[] { ".jpg", ".jpeg", ".png" };
        var allfiles = Directory.GetFiles(imagesPath);
        List<FileInfo> files = new List<FileInfo>();
        foreach(string f in allfiles) files.Add(new FileInfo(f));
        var filesSorted = files.Where(f => extensions.Contains( f.Extension.ToLower()))
                               .Where(f => f.CreationTime < dt)
                               .OrderByDescending(f => f.CreationTime);
        this.imageList1.ImageSize = new Size(256, 250);
        this.imageList1.ColorDepth = ColorDepth.Depth32Bit;
        foreach (FileInfo fileInfo in filesSorted)
        {
            try {
                  this.imageList1.Images.Add(fileInfo.Name,
                                           Image.FromFile(fileInfo.FullName));
            }
            catch {
                    Console.WriteLine(fileInfo.FullName + "  is is not a valid image.");
            }
        }
        lstView_un.View = View.LargeIcon;
        lstView_un.LargeImageList = this.imageList1;
        lstView_un.Items.Clear();
        for (int j = 0; j < this.imageList1.Images.Count; j++)
        {
            ListViewItem item = new ListViewItem();
            item.ImageIndex = j;
            item.Text = imageList1.Images.Keys[j].ToString();
            this.lstView_un.Items.Add(item);
        }             
   }
TaW
  • 53,122
  • 8
  • 69
  • 111
  • Your answer is exactly what I need, but how to use it? Please let help me by editing my code shown above. – usminuru May 18 '15 at 12:25
  • This should cover all your bases. Usually you should use the code samples provided to improve your code, not hope for fully written code, though! – TaW May 18 '15 at 13:52