Hi,

having a bit of a head-scratcher time here.

I have a FOR loop that searches through a Directory for images, collects the EXIF DateTimeOriginal information form each image and then is supposed to output this collected data inside an HTML TABLE.

I know that the FOR loop does exactly what it should and that my HTML page creation also works as it should but I cannot get both pieces of coding to work nicely together.

Be aware there maybe a lot of code to follow, to try a show my point.

Here is my working FOR loop:

string[] filePaths = Directory.GetFiles(@"C:\Users\1CM69\Pics & Vids\Archives\Family\2002\", "*.*", SearchOption.AllDirectories);
                foreach (string file in filePaths)
                {
                    try
                    {

                        Bitmap MyPhoto = new Bitmap(file);
                        const int IDDateTimeOriginal = 36867;
                        PropertyItem TakenDate = MyPhoto.GetPropertyItem(IDDateTimeOriginal);
                        Encoding ascii = Encoding.ASCII;
                        string DateTaken = ascii.GetString(TakenDate.Value, 0, TakenDate.Len - 1);
                    }


                    catch (ArgumentException) //if the property doesn't exists
                    {
                        string DateTaken = "MISSING ENTRY";
                    }
                    }

Now here is the relevent code snippet showing this loop in place:

 private void bAction_Click(object sender, EventArgs e)
        {

            if (cbFolders.SelectedIndex < 0)
            {
                MessageBox.Show("You Have Not Chosen a Year Yet!");
            }
            else
            {

                string[] filePaths = Directory.GetFiles(@"C:\Users\1CM69\Pics & Vids\Archives\Family\2002\", "*.*", SearchOption.AllDirectories);
                foreach (string file in filePaths)
                {
                    try
                    {

                        Bitmap MyPhoto = new Bitmap(file);
                        const int IDDateTimeOriginal = 36867;
                        PropertyItem TakenDate = MyPhoto.GetPropertyItem(IDDateTimeOriginal);
                        Encoding ascii = Encoding.ASCII;
                        string DateTaken = ascii.GetString(TakenDate.Value, 0, TakenDate.Len - 1);
                    }


                    catch (ArgumentException) //if the property doesn't exists
                    {
                        string DateTaken = "MISSING ENTRY";
                    }
                    }



                    /// CREATE NEW DOCUMENT
                    var xDocument = new XDocument(
                    new XDocumentType("html", null, null, null),

                    /// OPEN <HTML> TAG
                    new XElement("html",

                        /// OPEN & CLOSE <HEAD> TAG
                        new XElement("head"),

                        /// OPEN & CLOSE <TITLE> TAG
                        new XElement("title", "Pics and Vids " + cbFolders.SelectedItem),

                        /// OPEN <BODY> TAG
                        new XElement("body",
                            new XAttribute("bgcolor", "#c8cbcc"),
                            new XAttribute("text", "#000000"),

                            /// OPEN <H1> TAG
                            new XElement("h1",
                                new XAttribute("align", "center"),

                                /// OPEN <B> TAG
                                new XElement("b",

                                    /// OPEN & CLOSE <FONT> TAG
                                    new XElement("font",
                                        new XAttribute("face", "arial"),
                                        new XAttribute("size", "+4"),
                                        new XAttribute("color", "#6305f0"),
                                        "Pics and Vids " + cbFolders.SelectedItem)

                                /// CLOSE <B> TAG
                                )

                            /// CLOSE <H1> TAG
                            ),

                                /// OPEN <P> TAG
                                new XElement("p",
                        ///new XAttribute("align", "center"),

                                    /// OPEN <CENTER> TAG
                                    new XElement("center",

                                    /// OPEN <TABLE> TAG
                                    new XElement("table",
                                        new XAttribute("cellpadding", "10"),
                                        new XAttribute("style", "border-width: 4px; border-spacing: 2px; border-style: double; border-color: rgb(99, 5, 240); border-collapse: separate; background-color: rgb(255, 245, 238);"),

                                        /// OPEN <TR> TAG
                                        new XElement("tr",

                                            /// OPEN <TD> TAG
                                            new XElement("td",
                                                new XAttribute("style", "border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;"),
                                                new XAttribute("align", "center"),

                                            /// OPEN <B> TAG
                                            new XElement("b",

                                                /// OPEN & CLOSE <FONT> TAG
                                                new XElement("font",
                                                    new XAttribute("face", "arial"),
                                                    new XAttribute("size", "+1"),
                                                    new XAttribute("color", "#6305f0"),
                                                    "Thumbnail")

                                            /// CLOSE <B> TAG    
                                            )

                                            /// CLOSE <TD> TAG    
                                            ),

                                            /// OPEN <TD> TAG
                                            new XElement("td",
                                                new XAttribute("style", "border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;"),
                                                new XAttribute("align", "center"),

                                            /// OPEN <B> TAG
                                            new XElement("b",

                                                /// OPEN & CLOSE <FONT> TAG
                                                new XElement("font",
                                                    new XAttribute("face", "arial"),
                                                    new XAttribute("size", "+1"),
                                                    new XAttribute("color", "#6305f0"),
                                                    "Filename")

                                            /// CLOSE <B> TAG    
                                            )

                                            /// CLOSE <TD> TAG    
                                            ),

                                            /// OPEN <TD> TAG
                                            new XElement("td",
                                                new XAttribute("style", "border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;"),
                                                new XAttribute("align", "center"),

                                            /// OPEN <B> TAG
                                            new XElement("b",

                                                /// OPEN & CLOSE <FONT> TAG
                                                new XElement("font",
                                                    new XAttribute("face", "arial"),
                                                    new XAttribute("size", "+1"),
                                                    new XAttribute("color", "#6305f0"),
                                                    "Date Taken")

                                            /// CLOSE <B> TAG    
                                            )

                                            /// CLOSE <TD> TAG    
                                            )


                                        /// CLOSE <TR> TAG
                                        ),

                                        /// OPEN <TR> TAG
                                        new XElement("tr",

                                            /// OPEN <TD> TAG
                                            new XElement("td",
                                                new XAttribute("style", "border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;"),
                                                new XAttribute("align", "center"),
                                                new XAttribute("valign", "middle"),

                                                /// OPEN <A> TAG
                                                new XElement("a",
                                                    new XAttribute("href", ""),
                                                    new XAttribute("target", "_new"),

                                                    /// OPEN <IMG> TAG
                                                    new XElement("img",
                                                        new XAttribute("src", ""),
                                                        new XAttribute("width", "100px"),
                                                        new XAttribute("border", "0"))

                                                //// CLOSE <A> TAG
                                                )

                                            //// CLOSE <TD> TAG
                                            ),

                                            /// OPEN <TD> TAG
                                            new XElement("td",
                                                new XAttribute("style", "border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;"),
                                                new XAttribute("align", "center"),
                                                new XAttribute("valign", "middle"),


                                                /// OPEN <B> TAG
                                                new XElement("b", "Filey Namey"


                                                //// CLOSE <B> TAG
                                                )

                                            //// CLOSE <TD> TAG
                                            ),

                                            /// OPEN <TD> TAG
                                            new XElement("td",
                                                new XAttribute("style", "border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;"),
                                                new XAttribute("align", "center"),
                                                new XAttribute("valign", "middle"),


                                                /// OPEN <B> TAG
                                                new XElement("b", DateTaken


                                                //// CLOSE <B> TAG
                                                )

The ERROR message I am receiving is:

The name 'DateTaken' does not exist in the current context

I can understand why I am getting this error, as the String DateTaken is outside of the block that constructs the b tag where I wish to actually use it.

What I cannot seem to fathom is how to recify this.

I have tried various ways to make the variable Global but to no avail, I have tried including the HTML page constructor block within the FOR loop but this gave huge errors.

Any help would be appreciated.

Kind regards..,

Kirk

DateTaken is defined in the inner block of the try and catch. That means, as soon as the block closes, the variable is out-of-scope. You'll need to define the string before the try to be able to use it after.

string DateTaken;
try 
{
    // omitted
    DateTaken = ascii.GetString(TakenDate.Value, 0, TakenDate.Len - 1);
}
catch
{
    DateTaken = "Missing";
}

// omitted
new XElement("b", DateTaken)

Hi,

thanks for the response and this did indeed solve that particular ERROR, although it has now thrown up more issues.

When the program is run it does as it should and searches the files within the specified directory but it is supposed to dynamically generate the required amount of tags to populate the table.

It should be counting how many files are in the directory and then repeating the tags from line 153 in the above post of mine i.e. /// OPEN <TR> TAG

to way below what I have actually posted code wise.

Therefore each file inside the searchable directory should cause the program to fire off a new TR inside the TABLE.

At the moment all it does is populate the one TR with the DateTimeOriginal of the last file in the directory.

This is the produced HTML file output:

<!DOCTYPE html >
<html>
    <head />
    <title>Pics and Vids 2002</title>
    <body bgcolor="#c8cbcc" text="#000000">
        <h1 align="center">
            <b>
                <font face="arial" size="+4" color="#6305f0">Pics and Vids 2002</font>
            </b>
        </h1>
        <p>
            <center>
                <table cellpadding="10" style="border-width: 4px; border-spacing: 2px; border-style: double; border-color: rgb(99, 5, 240); border-collapse: separate; background-color: rgb(255, 245, 238);">
                    <tr>
                        <td style="border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;" align="center">
                            <b>
                                <font face="arial" size="+1" color="#6305f0">Thumbnail</font>
                            </b>
                        </td>
                        <td style="border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;" align="center">
                            <b>
                                <font face="arial" size="+1" color="#6305f0">Filename</font>
                            </b>
                        </td>
                        <td style="border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;" align="center">
                            <b>
                                <font face="arial" size="+1" color="#6305f0">Date Taken</font>
                            </b>
                        </td>
                    </tr>
                    <tr>
                        <td style="border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;" align="center" valign="middle">
                            <a href="" target="_new">
                                <img src="" width="100px" border="0" />
                            </a>
                        </td>
                        <td style="border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;" align="center" valign="middle">
                            <b>Filey Namey</b>
                        </td>
                        <td style="border-width: 1px; padding: 10px; border-style: inset; border-color: gray; background-color: white;" align="center" valign="middle">
                            <b>2002:12:26 14:53:11</b>
                        </td>
                    </tr>
                </table>
            </center>
        </p>
    </body>
</html>

You can plainly see the apart from the TR containing the column headers, there is only one other TR in the table although the actual directory contains 38 images, so there should be a further 37 TR each containing the DateTimeOriginal data relating to each image held within.

If I could ask for further help on this point I would be very humble.

Regards..,

Kirk

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.