Class that can be used to access an Exif profile.
Inheritance: ImageProfile
    public void Test_Constructor()
    {
      using (MemoryStream memStream = new MemoryStream())
      {
        using (MagickImage image = new MagickImage(Files.ImageMagickJPG))
        {
          ExifProfile profile = image.GetExifProfile();
          Assert.IsNull(profile);

          profile = new ExifProfile();
          profile.SetValue(ExifTag.Copyright, "Dirk Lemstra");

          image.AddProfile(profile);

          profile = image.GetExifProfile();
          Assert.IsNotNull(profile);

          image.Write(memStream);
        }

        memStream.Position = 0;
        using (MagickImage image = new MagickImage(memStream))
        {
          ExifProfile profile = image.GetExifProfile();

          Assert.IsNotNull(profile);
          Assert.AreEqual(1, profile.Values.Count());

          ExifValue value = profile.Values.FirstOrDefault(val => val.Tag == ExifTag.Copyright);
          TestValue(value, "Dirk Lemstra");
        }
      }
    }
Exemple #2
0
        public bool Save(string rootpath)
        {
            bool success = false;

            ImageMagick.ExifProfile exifprofile = new ImageMagick.ExifProfile();
            exifprofile.SetValue(ExifTag.Copyright, " ©" + Copyright);
            exifprofile.SetValue(ExifTag.Artist, Creator);
            exifprofile.SetValue(ExifTag.ImageDescription, Subject);
            exifprofile.SetValue(ExifTag.Software, Software);
            _image.AddProfile(exifprofile);

            ImageMagick.IptcProfile iptcprofile = new ImageMagick.IptcProfile();
            iptcprofile.SetValue(IptcTag.CopyrightNotice, "No Unauthorized reproduction ©" + Copyright);
            iptcprofile.SetValue(IptcTag.Byline, Creator);
            iptcprofile.SetValue(IptcTag.Country, Country);
            iptcprofile.SetValue(IptcTag.Headline, Headline);
            iptcprofile.SetValue(IptcTag.Keyword, Keywords);
            iptcprofile.SetValue(IptcTag.Source, Source);
            iptcprofile.SetValue(IptcTag.Caption, Subject);
            iptcprofile.SetValue(IptcTag.Title, Title);
            _image.AddProfile(iptcprofile);

            _image.Write(rootpath);
            success = true;

            return(success);
        }
    private static void TestProfile(ExifProfile profile)
    {
      Assert.IsNotNull(profile);

      Assert.AreEqual(44, profile.Values.Count());

      foreach (ExifValue value in profile.Values)
      {
        Assert.IsNotNull(value.Value);

        if (value.Tag == ExifTag.Software)
          Assert.AreEqual("Adobe Photoshop 7.0", value.ToString());

        if (value.Tag == ExifTag.XResolution)
          Assert.AreEqual(300.0, value.Value);

        if (value.Tag == ExifTag.GPSLatitude)
        {
          double[] pos = (double[])value.Value;
          Assert.AreEqual(54, pos[0]);
          Assert.AreEqual(59.38, pos[1]);
          Assert.AreEqual(0, pos[2]);
        }

        if (value.Tag == ExifTag.ShutterSpeedValue)
          Assert.AreEqual(9.5, value.Value);
      }
    }
		//public PhotoInfo ReadExif2(string name)
		//{
		//	PhotoInfo info = new PhotoInfo() { FileName = name };

		//	using (MagickImage image = new MagickImage(name))
		//	{
		//		ExifProfile profile = image.GetExifProfile();

		//		ExifValue value = profile.GetValue(ExifTag.DateTimeDigitized);

		//		if (value.Value == null)
		//			throw new Exception("Неудалось считать дату и время файла");

		//		info.DateTimeDigitized = (DateTime)value.Value;

		//		value = profile.GetValue(ExifTag.Model);

		//		info.Model = (string)value.Value;

		//		if (info.Model == null)
		//			info.Model = "Неизвестная модель";

		//	}

		//	return info;
		//}

		public static void WriteExif(PhotoInfo info, int? quality)
        {
			// Для моего телефона не работает метод image.AddProfile. Т.к. для моего телефона дату менять ненадо, то пропускаем.
			if (info.Model.ToUpper().Contains("HUAWEI P7-L10"))
				return;

			using (MagickImage image = new MagickImage(info.FileName))
            {
				if (quality != null && quality < image.Quality)
					image.Quality = quality.Value;

                ExifProfile profile = image.GetExifProfile();

				if (profile == null)
					profile = new ExifProfile();

                profile.SetValue(ExifTag.DateTimeDigitized, info.DateTimeDigitized.ToString(DATE_TIME_FORMAT));
				profile.SetValue(ExifTag.DateTime, info.DateTimeDigitized.ToString(DATE_TIME_FORMAT));
				profile.SetValue(ExifTag.DateTimeOriginal, info.DateTimeDigitized.ToString(DATE_TIME_FORMAT));

				//try
				//{
					image.AddProfile(profile, true);

					image.Write(info.FileName);
				//}
				//catch (Exception exc)
				//{ 
				
				//}
            }
        }
    public void Test_InvalidTag()
    {
      var exifProfile = new ExifProfile();

      ExceptionAssert.Throws<NotSupportedException>(() =>
      {
        exifProfile.SetValue((ExifTag)42, 42);
      });
    }
    public void Test_UnknownExifTag()
    {
      var exifProfile = new ExifProfile();
      exifProfile.SetValue(ExifTag.ImageWidth, 42);

      var bytes = exifProfile.ToByteArray();
      bytes[16] = 42;

      exifProfile = new ExifProfile(bytes);

      ExifTag unkownTag = (ExifTag)298;
      ExifValue value = exifProfile.GetValue(unkownTag);
      Assert.AreEqual(42, value.Value);
      Assert.AreEqual("42", value.ToString());

      bytes = exifProfile.ToByteArray();
      Assert.AreEqual(0, bytes.Length);
    }
    public void Test_ExifTag()
    {
      var exifProfile = new ExifProfile();

      exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)1);
      ExifValue value = exifProfile.GetValue(ExifTag.ResolutionUnit);
      Assert.AreEqual("None", value.ToString());

      exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)2);
      value = exifProfile.GetValue(ExifTag.ResolutionUnit);
      Assert.AreEqual("Inches", value.ToString());

      exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)3);
      value = exifProfile.GetValue(ExifTag.ResolutionUnit);
      Assert.AreEqual("Centimeter", value.ToString());

      exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)4);
      value = exifProfile.GetValue(ExifTag.ResolutionUnit);
      Assert.AreEqual("4", value.ToString());

      exifProfile.SetValue(ExifTag.ImageWidth, 123);
      value = exifProfile.GetValue(ExifTag.ImageWidth);
      Assert.AreEqual("123", value.ToString());
    }
 public void Test_Constructor_Empty()
 {
   using (MagickImage image = new MagickImage(Files.ImageMagickJPG))
   {
     using (MemoryStream memStream = new MemoryStream())
     {
       ExifProfile profile = new ExifProfile(memStream);
       image.AddProfile(profile);
     }
   }
 }