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