public void WriteImageRegionMetadata() { // Copy Test file JpgPhoto testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp4); File.Copy(this.jpgPhotoOne.FileFullName, testPhoto.FileFullName, true); string testValueSuffix = DateTime.Now.ToUniversalTime().ToString(); testPhoto.Metadata.MicrosoftRegionInfo.Regions[0].PersonDisplayName = "PersonDisplayName" + testValueSuffix; testPhoto.Metadata.MicrosoftRegionInfo.Regions[0].PersonEmailDigest = "PersonEmailDigest" + testValueSuffix; testPhoto.Metadata.MicrosoftRegionInfo.Regions[0].PersonLiveIdCID = "PersonLiveIdCID" + testValueSuffix; testPhoto.Metadata.MicrosoftRegionInfo.Regions[0].RectangleString = "0.1, 0.2, 0.3, 0.4"; // And save testPhoto.WriteMetadata(); // Now read it from scratch testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp4); StringAssert.Matches(testPhoto.Metadata.MicrosoftRegionInfo.Regions[0].PersonDisplayName, new Regex("PersonDisplayName" + testValueSuffix)); StringAssert.Matches(testPhoto.Metadata.MicrosoftRegionInfo.Regions[0].PersonEmailDigest, new Regex("PersonEmailDigest" + testValueSuffix)); StringAssert.Matches(testPhoto.Metadata.MicrosoftRegionInfo.Regions[0].PersonLiveIdCID, new Regex("PersonLiveIdCID" + testValueSuffix)); StringAssert.Matches(testPhoto.Metadata.MicrosoftRegionInfo.Regions[0].RectangleString, new Regex("0.1, 0.2, 0.3, 0.4")); File.Delete(testPhoto.FileFullName); }
public void WriteGpsMetadata() { // Copy Test file JpgPhoto testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp9); File.Copy(this.jpgPhotoOne.FileFullName, testPhoto.FileFullName, true); // Test data, includes Unicode strings GpsPosition positionCreated = new GpsPosition(101.23, -34.321, -99.8); GpsPosition positionShow = new GpsPosition(-123.0, 179); // Scrub existing data testPhoto.Metadata.GpsPositionOfLocationCreated = new GpsPosition(); testPhoto.Metadata.GpsPositionOfLocationShown = new GpsPosition(); testPhoto.WriteMetadata(); // Check for empty data testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp9); Assert.AreEqual <GpsPosition>(testPhoto.Metadata.GpsPositionOfLocationCreated, new GpsPosition(), "Blank GpsPosition Created"); Assert.AreEqual <GpsPosition>(testPhoto.Metadata.GpsPositionOfLocationShown, new GpsPosition(), "Blank GpsPosition Shown"); // Write GpsPosition Created testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp9); testPhoto.Metadata.GpsPositionOfLocationCreated = positionCreated; testPhoto.Metadata.GpsPositionOfLocationShown = new GpsPosition(); testPhoto.WriteMetadata(); // And Check Created testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp9); Assert.AreEqual <GpsPosition>(testPhoto.Metadata.GpsPositionOfLocationCreated, positionCreated, "WriteCreated Created"); Assert.AreEqual <GpsPosition>(testPhoto.Metadata.GpsPositionOfLocationShown, new GpsPosition(), "WriteCreated Shown"); // Write GpsPosition Shown testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp9); testPhoto.Metadata.GpsPositionOfLocationCreated = new GpsPosition(); testPhoto.Metadata.GpsPositionOfLocationShown = positionShow; testPhoto.WriteMetadata(); // And Check Shown testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp9); Assert.AreEqual <GpsPosition>(testPhoto.Metadata.GpsPositionOfLocationCreated, new GpsPosition(), "WriteShown Created"); Assert.AreEqual <GpsPosition>(testPhoto.Metadata.GpsPositionOfLocationShown, positionShow, "WriteShown Shown"); // Tidy up File.Delete(testPhoto.FileFullName); }
public void WriteAddressMetadata() { // Copy Test file JpgPhoto testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp10); File.Copy(this.jpgPhotoOne.FileFullName, testPhoto.FileFullName, true); // Test data, includes Unicode strings Address addressCreated = new Address(@"CòuntryCreàted/RegÎon/Ĉity/Stréét"); Address addressShown = new Address(@"CòuntryShówn/RegÎon/Ĉity/Stréét"); // Scrub existing data testPhoto.Metadata.AddressOfLocationCreated = new Address(); testPhoto.Metadata.AddressOfLocationShown = new Address(); testPhoto.WriteMetadata(); // Check for empty data Assert.AreEqual <Address>(testPhoto.Metadata.AddressOfLocationCreated, new Address(), "Blank Address Created"); Assert.AreEqual <Address>(testPhoto.Metadata.AddressOfLocationShown, new Address(), "Blank Address Shown"); // Write Address Created testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp10); testPhoto.Metadata.AddressOfLocationCreated = addressCreated; testPhoto.Metadata.AddressOfLocationShown = new Address(); testPhoto.WriteMetadata(); // And Check Created testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp10); Assert.AreEqual <Address>(testPhoto.Metadata.AddressOfLocationCreated, addressCreated, "WriteAddress Created"); Assert.AreEqual <Address>(testPhoto.Metadata.AddressOfLocationShown, new Address(), "WriteAddress Shown"); // Write Address Shown testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp10); testPhoto.Metadata.AddressOfLocationCreated = new Address(); testPhoto.Metadata.AddressOfLocationShown = addressShown; testPhoto.WriteMetadata(); // And Check Shown testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp10); Assert.AreEqual <Address>(testPhoto.Metadata.AddressOfLocationCreated, new Address(), "WriteShown Created"); Assert.AreEqual <Address>(testPhoto.Metadata.AddressOfLocationShown, addressShown, "WriteShown Shown"); // Tidy up File.Delete(testPhoto.FileFullName); }
public static void AddTag(string inputFile) { File.Copy(inputFile, "JpgPhotoExamples.AddTag.jpg", true); JpgPhoto jpgPhoto = new JpgPhoto("JpgPhotoExamples.AddTag.jpg"); jpgPhoto.ReadMetadata(); jpgPhoto.Metadata.Tags.Add("Test Tag: " + DateTime.Now.ToString()); jpgPhoto.WriteMetadata(); }
public static void WriteMetadata(string inputFile) { File.Copy(inputFile, "JpgPhotoExamples.WriteMetadata.jpg", true); JpgPhoto jpgPhoto = new JpgPhoto("JpgPhotoExamples.WriteMetadata.jpg"); jpgPhoto.ReadMetadata(); jpgPhoto.Metadata.Comment = "Test Comment"; jpgPhoto.WriteMetadata(); }
public static void AddGpsCoor(string inputFile) { File.Copy(inputFile, "JpgPhotoExamples.AddGpsCoor.jpg", true); JpgPhoto jpgPhoto = new JpgPhoto("JpgPhotoExamples.AddGpsCoor.jpg"); jpgPhoto.ReadMetadata(); jpgPhoto.Metadata.GpsPositionOfLocationCreated.Latitude.Numeric = 1.05; jpgPhoto.Metadata.GpsPositionOfLocationCreated.Longitude.Numeric = -0.95; jpgPhoto.Metadata.GpsPositionOfLocationCreated.Dimension = GpsPosition.Dimensions.TwoDimensional; jpgPhoto.Metadata.GpsPositionOfLocationCreated.Source = "GPS Test"; jpgPhoto.WriteMetadata(); }
public void WriteMetadataAndCheckForMetadataLoss() { JpgPhoto beforePhoto = TestPhotos.Load(TestPhotos.UnitTest3); JpgPhoto afterPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp5); // Copy Test file File.Copy(beforePhoto.FileFullName, afterPhoto.FileFullName, true); // Change date and save afterPhoto.Metadata.FotoflyDateLastSave = DateTime.Now.AddTicks(-DateTime.Now.TimeOfDay.Ticks); afterPhoto.WriteMetadata(); MetadataDump beforeDump; MetadataDump afterDump; using (WpfFileManager wpfFileManager = new WpfFileManager(beforePhoto.FileFullName)) { beforeDump = new MetadataDump(wpfFileManager.BitmapMetadata); beforeDump.GenerateStringList(); } using (WpfFileManager wpfFileManager = new WpfFileManager(afterPhoto.FileFullName)) { afterDump = new MetadataDump(wpfFileManager.BitmapMetadata); afterDump.GenerateStringList(); } for (int i = 0; i < beforeDump.StringList.Count; i++) { // Ignore schema changes, edit dates and created software if (beforeDump.StringList[i] != afterDump.StringList[i] && !beforeDump.StringList[i].Contains("DateLastSave") && !beforeDump.StringList[i].Contains("LastEditDate") && !beforeDump.StringList[i].Contains("ushort=513") && !beforeDump.StringList[i].Contains("OffsetSchema")) { Assert.Fail("Metadata mismatch " + beforeDump.StringList[i] + " != " + afterDump.StringList[i]); } } if (new FileInfo(afterPhoto.FileFullName).Length > new FileInfo(beforePhoto.FileFullName).Length) { Assert.Fail("Photo has decreased in size after saving"); } // Clean up File.Delete(afterPhoto.FileFullName); }
public static void SetPhotoTags(string inputFile, List <string> tags) { JpgPhoto jpgPhoto = new JpgPhoto(inputFile); // TODO: BUGBUG: Changing description is throwing exception. In the end it is trying to set null value //jpgPhoto.Metadata.Description = @"Tags updated on " + DateTime.Now.ToString(); Console.WriteLine($"oldTags: {jpgPhoto.Metadata.Tags}"); // not sure whether or not to preserve the old tags. // Assuming we are not settings the same tags repeatedly, retaining original tags. //jpgPhoto.Metadata.Tags.Clear(); jpgPhoto.Metadata.Tags.AddRange(tags); jpgPhoto.WriteMetadata(); }
public static void AddRegion(string inputFile) { File.Copy(inputFile, "JpgPhotoExamples.AddRegion.jpg", true); // Open file and read metadata JpgPhoto jpgPhoto = new JpgPhoto("JpgPhotoExamples.AddRegion.jpg"); jpgPhoto.ReadMetadata(); // Create new Region MicrosoftImageRegion newRegion = new MicrosoftImageRegion(); newRegion.PersonDisplayName = "Ben Vincent"; newRegion.RectangleString = "0.1, 0.1, 0.1, 0.1"; // Add the new region to the photo jpgPhoto.Metadata.MicrosoftRegionInfo.Regions.Add(newRegion); // Save jpgPhoto.WriteMetadata(); }
public void WriteMetadataAndCheckForImageLoss() { // File that will be changed JpgPhoto beforePhoto = new JpgPhoto(TestPhotos.UnitTestTemp6); JpgPhoto afterPhoto = new JpgPhoto(TestPhotos.UnitTestTemp7); // Get a copy of a file File.Copy(this.jpgPhotoOne.FileFullName, beforePhoto.FileFullName, true); File.Copy(this.jpgPhotoOne.FileFullName, afterPhoto.FileFullName, true); // Change some metadata afterPhoto.Metadata.Description = "Test Description" + DateTime.Now.ToString(); afterPhoto.Metadata.Comment = "Test Comment" + DateTime.Now.ToString(); afterPhoto.Metadata.Copyright = "Test Copyright" + DateTime.Now.ToString(); afterPhoto.Metadata.Subject = "Subject Copyright" + DateTime.Now.ToString(); // Save as temp file afterPhoto.WriteMetadata(); // Open Original File using (Stream beforeStream = File.Open(beforePhoto.FileFullName, FileMode.Open, FileAccess.Read)) { // Open the Saved File using (Stream afterStream = File.Open(afterPhoto.FileFullName, FileMode.Open, FileAccess.Read)) { // Compare every pixel to ensure it has changed BitmapSource beforeBitmap = BitmapDecoder.Create(beforeStream, BitmapCreateOptions.None, BitmapCacheOption.OnDemand).Frames[0]; BitmapSource afterBitmap = BitmapDecoder.Create(afterStream, BitmapCreateOptions.None, BitmapCacheOption.OnDemand).Frames[0]; PixelFormat pf = PixelFormats.Bgra32; FormatConvertedBitmap fcbOne = new FormatConvertedBitmap(beforeBitmap, pf, null, 0); FormatConvertedBitmap fcbTwo = new FormatConvertedBitmap(afterBitmap, pf, null, 0); GC.AddMemoryPressure(((fcbOne.Format.BitsPerPixel * (fcbOne.PixelWidth + 7)) / 8) * fcbOne.PixelHeight); GC.AddMemoryPressure(((fcbTwo.Format.BitsPerPixel * (fcbTwo.PixelWidth + 7)) / 8) * fcbTwo.PixelHeight); int width = fcbOne.PixelWidth; int height = fcbOne.PixelHeight; int bpp = pf.BitsPerPixel; int stride = (bpp * (width + 7)) / 8; byte[] scanline0 = new byte[stride]; byte[] scanline1 = new byte[stride]; Int32Rect lineRect = new Int32Rect(0, 0, width, 1); // Loop through each row for (int y = 0; y < height; y++) { lineRect.Y = y; fcbOne.CopyPixels(lineRect, scanline0, stride, 0); fcbTwo.CopyPixels(lineRect, scanline1, stride, 0); // Loop through each column for (int b = 0; b < stride; b++) { if (Math.Abs(scanline0[b] - scanline1[b]) > 0) { Assert.Fail("Saved file was not solved losslessly"); } } } } } // Tidy UP File.Delete(beforePhoto.FileFullName); File.Delete(afterPhoto.FileFullName); }
public void WriteMetadataToFile() { // Copy Test file JpgPhoto testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp3); File.Copy(this.jpgPhotoTwo.FileFullName, testPhoto.FileFullName, true); // Test value to write string testString = "Test " + DateTime.Now.ToString(); DateTime testDate = DateTime.Now.AddTicks(-DateTime.Now.TimeOfDay.Ticks); string testTag = "Test Tag Î"; // Write text testPhoto.Metadata.Description = testString; testPhoto.Metadata.Comment = testString; testPhoto.Metadata.Copyright = testString; testPhoto.Metadata.AddressOfLocationCreatedSource = testString; testPhoto.Metadata.Tags = new TagList(); testPhoto.Metadata.Tags.Add(new Tag(testTag)); // Write dates testPhoto.Metadata.DateAquired = testDate; testPhoto.Metadata.DateDigitised = testDate; testPhoto.Metadata.DateTaken = testDate; testPhoto.Metadata.DateUtc = testDate; testPhoto.Metadata.AddressOfLocationCreatedLookupDate = testDate; // Save Photo Three testPhoto.WriteMetadata(); // Check the file was created if (!File.Exists(testPhoto.FileFullName)) { Assert.Fail("File save failed"); } // Read metadata testPhoto = TestPhotos.Load(TestPhotos.UnitTestTemp3); testPhoto.ReadMetadata(); // Check the file was created if (testPhoto.Metadata == null) { Assert.Fail("Unable to read saved files metadata"); } // Check the text was set correctly StringAssert.Matches(testPhoto.Metadata.Description, new Regex(testString), "Description"); StringAssert.Matches(testPhoto.Metadata.Comment, new Regex(testString), "Comment"); StringAssert.Matches(testPhoto.Metadata.Copyright, new Regex(testString), "Copyright"); StringAssert.Matches(testPhoto.Metadata.AddressOfLocationCreatedSource, new Regex(testString), "AddressOfLocationCreatedSource"); // Check date was written Assert.AreEqual <DateTime>(testPhoto.Metadata.DateAquired, testDate, "DateAquired"); Assert.AreEqual <DateTime>(testPhoto.Metadata.DateDigitised, testDate, "DateDigitised"); Assert.AreEqual <DateTime>(testPhoto.Metadata.DateTaken, testDate, "DateTaken"); Assert.AreEqual <DateTime>(testPhoto.Metadata.DateUtc, testDate, "UtcDate"); Assert.AreEqual <DateTime>(testPhoto.Metadata.AddressOfLocationCreatedLookupDate, testDate, "AddressOfLocationCreatedLookupDate"); Assert.AreEqual <Tag>(testPhoto.Metadata.Tags.Last(), new Tag(testTag), "Tags"); if (new FileInfo(this.jpgPhotoTwo.FileFullName).Length > new FileInfo(testPhoto.FileFullName).Length) { Assert.Fail("Photo has decreased in size after saving"); } File.Delete(testPhoto.FileFullName); }