根据修改日期将图片从文件夹加载到图片框中

时间:2015-12-17 16:33:15

标签: c# sorting picturebox imagelist

当有人登录应用程序时,会将该人的照片保存到文件夹中。在登录表单上我有4个图片框。我希望根据过去4个修改日期,使用最近4张最近的照片填充4个图片框。因此,如果有10张照片只是通过修改排序并显示最近的4张。

我首先尝试将它们添加到列表中,但之后不确定如何使用它以使其按照我想要的方式进行操作

DirectoryInfo directoryInfo = new DirectoryInfo(PicPath);
    var recentpics = directoryInfo.GetFiles("*.jpg*",   SearchOption.AllDirectories).OrderBy(t => t.LastWriteTime).ToList();

然后我尝试了这个并没有排序,但我想弄清楚如何首先显示它们,这也不起作用。

string PicPath2 = @"path_to_Pictures";
            string[] list = Directory.GetFiles(PicPath2, "*.jpg");
            PictureBox[] picturebox = new PictureBox[recentpics.Count];
            int y = 0;
            for (int index = 0; index < 5; index++)
            {
                picturebox[index] = new PictureBox();
                this.Controls.Add(picturebox[index]);
                if (index % 3 == 0) y = y + 10;
                picturebox[index].Location = new Point(index * 120 + 20, y);
                picturebox[index].Size = new Size(100, 120);
                picturebox[index].Image = Image.FromFile(list[index]);
            }

现在试着弄清楚如何实现这个目标。

2 个答案:

答案 0 :(得分:0)

如果你需要最新的,你必须排序OrderByDescending,如果只需要4,不加载所有列表,Take(4)

DirectoryInfo directoryInfo = new DirectoryInfo(PicPath);
var recentpics = directoryInfo.GetFiles("*.jpg*",  SearchOption.AllDirectories).OrderByDescending(t => t.LastWriteTime).Take(4).ToList();

第二部分

List<PictureBox> picturebox = List<PictureBox>;
var y = 10;
foreach (var file in recentpics)
{
   var pb = new PictureBox();                
   pb.Location = new Point(picturebox.Count * 120 + 20, y);
   pb.Size = new Size(100, 120);                
   pb.Image = Image.FromFile(file.FullName);
   this.Controls.Add(pb);
   picturebox.Add(pb);
}

这是我的完整代码

public partial class Form1 : Form
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(547, 417);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);

    }

    #endregion

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        List<PictureBox> picturebox = new List<PictureBox>();
        DirectoryInfo directoryInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
        var recentpics = directoryInfo.GetFiles("*.jpg*", SearchOption.AllDirectories).OrderByDescending(t => t.LastWriteTime).Take(4).ToList();
        var y = 10;
        foreach (var file in recentpics)
        {
            var pb = new PictureBox();                
            pb.Location = new Point(picturebox.Count * 120 + 20, y);
            pb.Size = new Size(100, 120);                
            pb.Image = Image.FromFile(file.FullName);
            this.Controls.Add(pb);
            picturebox.Add(pb);
        }
    }
}

答案 1 :(得分:0)

private void Form1_Load(object sender, EventArgs e)
{
    int a=75,b=27;
    DirectoryInfo d = new DirectoryInfo(@"C:\images\52");
    FileInfo[] files = d.GetFiles("*.png");
    foreach(FileInfo f in files)
    {
    PictureBox p = new PictureBox();
    p.Location = new Point(a + 137, b);
   p.Image = Image.FromFile(Path.Combine(f.DirectoryName, f.Name));
   p.Size = new System.Drawing.Size(137, 171);
   p.SizeMode = PictureBoxSizeMode.StretchImage;
   this.Controls.Add(p);
   a += 75;
   p.BringToFront();
  }
 }

也许这会有所帮助。我认为它很直接。