protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (Page.IsPostBack) { string cmd = Page.Request.Form[this.ClientID + "_update"]; string desc = Page.Request.Form[this.ClientID + "_description"]; string artist = Page.Request.Form[this.ClientID + "_artist"]; string copyright = Page.Request.Form[this.ClientID + "_copyright"]; if (cmd == "Update") { if (imageUrl == string.Empty) { throw(new Exception("Empty ImageUrl; cannot update EXIF data.")); } string mappedImageUrl = HttpContext.Current.Server.MapPath(sourceDirectory + "/" + imageUrl); ExifProperties.WriteDescriptionInImage(mappedImageUrl, artist, copyright, desc); if (this.EnablePreviews) { FileInfo file = new FileInfo(mappedImageUrl); string mappedThumbDirectory = HttpContext.Current.Server.MapPath(previewDirectory); string thumbHash = UniqueID + "_" + file.FullName.GetHashCode().ToString() + file.Extension; ExifProperties.WriteDescriptionInImage(mappedThumbDirectory + "\\" + thumbHash, artist, copyright, desc); } } } }
private void UpdateImageProperty() { PropertyList = new ObservableCollection <PropertyViewer>(); foreach (Property prop in SelectedImage.properties) { PropertyViewer propertyViewer = new PropertyViewer(); ExifProperties currentProp = (ExifProperties)prop.ExifCode; propertyViewer.PropertyName = EnumHelper.GetEnumDescription(currentProp); propertyViewer.PropertyValue = prop.Value; PropertyList.Add(propertyViewer); } }
public static string GetProperty(string fileName, ExifProperties property) { string value = ""; using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { using (Image image = Image.FromStream(fs, false, false)) { var propertyItem = image.GetPropertyItem((int)property); value = Encoding.UTF8.GetString(propertyItem.Value, 0, propertyItem.Len - 1); } } return(value); }
/// <summary> /// Read all <see cref="System.Drawing.Imaging.PropertyItem"/>s from the specified /// <paramref name="jpegFilePath"/> and translate them to a dictionary containing all possible /// EXIF properties and their respective values. When a value is not set for the specified /// image, an empty string is provided. /// </summary> /// <param name="jpegFilePath">The path to an existing Jpeg file.</param> /// <exception cref="System.ArgumentException">Thrown when the specified <paramref name="jpegFilePath"/> /// does not point to an existing, valid Jpeg file.</exception> public static Dictionary<string, string> Read(string jpegFilePath) { var propertyItems = new List<PropertyItem>(); var exifProperties = new ExifProperties(); var jpegProperties = new Dictionary<string, string>(); foreach (string exifProperty in exifProperties.Values) jpegProperties.Add(exifProperty, string.Empty); using (FileStream stream = new FileStream(jpegFilePath, FileMode.Open, FileAccess.Read)) { Image image = System.Drawing.Image.FromStream(stream, true, false); propertyItems.AddRange(image.PropertyItems); } foreach (var property in propertyItems) { if (!exifProperties.ContainsKey(property.Id)) continue; string propertyName = exifProperties[property.Id]; string propertyValue = string.Empty; if (property.Type == 0x1) { propertyValue = property.Value[0].ToString().TrimEnd('\0'); } else if (property.Type == 0x2) { // 2 = ASCII // An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with null. propertyValue = Encoding.ASCII.GetString(property.Value).TrimEnd('\0'); } else if (property.Type == 0x3) { // 3 = unsigned short switch (property.Id) { case 0x8827: // ISO propertyValue = "ISO-" + ConvertToUInt16(property.Value).ToString(); break; case 0xA217: // sensing method { switch (ConvertToUInt16(property.Value)) { case 1: propertyValue = "Not defined"; break; case 2: propertyValue = "One-chip color area sensor"; break; case 3: propertyValue = "Two-chip color area sensor"; break; case 4: propertyValue = "Three-chip color area sensor"; break; case 5: propertyValue = "Color sequential area sensor"; break; case 7: propertyValue = "Trilinear sensor"; break; case 8: propertyValue = "Color sequential linear sensor"; break; default: propertyValue = " reserved"; break; } } break; case 0x8822: // aperture switch (ConvertToUInt16(property.Value)) { case 0: propertyValue = "Not defined"; break; case 1: propertyValue = "Manual"; break; case 2: propertyValue = "Normal program"; break; case 3: propertyValue = "Aperture priority"; break; case 4: propertyValue = "Shutter priority"; break; case 5: propertyValue = "Creative program (biased toward depth of field)"; break; case 6: propertyValue = "Action program (biased toward fast shutter speed)"; break; case 7: propertyValue = "Portrait mode (for closeup photos with the background out of focus)"; break; case 8: propertyValue = "Landscape mode (for landscape photos with the background in focus)"; break; default: propertyValue = "reserved"; break; } break; case 0x9207: // metering mode switch (ConvertToUInt16(property.Value)) { case 0: propertyValue = "unknown"; break; case 1: propertyValue = "Average"; break; case 2: propertyValue = "CenterWeightedAverage"; break; case 3: propertyValue = "Spot"; break; case 4: propertyValue = "MultiSpot"; break; case 5: propertyValue = "Pattern"; break; case 6: propertyValue = "Partial"; break; case 255: propertyValue = "Other"; break; default: propertyValue = "reserved"; break; } break; case 0x9208: // light source { switch (ConvertToUInt16(property.Value)) { case 0: propertyValue = "unknown"; break; case 1: propertyValue = "Daylight"; break; case 2: propertyValue = "Fluorescent"; break; case 3: propertyValue = "Tungsten"; break; case 17: propertyValue = "Standard light A"; break; case 18: propertyValue = "Standard light B"; break; case 19: propertyValue = "Standard light C"; break; case 20: propertyValue = "D55"; break; case 21: propertyValue = "D65"; break; case 22: propertyValue = "D75"; break; case 255: propertyValue = "other"; break; default: propertyValue = "reserved"; break; } } break; case 0x9209: { switch (ConvertToUInt16(property.Value)) { case 0: propertyValue = "Flash did not fire"; break; case 1: propertyValue = "Flash fired"; break; case 5: propertyValue = "Strobe return light not detected"; break; case 7: propertyValue = "Strobe return light detected"; break; default: propertyValue = "reserved"; break; } } break; default: propertyValue = ConvertToUInt16(property.Value).ToString(); break; } } else if (property.Type == 0x4) { // 4 = unsigned int propertyValue = ConvertToUInt32(property.Value).ToString(); } else if (property.Type == 0x5) { // 5 = rational of two unsigned ints byte[] n = new byte[property.Len / 2]; byte[] d = new byte[property.Len / 2]; Array.Copy(property.Value, 0, n, 0, property.Len / 2); Array.Copy(property.Value, property.Len / 2, d, 0, property.Len / 2); uint a = ConvertToUInt32(n); uint b = ConvertToUInt32(d); Rational r = new Rational(a, b); switch (property.Id) { case 0x9202: // aperture propertyValue = "F/" + Math.Round(Math.Pow(Math.Sqrt(2), r.ToDouble()), 2).ToString(); break; case 0x920A: propertyValue = r.ToDouble().ToString(); break; case 0x829A: propertyValue = r.ToDouble().ToString(); break; case 0x829D: // F-number propertyValue = "F/" + r.ToDouble().ToString(); break; default: propertyValue = r.ToString("/"); break; } } else if (property.Type == 0x7) { // 7 = undefined // A byte that can take any value depending on the field switch (property.Id) { case 0xA300: { if (property.Value[0] == 3) { propertyValue = "DSC"; } else { propertyValue = "reserved"; } break; } case 0xA301: if (property.Value[0] == 1) propertyValue = "A directly photographed image"; else propertyValue = "Not a directly photographed image"; break; default: propertyValue = "-"; break; } } else if (property.Type == 0x9) { // 9 = signed int propertyValue = ConvertToInt32(property.Value).ToString(); } else if (property.Type == 0xA) { // 10 = rational of two signed ints byte[] n = new byte[property.Len / 2]; byte[] d = new byte[property.Len / 2]; Array.Copy(property.Value, 0, n, 0, property.Len / 2); Array.Copy(property.Value, property.Len / 2, d, 0, property.Len / 2); int a = ConvertToInt32(n); int b = ConvertToInt32(d); Rational r = new Rational(a, b); switch (property.Id) { case 0x9201: // shutter speed propertyValue = "1/" + Math.Round(Math.Pow(2, r.ToDouble()), 2).ToString(); break; case 0x9203: propertyValue = Math.Round(r.ToDouble(), 4).ToString(); break; default: propertyValue = r.ToString("/"); break; } } jpegProperties[propertyName] = propertyValue; } return jpegProperties; }
/// <summary> /// Render this control to the output parameter specified. /// </summary> /// <param name="output"> The HTML writer to write out to </param> protected override void Render(HtmlTextWriter output) { if (!controlValidated) { output.Write("An exception has occured, the URL specified may be incorrect."); return; } if (imageUrl.IndexOf("..") > -1) { output.Write("Illegal characters in url."); return; } string mappedImageUrl = HttpContext.Current.Server.MapPath(sourceDirectory + "/" + imageUrl); if (File.Exists(mappedImageUrl)) { ExifProperties exif; FileInfo file = new FileInfo(mappedImageUrl); // get the path of the image by removing the filename from the image url string imagePath = this.ImageUrl.Replace(file.Name, ""); if (imagePath.StartsWith("/")) { imagePath = imagePath.Substring(1, imagePath.Length - 1); } if (imagePath.EndsWith("/")) { imagePath = imagePath.Substring(0, imagePath.Length - 1); } // get the displayed path string displayPath = string.Empty; if (rootName != string.Empty) { if (rootName.EndsWith("/")) { displayPath = rootName.Substring(0, rootName.Length - 1); } else { displayPath = rootName; } } displayPath += imagePath; // Find Next and Previous Images string nextImagePath = string.Empty; string previousImagePath = string.Empty; string extension; bool isFound = false; FileInfo[] files = file.Directory.GetFiles(); for (int i = 0; i < files.Length; i++) { if (file.FullName == files[i].FullName) { isFound = true; continue; } extension = files[i].Extension.ToLower(); if (extension == ".gif" | extension == ".jpg" | extension == ".jpeg" | extension == ".png") { if (!isFound) { previousImagePath = imagePath + "/" + files[i].Name; } else if (isFound) { nextImagePath = imagePath + "/" + files[i].Name; break; } } } output.Write("\n<table id=\"{0}\"", UniqueID); if (CssClass != "") { output.Write(" class=\"{0}\"", CssClass); } if (!cellPadding.IsEmpty) { output.Write(" cellPadding=\"{0}\"", cellPadding.ToString()); } if (!cellSpacing.IsEmpty) { output.Write(" cellPadding=\"{0}\"", cellSpacing.ToString()); } if (!BorderWidth.IsEmpty) { output.Write(" border=\"{0}\"", BorderWidth.ToString()); } if (!Width.IsEmpty) { output.Write(" width=\"{0}\"", Width.ToString()); } if (!Height.IsEmpty) { output.Write(" height=\"{0}\"", Height.ToString()); } output.Write(">\n"); output.Indent++; output.WriteFullBeginTag("tr"); output.WriteLine(); output.Indent++; output.WriteBeginTag("td"); output.WriteAttribute("colspan", "2"); output.WriteAttribute("class", pathCssClass); output.WriteAttribute("width", "100%"); output.Write(HtmlTextWriter.TagRightChar); output.WriteLine(); output.Indent++; output.WriteBeginTag("span"); output.WriteAttribute("style", "float:right;"); output.Write(HtmlTextWriter.TagRightChar); if (Page.Request.UrlReferrer != null) { output.WriteBeginTag("a"); output.WriteAttribute("class", this.navLinkCssClass); output.WriteAttribute("href", "./?subfolder=" + imagePath); output.Write(HtmlTextWriter.TagRightChar); output.Write("Back"); output.WriteEndTag("a"); } if (previousImagePath != string.Empty) { output.Write(" "); output.WriteBeginTag("a"); output.WriteAttribute("class", this.navLinkCssClass); output.WriteAttribute("href", "?src=" + previousImagePath.Replace(" ", "%20")); output.Write(HtmlTextWriter.TagRightChar); output.Write("Previous"); output.WriteEndTag("a"); } if (nextImagePath != string.Empty) { output.Write(" "); output.WriteBeginTag("a"); output.WriteAttribute("class", this.navLinkCssClass); output.WriteAttribute("href", "?src=" + nextImagePath.Replace(" ", "%20")); output.Write(HtmlTextWriter.TagRightChar); output.Write("Next"); output.WriteEndTag("a"); } output.WriteEndTag("span"); output.WriteLine(this.RootName + "/" + imagePath); output.WriteLine(); output.Indent--; output.WriteEndTag("td"); output.Indent--; output.WriteEndTag("tr"); output.WriteLine(); output.Write("\t<tr>\n"); output.Write("\t\t<td colspan=\"2\" class=\"{0}\">\n", imageCssClass); bool previewUpdate = false; if (enablePreviews) { string mappedThumbDirectory = HttpContext.Current.Server.MapPath(previewDirectory); string thumbName = UniqueID + "_" + file.FullName.GetHashCode().ToString() + file.Extension; string previewUrl = Page.ResolveUrl(previewDirectory + "/" + thumbName); output.Write("\t\t\t<img src=\"{0}\" border=\"0\">\n", previewUrl); previewUpdate = makeThumb(file, mappedThumbDirectory + "/" + thumbName); exif = new ExifProperties(mappedThumbDirectory + "/" + thumbName); } else { // output.Write("\t\t\t<img src=\"{0}\" border=\"0\" width=\"{1}\" height=\"{2}\">\n", imageUrl, scaleSize.Width, scaleSize.Height); output.Write("\t\t\t<img src=\"{0}\" border=\"0\">\n", imageUrl); exif = new ExifProperties(mappedImageUrl); } output.Write("\t\t</td>\n"); output.Write("\t</tr>\n"); // Display File Name output.Write("\t<tr>\n"); output.Write("\t\t<td colspan=\"2\" class=\"{0}\">{1}</td>\n", fileNameCssClass, file.Name); output.Write("\t</tr>\n"); // Display File Information renderExifDetail(output, "File Size", string.Empty, (file.Length / 1024).ToString(), "KB"); renderExifDetail(output, "File Updated", string.Empty, file.LastWriteTime.ToString(), string.Empty); renderExifDetail(output, "Dimensions", exif.Width.ToString(), " x ", exif.Height.ToString()); if (this.EnablePreviews) { if (previewUpdate) { renderExifDetail(output, "Preview", string.Empty, "Updated", string.Empty); } else { renderExifDetail(output, "Preview", string.Empty, "Cached", string.Empty); } } // Display EXIF Information if (enableExifDetails) { output.WriteFullBeginTag("tr"); output.WriteBeginTag("td"); output.WriteAttribute("colspan", "2"); output.WriteAttribute("class", exifHeaderCssClass); output.WriteLine(HtmlTextWriter.TagRightChar); output.Write("EXIF Details"); output.WriteEndTag("td"); output.WriteEndTag("tr"); output.WriteLine(); renderExifDetail(output, "Description", string.Empty, exif.Description, string.Empty, 3); renderExifDetail(output, "Artist", string.Empty, exif.Artist, string.Empty, 1); renderExifDetail(output, "Copyright", string.Empty, exif.Copyright, string.Empty, 1); renderExifDetail(output, "Software", string.Empty, exif.Software, string.Empty); renderExifDetail(output, "Date Picture Taken", string.Empty, exif.DateTimeTaken.ToString(), string.Empty); renderExifDetail(output, "Focal Length", string.Empty, exif.FocalLength.ToString(), " mm."); renderExifDetail(output, "Exposure Time", string.Empty, exif.ExposureTime.ToString(), string.Empty); renderExifDetail(output, "F-Number", "F/", exif.FNumber.ToString(), string.Empty); renderExifDetail(output, "ISO Speed Rating", "ISO-", exif.ISOSpeedRating.ToString(), string.Empty); renderExifDetail(output, "Manufacturer", string.Empty, exif.Make, string.Empty); renderExifDetail(output, "Model", string.Empty, exif.Model, string.Empty); } if (Page.User.IsInRole("Administrator")) { output.WriteFullBeginTag("tr"); output.WriteFullBeginTag("td"); output.Write(" "); output.WriteEndTag("td"); output.WriteFullBeginTag("td"); output.Write("<input type=\"submit\" name=\"" + this.ClientID + "_update\" value=\"Update\">"); output.WriteEndTag("td"); output.WriteEndTag("tr"); output.WriteLine(); } output.Write("</table>"); output.WriteLine(); } else { output.Write(mappedImageUrl); output.Write("<br />"); output.Write("File not found!"); } }