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

本文关键字:加载 列表 数据 | 更新日期: 2023-09-27 18:36:56

:)

遇到了一个小问题,当我选择图片时,我尝试在图片框中查看计算机中的图片文件。文件显示在列表框中,但在选中时不会显示在图片框中

这是我的代码

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

米莉

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

您将文件名 ( 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;
        //}
    }

现在它对我来说效果很好!

private void Form1_Load(object sender, EventArgs e) { 获取图片(); } private void listBox1_SelectedIndexChanged(Object sender, EventArgs e) { pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedItem).全名); 图片框1.大小模式 = 图片框大小模式.拉伸图像; }

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