Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to open several images from a folder and with every image I need to convert it to binary format. How can I open several files automatically within folder\sub folders?

Right Now I am able to open 1 file at a time by manually choosing form the openFileDialog.
C#
openFileDialog1.FileName = "";
openFileDialog1.Title = "Images";           
openFileDialog1.Filter = "Png Images| *.png"; 
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName.ToString() != "")
{
   Image.ImageLocation = openFileDialog1.FileName.ToString();
   img = new Bitmap(openFileDialog1.FileName.ToString());
}
Posted
Updated 9-Nov-12 9:42am
v2

1 solution

If you look closely at the OpenFileDialog class, you will see it has a Multiselect property[^] - set that to true and it will allow the user to select more than one file at a time. You can then use the Filenames property to open each image in a loop.

Do be aware however, that you should Dispose of Bitmap objects when you are finished with, don't just let them go out of scope or it will cause problems later.

BTW: You do not have to call ToString on string objects such as OpenFileDialog.FileName.
 
Share this answer
 
Comments
fjdiewornncalwe 9-Nov-12 15:42pm    
+5. Excellent.
datt265 9-Nov-12 15:56pm    
I have hundreds of images in several sub folders that will be used to train a neural net, that is why I want to load the files one after the other automatically, instead of having the user selecting which files to open.
OriginalGriff 10-Nov-12 3:31am    
Then do it with Directory.GetFiles - it has an overload which return subdirectory contents as well:
Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900