What is the best way to resize images using .NET, without losing the EXIF data? I'm okay with using .NET 2 System.Drawing.* classes, WPF classes, or open-source libraries.
The only easy way I found to handle this all for now is to use the Graphics.FromImage (.NET 2) to perform the resizing and to re-write the EXIF data with an OpenSource library manually (each piece of data one by one).
Your suggestion of extracting the EXIF data before resizing, and then re-inserting the EXIF data seems like a decent solution.
EXIF data can be defined only for formats like JPEG and TIFF - when you load such an image into a Graphics object for resizing, you're essentially converting the image to a regular bitmap. Hence, you lose the EXIF data.
Slightly related thread about EXIF extraction using C# here.
I used Magick .NET and created 2 extension methods:
public static byte[] ToByteArray(this Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
public static Image AttachMetadData(this Image imgModified, Image imgOriginal)
{
using (MagickImage imgMeta = new MagickImage(imgOriginal.ToByteArray()))
using (MagickImage imgModi = new MagickImage(imgModified.ToByteArray()))
{
foreach (var profileName in imgMeta.ProfileNames)
{
imgModi.AddProfile(imgMeta.GetProfile(profileName));
}
imgModified = imgModi.ToImage();
}
return imgModified;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With