Click here to Skip to main content
12,695,325 members (27,113 online)
Rate this:
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);

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

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 2:27am
Updated 17-Nov-12 5:04am
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...
Rate this: bad
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) {
But I will admit I am at a loss as to why you would want to do this in the first place! :laugh:
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:
Rate this: bad
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.


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.

// 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)
        fileNames = Directory.GetFiles(filePath, fileType, SearchOption.AllDirectories);
    catch (DirectoryNotFoundException ex)
        MessageBox.Show("Error: " + ex.Message);
        // we're dead: give up the ghost !


    // 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.
bmw318mt 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?


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2017
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