public static DateTime? GetTakenTime(string fullName) { ExifReader reader = null; try { reader = new ExifReader(fullName); DateTime datePictureTaken; if (reader.GetTagValue(ExifTags.DateTimeOriginal, out datePictureTaken)) { Debug.WriteLine(string.Format("The picture was taken on {0}", datePictureTaken)); return datePictureTaken; } if (reader.GetTagValue(ExifTags.DateTimeDigitized, out datePictureTaken)) { Debug.WriteLine(string.Format("The picture was taken on {0}", datePictureTaken)); return datePictureTaken; } if (reader.GetTagValue(ExifTags.DateTime, out datePictureTaken)) { Debug.WriteLine(string.Format("The picture was taken on {0}", datePictureTaken)); return datePictureTaken; } if (reader.GetTagValue(ExifTags.GPSDateStamp, out datePictureTaken)) { Debug.WriteLine(string.Format("The picture was taken on {0}", datePictureTaken)); return datePictureTaken; } Debug.WriteLine(string.Format("The picture was taken on {0}", datePictureTaken)); return datePictureTaken; } catch (Exception ex) { // Something didn't work! Debug.WriteLine(ex.ToString()); // MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); if (reader != null) reader.Dispose(); } return null; }
public void Parse2(string filename) { int cameraNameOffset = 0x32E; int distortionCorrectionOffset = 0x340; UInt16[] distortionValues = new UInt16[16]; string cameraName; string lensName; Distortion distortion; Distortion distortion2; double focalLength = 0; double aperture = 0; byte[] fileBytes = File.ReadAllBytes(filename); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 32; i++) { sb.Append(Convert.ToString(fileBytes[distortionCorrectionOffset + i], 16).PadLeft(2, '0')); } FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); fs.Seek(cameraNameOffset, 0); BinaryReader binaryReader = new BinaryReader(fs); cameraName = (new string(Util.ReplaceNULWithBlanks(binaryReader.ReadChars(18)))).Trim(); for (int i = 0; i < 16; i++) { distortionValues[i] = ReverseBytes(binaryReader.ReadUInt16()); } //fs.Seek(GetLensOffsetFromCameraName((new string(Util.ReplaceNULWithBlanks(cameraName)).Trim())), 0); //BinaryReader binaryReaderLensName = new BinaryReader(fs); //lensName = binaryReaderLensName.ReadChars(34); lensName = offsets[cameraName].GetLensName(fs); distortion.n = ReverseBytes(distortionValues[12]); distortion.scale = 1.0 / (1.0 + (ReverseBytes(distortionValues[5]) / 32768.0)); distortion.a = distortion.scale * (ReverseBytes(distortionValues[8]) / 32768.0); distortion.b = distortion.scale * (ReverseBytes(distortionValues[4]) / 32768.0); distortion.c = distortion.scale * (ReverseBytes(distortionValues[11]) / 32768.0); distortion2.n = distortionValues[12]; distortion2.scale = 1.0 / (1.0 + (distortionValues[5] / 32768.0)); distortion2.a = distortion2.scale * (distortionValues[8] / 32768.0); distortion2.b = distortion2.scale * (distortionValues[4] / 32768.0); distortion2.c = distortion2.scale * (distortionValues[11] / 32768.0); //EM5 vielleicht:0xC20E oder 0x2854 bis 0x2863 ExifReader reader = null; try { reader = new ExifReader(filename, 0x800); //string CameraMaker; //string CameraModel; //reader.GetTagValue<String>(ExifTags.Make, out CameraMaker); //reader.GetTagValue<String>(ExifTags.Model, out CameraModel); reader.GetTagValue<Double>(ExifTags.FocalLength, out focalLength); reader.GetTagValue<Double>(ExifTags.ApertureValue, out aperture); } catch (Exception ex) { // Something didn't work! if (reader != null) reader.Dispose(); } //using (FileStream fs2 = new FileStream("test.ab", FileMode.OpenOrCreate)) //{ // using (BinaryWriter w = new BinaryWriter(fs2)) // { // for (int i = 0; i < 16; i++) // { // w.Write(distortionValues[i]); // } // } //} //distortionValues[10]= 1; //int checksumResult = Verify_checksums(distortionValues); WriteOutputToFile(cameraName, lensName, focalLength.ToString(), aperture.ToString(), distortionValues, distortion, distortion2, filename); }
/// <summary> /// Reads the meta data using ExifLib (much more faster!) /// </summary> /// <returns>true if date and camera model could be read from the file, otherwise false</returns> public bool ReadMetaDataExifLib() { ExifReader exifReader = null; try { exifReader = new ExifReader(this.OriginalFileName); DateTime taken; if (!exifReader.GetTagValue<DateTime>(ExifTags.DateTimeOriginal, out taken)) { // If date is not contained in the EXIF information use the file modified date (LastWriteTime). // This is usually the time the file was saved in the camera FileInfo fi = new FileInfo(this.OriginalFileName); taken = fi.LastWriteTime; } this.Taken = taken; string dateTakenIso = this.Taken.ToString("yyyyMMdd_HHmmss"); string model = string.Empty; string suffix = string.Empty; if (exifReader.GetTagValue<string>(ExifTags.Model, out model)) { string entry = cameraModelDictionary.FirstOrDefault(e => model.IndexOf(e.Key, StringComparison.OrdinalIgnoreCase) >= 0).Value; suffix = string.IsNullOrEmpty(entry) ? string.Empty : "_" + entry; } this.NewFileName = string.Format("IMG_{0}{1}.jpg", dateTakenIso, suffix); this.NearlyOriginalFileName = string.Format("{0}{1}.jpg", Path.GetFileNameWithoutExtension(this.OriginalFileName), suffix); return true; } catch (ExifLibException e) { // e.g. if no EXIF information is present at all Debug.WriteLine("{0} in Datei {1}: {2}", e.GetType().Name, this.OriginalFileName, e.Message); return false; } finally { if (exifReader != null) { exifReader.Dispose(); } } }
private void btnPopulate_Click(object sender, EventArgs e) { if (!File.Exists(txtFileName.Text)) { MessageBox.Show(this, "Please enter a valid filename", "File not found", MessageBoxButtons.OK); return; } ExifReader reader = null; try { reader = new ExifReader(txtFileName.Text); // Get the image thumbnail (if present) var thumbnailBytes = reader.GetJpegThumbnailBytes(); if (thumbnailBytes == null) pictureBoxThumbnail.Image = null; else { using (var stream = new MemoryStream(thumbnailBytes)) pictureBoxThumbnail.Image = Image.FromStream(stream); } // To read a single field, use code like this: /* DateTime datePictureTaken; if (reader.GetTagValue<DateTime>(ExifTags.DateTimeDigitized, out datePictureTaken)) { MessageBox.Show(this, string.Format("The picture was taken on {0}", datePictureTaken), "Image information", MessageBoxButtons.OK); } */ // Parse through all available fields string props = ""; foreach (ushort tagID in Enum.GetValues(typeof(ExifTags))) { object val; if (reader.GetTagValue(tagID, out val)) { // Arrays don't render well without assistance. string renderedTag; if (val is Array) { renderedTag = ""; foreach (object item in (Array)val) renderedTag += item + ","; renderedTag = renderedTag.Substring(0, renderedTag.Length - 1); } else renderedTag = val.ToString(); props += string.Format("{0}:{1}\r\n", Enum.GetName(typeof(ExifTags), tagID), renderedTag); } } // Remove the last carriage return props = props.Substring(0, props.Length - 2); txtFields.Text = props; } catch (Exception ex) { // Something didn't work! MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); if (reader != null) reader.Dispose(); } }
private String GetExifDate(string path) { String strDate; try { ExifReader exifData = new ExifReader(path); exifData.GetTagValue(ExifTags.DateTimeOriginal, out strDate); exifData.Dispose(); // Close file } catch (ExifLibException ex) { Console.WriteLine(ex.ToString()); return String.Empty; } catch(FileNotFoundException ex) { Console.WriteLine(ex.ToString()); return String.Empty; } if (null == strDate || String.Empty == strDate) return String.Empty; strDate = strDate.Replace(":", "-"); return strDate; }