Im trying to write metadata to images using the following code.But when i try to add the date field an error is thrown String was not recognized as a valid DateTime
using (Stream bitmapStream = File.Open(fileName, FileMode.Open,
Here Date is the value obtained from DatePicker.
date.tostring()->29/01/2015 06:35:13 PM
date.ToShortDateString()-> 29/01/2015
Stack Trace:
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.Convert.ToDateTime(String value, IFormatProvider provider)
at System.Windows.Media.Imaging.BitmapMetadata.set_DateTaken(String value)
at myprogram.Main.worker_DoWork(Object sender, DoWorkEventArgs e)
Based on the exception and the example in the documentation, I suspect you need to format this as a US short date. The simplest way of doing this is probably just to use the invariant culture. For example:
metadata.DateTaken = date.ToString("d", CultureInfo.InvariantCulture)
(Frankly it's a shame that it's a string property rather than a DateTime
, but there we go...)
See more on this question at Stackoverflow