Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I have
 
string[] filenames = Directory.GetFiles(@"C:\temp\test", "*.png", SearchOption.AllDirectories);
 
string[] myArr = new string[10];
 
private void btnLoadTrainingData_Click(object sender, EventArgs e)
{
            foreach (string f in filenames)
            {                
                img = (Bitmap)Bitmap.FromFile(f);
                Console.WriteLine(convertToBinary(img));            
            }
}
The method 'convertToBinary' returns a string value of 1's and 0's of the converted image.
 
What I need is how to store the string of each returned value in an array + the filename.
 
I want something like this:
A_001.png 100100011
A_002.png 100110011
B_001.png 001110111
etc
 

 
I have tried to implement an array (see below) but I am always getting the last file that was opened.
 
private void populateArray(string binVal)
    {
        for (int j = 0; j < myArr.Length; j++)
        {
            //for (int j = 0; j < 2; j++)
            myArr[j] = (binVal);
        }
Posted 17-Nov-12 1:27am
bmw318mt1.2K
Edited 17-Nov-12 4:04am
v2
Comments
Vivek Krishnamurthy at 17-Nov-12 7:53am
   
Its unclear what the problem is...
 
You can concatenate the file name with the return of convertToBinary in the first foreach you written...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You could just modify your existing foreach loop:
int i = 0;
foreach (string f in filenames){                
   img = (Bitmap)Bitmap.FromFile(f);
   myArr[i++] = f + " " + convertToBinary(img);            
   if (i >= myArr.Length) {
      break;
      }
   }
But I will admit I am at a loss as to why you would want to do this in the first place! Laugh | :laugh:
  Permalink  
Comments
BillWoodruff at 17-Nov-12 8:53am
   
I +5'd your answer, although in this case we don't know if the user intentionally set the size of 'myArr to #10.
 
Note to OP: newcomers to C# often do not realize that using the ++ operator in an array index will NOT auto-increment an integer variable until AFTER its use, as OG's code makes use of here.
OriginalGriff at 17-Nov-12 8:57am
   
I know what you mean - but looking at the OP's other questions, I suspect that ten was a random number - I would have suggested a list, but I didn't want to confuse him any more than he is at the moment. :sigh:
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

While I believe OriginalGriff has answered this question correctly, I'll throw this in the pot just to add a slightly different flavor.
 
@bmw318mt:
 
1. There's no mystery why your current code sticks the same binary-string value in each array element: you are only passing it one value !
 
2. Do you assign 'myArr a fixed size of ten elements because that's all you want; and, are you sure the result of the Directory Search will always return at least ten elements ?
 
3. Note: this example uses only one string array, but there's no reason not to use two of them, and it might make the code clearer to use two of them.
 
Consider:
// other required 'using statements
using System.IO;
//
// Class scoped variables
private string[] fileNames;
private string testFilePath = @"C:\temp\test";
private string testFileType = @"*.png";
 
private void populateArray(string filePath, string fileType)
{
    try
    {
        fileNames = Directory.GetFiles(filePath, fileType, SearchOption.AllDirectories);
    }
    catch (DirectoryNotFoundException ex)
    {
        MessageBox.Show("Error: " + ex.Message);
        // we're dead: give up the ghost !
        return;
 
    }
 
    // method scoped variable
    Bitmap img;
 
    for (int j = 0; j < fileNames.Length; j++)
    {
        img = (Bitmap)Bitmap.FromFile(fileNames[j]);
        // here we use your convertToBinary method
        // which you do not show us the code for
        fileNames[j]+= " " + convertToBinary(img);
    }
}
//
// so, you'd call the code like this:
// populateArray(testFilePath, testFileType);
Now, let's take one step back, and consider the possibilities of errors in your using Directory.GetFiles ... we've demonstrated catching one kind of error above: you may want to consider whether to handle one or more of several specific possible errors of this File.IO method ... see: [^] ... or handle all possible error messages with the same error handler ... but, imho, you should think about the possibilities of errors using a method like GetFiles ! An error I once encountered using the 'AllDirectories option was caused by one sub-folder not having access permission.
 
What if you find some enormously large number of files that match in a specified directory ? Is that a problem ? Using the techniques here, you cannot just "eat until full," then stop:
 
So, if all you really want is ten filepaths, or some other arbitrary number ... ?
 
Looking one step further: your array of strings where the file name of the file is concatenated with a binary representation of file contents (byte-by-byte) essentially ... assuming you want to use both pieces of information now merged into one array element ... means that to access a given file's binary data, you are going to have take an array element and "split it" in some way: that's very inefficient, and unnecessary.
 
So, how else might you design your data structure to preserve the linkage between file name and binary representation: a Dictionary<string,string> is one choice that can be used here, since we can assume the complete filepath of any one file is unique, and can be used as the Key for each Dictionary item; naturally, the binary representation would become the Value of the Key for the given Dictionary item.
  Permalink  
v3
Comments
bmw318mt at 20-Nov-12 11:25am
   
should i dispose each image after use? if yes is this the correct way
img.Dispose(); after the foreach loop?
 
thanks

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



Advertise | Privacy | Mobile
Web01 | 2.8.141022.1 | Last Updated 17 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