0
I need to search for images in all subdirectories and present them in one picturebox
, but the code I currently have only allows me to search in a single folder and without filtering by file types such as *jpg
, *png
, etc....
Follow my code below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace _myfotospf
{
public partial class FormFotos1 : Form
{
public FormFotos1()
{
InitializeComponent();
}
private void FormFotos_Load(object sender, EventArgs e)
{
string[] files = Directory.GetFiles(@"C:\Users\...\Imagens");
DataTable table = new DataTable();
table.Columns.Add("Nome do ficheiro (duplo clique para ver a miniatura)");
for (int i = 0; i < files.Length; i++)
{
FileInfo file = new FileInfo(files[i]);
table.Rows.Add(file.Name);
}
dataGridView1.DataSource = table;
}
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
FormFotos2 myForm = new FormFotos2();
string imageName = dataGridView1.CurrentRow.Cells[0].Value.ToString();
Image img;
img = Image.FromFile(@"C:\Users\...\Imagens\" + imageName);
myForm.pictureBox1.Image = img;
myForm.ShowDialog();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
João Martins, Thanks. Now there are no results in datagridview
– Paulo Ferreira
Okay, one "." point was missing before each extension when we declared the list
extensoes
. I edited the code and it should work!– João Martins
That’s right. I didn’t even notice that detail. The problem is that the results appear in datagridview but when I double click on the table to view the images, they do not appear. Formfotos2 myForm = new Formfotos2(); string imageName = dataGridView1.CurrentRow.Cells[0].Value.Tostring(); Image img; img = Image.Fromfile(@"C:........." + imageName); myForm.pictureBox1.Image = img; myForm.Showdialog();
– Paulo Ferreira
Validate if the path to the image is correct when you do the
Image.FromFile
.– João Martins
When the images are in the root of the directory, they appear, but when they are inside other subfolders, it gives error: System.IO.Filenotfoundexception Hresult=0x80070002 Message=C:......... 20170301_102446.jpg
– Paulo Ferreira
In the variable
diretorios
All you have to do is put in a directory, or it will do the same thing three times unnecessarily. As for the problem of the path, put the absolute path instead of the relative path.– João Martins
The path is already absolute. I deleted the folder names.
– Paulo Ferreira
Then there is some error in the path itself. The error is quite explicit
System.IO.FileNotFoundException
.– João Martins
Images only do not appear if they are inside subfolders. I don’t know how to solve this.
– Paulo Ferreira
If you place the path manually on this line
Image.FromFile(@"C:\Users\...\Imagens\" + imageName);
, still not working?– João Martins
João Martins, thanks again for the help. It doesn’t work. picturebox only returns the image if it is in the root folder that I point out on the way. If the image is inside subfolders, it no longer appears. I’m turning my head and I can’t find the reason why.
– Paulo Ferreira
Will it be access permissions? Already tried to access Visual Studio as Administrator?
– João Martins
Not permissions. Had already tested. The problem is that the path to the file is not correct if the image is inside another folder. Type: if image x is in folder A, it appears in picturebox, but if image y is inside folder B that is inside folder A, it is already wrong. The picturebox is not looking for the right path.
– Paulo Ferreira
When you say that the path to the file is not correct when it is inside another folder, what exactly do you mean? What is the path? Can give a real example?
– João Martins
Let’s imagine that I have these files: C: Users User1 Desktop Images img001.jpg C: Users User1 Desktop Images 2018 img002.jpg The path that is set is: img = Image.Fromfile(@"C: Users User1 Desktop Images" + imageName); The img001.jpg file appears in the picturebox when I double-click on datagridview, but the img002.jpg image no longer appears (gives error), because double-clicking on datagridview always returns a path to the root folder, like: C: Users User1 Desktop Images img002.jpg
– Paulo Ferreira