SixLabors / ImageSharp

:camera: A modern, cross-platform, 2D Graphics library for .NET
https://sixlabors.com/products/imagesharp/
Other
7.31k stars 846 forks source link

ImageSharp is not supporting negative values on writing EXIF MetaData #2653

Closed SaranyaRamasamy96 closed 6 months ago

SaranyaRamasamy96 commented 6 months ago

Prerequisites

ImageSharp version

2.1.6

Other ImageSharp packages and versions

3.1.2

Environment (Operating system, version and so on)

Windows

.NET Framework version

.NET Standard 2.1

Description

I am trying to write EXIF data of an image and noticed that negative values are not supported in ImageSharp. I have used version 2.1.6 to support my project, which is .NET Standard 2.1. Please find the code below:

Steps to Reproduce

public static async Task UpdateMetadataAsync(Image image, string pathWithExtension)
        {
            string extension = Path.GetExtension(pathWithExtension).Replace(".", "");
            if (AppConstants.ImageFormats.Contains(extension))
            {
               image.Latitude = "38.94312104038846";
               image.Longitude = "-77.41306837162972";
                var image = await Image.LoadAsync(pathWithExtension);
                if (image.Metadata.ExifProfile == null)
                    image.Metadata.ExifProfile = new ExifProfile();
                if (image!= null && !string.IsNullOrEmpty(image.Image_Capture_Date.ToString()))
                {
                    var imageCaptureDate = (DateTime)image.Image_Capture_Date;
                    var imageCaptureDateWithFormat = imageCaptureDate.ToString(AppConstants.DateFormat_YYYY_MM_DD);
                    image.Metadata.ExifProfile = new ExifProfile();
                    image.Metadata.ExifProfile.SetValue(ExifTag.DateTimeOriginal, imageCaptureDateWithFormat);
                    image.Metadata.ExifProfile.SetValue(ExifTag.XPAuthor, image.Photographer);
                    image.Metadata.ExifProfile.SetValue(ExifTag.GPSLatitude, ConvertDecimalToRational(Convert.ToDouble(image.Latitude)));
                    image.Metadata.ExifProfile.SetValue(ExifTag.GPSLongitude, ConvertDecimalToRational(Convert.ToDouble(image.Longitude)));
                    image.Save(pathWithExtension);
                }
            }
        }
       private static Rational[] ConvertDecimalToRational(double decimalValue)
        {
            int degrees = (int)Math.Floor(decimalValue);
            double remainingMinutes = (decimalValue - degrees) * 60;
            int minutes = (int)Math.Floor(remainingMinutes);
            double seconds = (remainingMinutes - minutes) * 60;
            Rational[] value = new Rational[] { new Rational(degrees), new Rational(minutes), new Rational(seconds) };
            return value;
        }

Images

No response

dlemstra commented 6 months ago

Exif doesn't support negative values for GPSLatitude and GPSLongitude. You will need to use GPSLatitudeRef and GPSLongitudeRef to specify the "direction". You can find more information on which value to us here: https://exiftool.org/TagNames/GPS.html.