Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize an image with .NET without losing EXIF data

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).

like image 869
TigrouMeow Avatar asked Aug 05 '09 01:08

TigrouMeow


2 Answers

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.

like image 65
Charlie Salts Avatar answered Oct 16 '22 13:10

Charlie Salts


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;
    }
like image 40
wtfidgafmfgtfooh Avatar answered Oct 16 '22 15:10

wtfidgafmfgtfooh