将数据从列表框加载到图片框

时间:2013-10-24 22:00:51

标签: c#

:)

我有一个小问题,当我选择它们时,我试图在图片框上查看我的计算机中的图片文件。文件显示在列表框中,但在选中时不会出现在图片框中

这是我的代码

using System;                                                                   /    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void getPictures_Click(object sender, EventArgs e)
        {
            string[] filters = { "*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp" };

            var directory = new DirectoryInfo(@"C:\Pictures");

            var files = new List<FileInfo>();

            foreach (var filter in filters)
            {
                var results = directory.GetFiles(filter, SearchOption.AllDirectories);
                files.AddRange(results);
            }

            foreach (var file in files)
            {
                lbName.Items.Add(file.Name);
            }
            var dialog = new FolderBrowserDialog();
            var result = dialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                string path = dialog.SelectedPath;
                lblText.Text = path;
            }
        }

        private void lbName_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(((FileInfo)lbName.SelectedItem).FullName);
        }
        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

            pictureBox1.Image = Image.FromFile(((FileInfo)lbName.SelectedItem).FullName);
        }
    }
}

我尝试了很多方法,但这是我正在使用的当前代码。谁知道我哪里错了?我还是C#的新手,并不清楚所有语法,但我已经到了那里。任何帮助都不仅仅是值得赞赏的。

米莉

2 个答案:

答案 0 :(得分:1)

您确实将文件名(lbName.Items.Add(file.Name);)添加到列表框中,然后将其强制转换回(FileInfo)lbName.SelectedItem。我检查了您的代码,lbName listBox - 对我来说不起作用......

我将您的代码更改为此

    private void Form1_Load(object sender, EventArgs e)
    {
        getPictures(); // load pics from hdd
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedItem).FullName);
    }

    private void getPictures()
    {
        string[] filters = { "*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp" };
        // change path to yours
        var directory = new DirectoryInfo(@"C:\Users\Public\Pictures\Sample Pictures");

        var files = new List<FileInfo>();

        foreach (var filter in filters)
        {
            var results = directory.GetFiles(filter, SearchOption.AllDirectories);
            files.AddRange(results);
        }

        foreach (var file in files)
        {
            listBox1.Items.Add(file);
        }
        // not quite sure what this code should do - so I comment out - as I do not think it is necessary for your question!
        //var dialog = new FolderBrowserDialog();
        //var result = dialog.ShowDialog();

        //if (result == DialogResult.OK)
        //{
        //    string path = dialog.SelectedPath;
        //    lblText.Text = path;
        //}
    }

现在它对我来说很好用!

答案 1 :(得分:0)

private void Form1_Load(object sender,EventArgs e)         {             getPictures();         }         private void listBox1_SelectedIndexChanged(object sender,EventArgs e)         {             pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedItem).FullName);             pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;         }

    private void getPictures()
    {
        string[] filters = { "*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp" };
        string resulPath = ""; // var that will hold the path that returns on the user search
        // not quite sure what this code should do - so I comment out - as I do not think it is necessary for your question!
        var dialog = new FolderBrowserDialog();
        var result = dialog.ShowDialog();

        if (result == DialogResult.OK)
        {
            string path = dialog.SelectedPath;
            resulPath = path;
        }

        // Here i set the DirectoryInfo with the var resulPath
        var directory = new DirectoryInfo(@"" + resulPath + "");

        var files = new List<FileInfo>();

        foreach (var filter in filters)
        {
            var results = directory.GetFiles(filter, SearchOption.AllDirectories);
            files.AddRange(results);
        }

        foreach (var file in files)
        {
            listBox1.Items.Add(file);
        }
    }