Created
March 10, 2025 19:46
-
-
Save smourier/7cf100ddb2c7ad9322a3b15b39edd46c to your computer and use it in GitHub Desktop.
WinRT C# code that loads some file, and save it back with EXIF metadata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using Windows.Foundation; | |
using Windows.Graphics.Imaging; | |
using Windows.Storage; | |
public static async Task AddExifToFile() | |
{ | |
// get an input file | |
var inputPath = @"d:\temp\tiger.png"; | |
var input = await StorageFile.GetFileFromPathAsync(inputPath); | |
using var inputStream = await input.OpenAsync(FileAccessMode.Read); | |
var decoder = await BitmapDecoder.CreateAsync(inputStream); | |
var data = await decoder.GetPixelDataAsync(); | |
// save to output file | |
var folder = await StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(inputPath)); | |
var output = await folder.CreateFileAsync("tiger2.png", CreationCollisionOption.ReplaceExisting); | |
using var outputStream = await output.OpenAsync(FileAccessMode.ReadWrite); | |
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outputStream); | |
encoder.SetPixelData(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, decoder.PixelWidth, decoder.PixelHeight, decoder.DpiX, decoder.DpiX, data.DetachPixelData()); | |
// add a metadata | |
var propertySet = new BitmapPropertySet | |
{ | |
{ "System.Title", new BitmapTypedValue("This is a tiger", PropertyType.String) } | |
// or | |
//{ "/xmp/dc:title", new BitmapTypedValue("This is a tiger", PropertyType.String) } | |
}; | |
await encoder.BitmapProperties.SetPropertiesAsync(propertySet); | |
await encoder.FlushAsync(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment