Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
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.
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 9 Nov '12 - 9:17
Edited 9 Nov '12 - 9:42


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.
  Permalink  
Comments
Marcus Kramer - 9 Nov '12 - 15:42
+5. Excellent.
bmw318mt - 9 Nov '12 - 15:56
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:31
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 424
1 Mahesh Bailwal 398
2 Maciej Los 205
3 Aarti Meswania 173
4 Rohan Leuva 165
0 Sergey Alexandrovich Kryukov 9,417
1 OriginalGriff 7,204
2 CPallini 3,933
3 Rohan Leuva 3,211
4 Maciej Los 2,743


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid