Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have

C#
string[] filenames = Directory.GetFiles(@"C:\temp\test", "*.png", SearchOption.AllDirectories);

string[] myArr = new string[10];


C#
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.

C#
private void populateArray(string binVal)
    {
        for (int j = 0; j < myArr.Length; j++)
        {
            //for (int j = 0; j < 2; j++)
            myArr[j] = (binVal);
        }
Posted
Updated 17-Nov-12 4:04am
v2
Comments
Vivek Krishnamurthy 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...

You could just modify your existing foreach loop:
C#
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:
 
Share this answer
 
Comments
BillWoodruff 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 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:
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:
C#
// 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.
 
Share this answer
 
v3
Comments
datt265 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)



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