Wie kommt man in C# an die "Date Taken" Property eines Bildes?
FileInfo fi = new FileInfo(file);
DateTime modified = fi.LastWriteTime;
DateTime created = fi.CreationTime;
Liefert nicht das genaue Datum zurück, wann das Bild aufgenommen wurde.
Ich brauche aber das genaue Datum, der Aufnahme. Jemand 'ne Idee wie ich da ran komme?
Ich benutze .Net 6.0
Der komplette Code bisher:
static void Main(string[] args)
{
Random rand = new Random();
foreach (var filePath in Directory.GetDirectories("G:\\230419 Pictures"))
{
string[] files = Directory.GetFiles(filePath);
foreach (string file in files)
{
try
{
FileInfo fi = new FileInfo(file);
DateTime modified = fi.LastWriteTime;
DateTime created = fi.CreationTime;
string dateTimeZeug = (modified < created ? modified : created).ToString("yyyy-MM-dd_hh-mm-ss");
bool createdSuccessfully = false;
int tries = 0;
while (!createdSuccessfully)
{
if (tries == 0)
{
try
{
File.Move(file, Path.Combine(filePath, dateTimeZeug + fi.Extension));
createdSuccessfully = true;
}
catch (Exception e)
{
tries++;
continue;
}
}
else
{
if (tries > 100)
{
throw new Exception("Error: " + file);
}
else
{
try
{
File.Move(file, Path.Combine(filePath, dateTimeZeug + "-" + rand.Next(100) + fi.Extension));
createdSuccessfully = true;
}
catch (Exception e)
{
tries++;
continue;
}
}
}
tries++;
}
}
catch (Exception e)
{
Console.WriteLine(file);
}
}
}
Console.WriteLine("Success!");
}
1 Antwort
"Date Taken" ist ein Wert in den EXIF Daten des Bilden. Das Stichwort ist also das Auslesen von EXIF Daten.
Eine Möglichkeit, wo du fündig werden könntest, sind die PropertyItems wenn du das Bild als System.Drawing.Image/Imaging geladen hast.
Alternativ könntest du auch eine externe Bibliothek dafür einbinden. Ein Beispiel findest du hier: https://github.com/drewnoakes/metadata-extractor-dotnet
Das in der Readme genannte Beispiel über DateTime müsste dein Feld entsprechen.
Es wird bestimmt noch weitere Bibliotheken geben. Du könntest auch manuell in den EXIF Bytes des Bildes stöbern.