Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:17am
bmw318mt1.2K
Edited 9-Nov-12 9:42am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 at 9-Nov-12 15:42pm
   
+5. Excellent.
bmw318mt at 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 at 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)



Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100