0

:)

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

这是我的代码

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# 的新手,不知道所有的语法,但我已经到了那里。任何帮助将不胜感激。

米莉

4

2 回答 2

1

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

我把你的代码改成了这个

    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;
        //}
    }

现在它对我来说很好用!

于 2013-10-24T22:11:00.503 回答
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);
        }
    }
于 2014-05-04T16:12:23.243 回答