:)
我有一个小问题,当我选择图片时,我试图在图片框中查看计算机上的图片文件。文件出现在列表框中,但在选择时不会出现在图片框中
这是我的代码
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# 的新手,不知道所有的语法,但我已经到了那里。任何帮助将不胜感激。
米莉