:)
I'm having a slight issue where I'm trying to view picture files from my computer on a picture box when I select them. The files appear in the list box but won't appear on the picture box when selected
This is my code
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);
}
}
}
I've tried a number of ways but this is the current code I'm working with. Anyone see where I'm going wrong? I'm still new to C# and don't know all the syntax off by heart but I'm getting there. Any help would be more than appreciated.
Milly
You do add the filename ( lbName.Items.Add(file.Name);
) to the listbox and afterwards cast it back to (FileInfo)lbName.SelectedItem
. I checked your code with lbName
being a listBox - did not work for me...
I changed your code to this one
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;
//}
}
And now it worked fine for me!
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.