6

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

TigrouMeow
  • 3,669
  • 3
  • 27
  • 31
  • I added a project on CodePlex, featuring the resizing without losing the EXIF data: http://tidytinypics.codeplex.com/ – TigrouMeow Dec 11 '09 at 01:10

2 Answers2

3

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.

Community
  • 1
  • 1
Charlie Salts
  • 13,109
  • 7
  • 49
  • 78
  • Okay I kept my solution then, it's working quite well, it's just a lot of code for nothing but it's fast enough :) – TigrouMeow Aug 05 '09 at 14:52
  • The only other alternative I know of is ImageMagick - I understand you can do transformations without losing EXIF data but that library is essentially doing the same thing you're already doing, just likely faster. – Charlie Salts Aug 06 '09 at 01:52
2

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;
    }