/// <summary> /// Reads the embedded image data from an image already in memory. /// </summary> /// <param name="image">Image in memory in which to read the embedded data</param> /// <returns>ImageData class with the embedded data.</returns> /// <exception cref="System.Exception"> /// Image loaded does not have all embedded data expected /// </exception> public static ImageInfo ReadImage(Image image, ImageInfo retData) { float num = 0; float den = 0; int degrees; int minutes; double seconds; String str; int height = image.Height; int width = image.Width; lock (image) { PropertyItem[] propItems = image.PropertyItems; PropertyItem gpsTime = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.gpsTime); PropertyItem gpsLat = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.gpsLat); PropertyItem gpsLong = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.gpsLong); PropertyItem gpsAlt = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.gpsAlt); PropertyItem yaw = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.yaw); PropertyItem gpsLatRef = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.gpsLatRef); PropertyItem gpsLongRef = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.gpsLongRef); PropertyItem dateTimeTaken = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.dateTimeTaken); PropertyItem cameraModel = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.cameraModel); PropertyItem focalLength = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.focalLength); PropertyItem title = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.title); PropertyItem comment = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.comment); PropertyItem author = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.author); PropertyItem keywords = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.keywords); PropertyItem subject = propItems.FirstOrDefault(p => p.Id == (int)ProportyItemId.subject); if (cameraModel != null) { str = asciiEncoder.GetString(cameraModel.Value); retData.CameraModel = str; } if (height > 0) { retData.Height = height; } if (width > 0) { retData.Width = width; } if (dateTimeTaken != null) { str = asciiEncoder.GetString(dateTimeTaken.Value); str = str.Trim(); str = str.Substring(0, str.Length - 1); StringBuilder s = new StringBuilder(str); s[4] = '/'; s[7] = '/'; retData.DateTimeCreated = DateTime.Parse(s.ToString()); } if (gpsTime != null) { retData.GPSHours = BitConverter.ToInt32(gpsTime.Value, 0); retData.GPSMinutes = BitConverter.ToInt32(gpsTime.Value, 8); num = (float)BitConverter.ToInt32(gpsTime.Value, 16); den = (float)BitConverter.ToInt32(gpsTime.Value, 20); retData.GPSSeconds = num / den; } if (gpsAlt != null) { num = (float)BitConverter.ToInt32(gpsAlt.Value, 0); den = (float)BitConverter.ToInt32(gpsAlt.Value, 4); retData.GPSAltitude = num / den; } // Latitude if (gpsLat != null) { degrees = BitConverter.ToInt32(gpsLat.Value, 0); minutes = BitConverter.ToInt32(gpsLat.Value, 8); seconds = (double)BitConverter.ToInt32(gpsLat.Value, 16) / (double)BitConverter.ToInt32(gpsLat.Value, 20); retData.GPSLatitudeDegrees = System.Math.Round((double)(degrees * 3600 + minutes * 60 + seconds) / 3600d, 6); } // Longitude if (gpsLong != null) { degrees = BitConverter.ToInt32(gpsLong.Value, 0); minutes = BitConverter.ToInt32(gpsLong.Value, 8); seconds = (double)BitConverter.ToInt32(gpsLong.Value, 16) / (double)BitConverter.ToInt32(gpsLong.Value, 20); retData.GPSLongitudeDegrees = System.Math.Round((double)(degrees * 3600 + minutes * 60 + seconds) / 3600d, 6); } if (gpsLatRef != null) { retData.GPSLatitudeRef = asciiEncoder.GetString(gpsLatRef.Value, 0, 1); } if (gpsLongRef != null) { retData.GPSLongitudeRef = asciiEncoder.GetString(gpsLongRef.Value, 0, 1); } if (yaw != null) { num = (float)BitConverter.ToInt32(yaw.Value, 0); den = (float)BitConverter.ToInt32(yaw.Value, 4); retData.Yaw = num / den; } if (focalLength != null) { retData.FocalLength = BitConverter.ToInt32(focalLength.Value, 0); } if (title != null) { retData.Title = title.GetMetaValueString(); } if (comment != null) { retData.Comments = comment.GetMetaValueString(); } if (author != null) { retData.Author = author.GetMetaValueString(); } if (keywords != null) { retData.Keywords = keywords.GetMetaValueString(); } if (subject != null) { retData.Subject = subject.GetMetaValueString(); } } return(retData); }