Exif tags on universal Windows Platform C#

2020-04-21 08:13发布

问题:

How do I add Tags to a jpeg picture on UWP (universal Windows Platform) / Windows Phone 10. WindowsBase and PresentationCore not available.

回答1:

Here is an example about how to write the Artist exif tag to a jpeg image in Windows Runtime.

You can find all of the EXIF tag ids in this web page:

http://www.exiv2.org/tags.html

        var src = await KnownFolders
                        .PicturesLibrary
                        .GetFileAsync("210644575939381015.jpg");

        using (var stream = await src.OpenAsync(FileAccessMode.ReadWrite))
        {
            var decoder = await BitmapDecoder.CreateAsync(stream);

            var encoder = await BitmapEncoder.CreateForTranscodingAsync(stream, decoder);

            // var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);

            var list = new List<KeyValuePair<string, BitmapTypedValue>>();

            var artist = new BitmapTypedValue("Hello World", Windows.Foundation.PropertyType.String);

            list.Add(new KeyValuePair<string, BitmapTypedValue>("/app1/ifd/exif/{ushort=315}", artist));

            await encoder.BitmapProperties.SetPropertiesAsync(list);

            await encoder.FlushAsync();

            await stream.FlushAsync();
        }