How to copy metadata from a photo to a rotated version of the photo in UWP?

WillAutioItrax 201 Reputation points
2022-06-01T16:13:34.923+00:00

I can take a picture in Xamarin Forms UWP and rotate it (if necessary) and change it's size.
That all works fine. Not all the meta data gets copied over to the new photo.

I can get the meta data from the old photo like this:
ImageProperties propsIn = await filein.Properties.GetImagePropertiesAsync();
DateTimeOffset dateTaken = propsIn.DateTaken;

        var requests = new System.Collections.Generic.List<string>();
        requests.Add("System.ApplicationName");
        IDictionary<string, object> retrievedProps = await propsIn.RetrievePropertiesAsync(requests);

        string programName;
        if (retrievedProps.ContainsKey("System.ApplicationName"))
        {
            programName = (string)retrievedProps["System.ApplicationName"];
        }

But I can't find how to copy dateTaken and programName to the new photo.

Thanks for your interest in this.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,291 Reputation points Microsoft Vendor
    2022-06-02T03:16:06.497+00:00

    Hello,

    You could try the following code to copy the metadata to a new photo.

       if (retrievedProps.ContainsKey("System.ApplicationName"))  
       {  
           Bitmap theImage = new Bitmap(path);  
           var programName = (string)retrievedProps["System.ApplicationName"];  
           Encoding _Encoding = Encoding.UTF8;  
           PropertyItem[] propItems = theImage.PropertyItems;  
           var DataTakenProperty = propItems.Where(a => a.Id.ToString("x") == "9004").FirstOrDefault();  
           var programNamenProperty = propItems.Where(a => a.Id.ToString("x") == "9003").FirstOrDefault();  
           DataTakenProperty.Value = _Encoding.GetBytes(dateTaken.ToString());  
           programNamenProperty.Value = _Encoding.GetBytes(programName);  
           theImage.SetPropertyItem(programNamenProperty);  
           theImage.SetPropertyItem(DataTakenProperty);  
           theImage.Save(path);  
           theImage.Dispose();  
       }  
    

    In addition, you could also use the Image class to get/set the EXIF information for your file.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.