.NET C# library for lossless Exif rewriting?

17,706

Solution 1

Here are the specifications for the Exif interchange format, if you plan to code your own library for editing tags.

http://www.exif.org/specifications.html

Here's a library written in Perl that meets your needs that you may be able to learn from:

http://www.sno.phy.queensu.ca/~phil/exiftool/

Here's a decent .NET library for Exif evaluation from The Code Project:

http://www.codeproject.com/KB/graphics/exiftagcol.aspx

Solution 2

You can do this without any external lib:

// Create image.
Image image1 = Image.FromFile("c:\\Photo1.jpg");

// Get a PropertyItem from image1. Because PropertyItem does not
// have public constructor, you first need to get existing PropertyItem
PropertyItem propItem = image1.GetPropertyItem(20624);

// Change the ID of the PropertyItem.
propItem.Id = 20625;

// Set the new PropertyItem for image1.
image1.SetPropertyItem(propItem);

// Save the image.
image1.Save("c:\\Photo1.jpg", ImageFormat.Jpg);

List of all possible PropertyItem ids (including exif) you can found here.

Update: Agreed, this method will re-encode image on save. But I have remembered another method, in WinXP SP2 and later there is new imaging components added - WIC, and you can use them to lossless write metadate - How-to: Re-encode a JPEG Image with Metadata.

Solution 3

exiv2net library (a .NET wrapper on top of exiv2) may be what you're looking for.

Share:
17,706
Emiel Bruijntjes
Author by

Emiel Bruijntjes

Updated on June 03, 2022

Comments

  • Emiel Bruijntjes
    Emiel Bruijntjes almost 2 years

    I have found various code and libraries for editing Exif.

    But they are only lossless when the image width and height is multiple of 16.

    I am looking for a library (or even a way to do it myself) to edit just the Exif portion in a JPEG file (or add Exif data if it doesn't exist yet), leaving the other data unmodified. Isn't that possible?

    So far I could only locate the Exif portion (starts with 0xFFE1) but I don't understand how to read the data.

  • Emiel Bruijntjes
    Emiel Bruijntjes almost 15 years
    This recompresses the image. There is a workaround not to recompress, by rotating the image twice, but only works if width and height are multiples of 16
  • BlackICE
    BlackICE over 13 years
  • Sina
    Sina over 5 years
    The exif.org site has been redone and all the hyperlinks load the same content which is really just some blog posts. The specs link doesn't actually link to specs anymore. However, you can still access it via exif.org/Exif2-2.PDF And if that stops working, use Web Archive to view an older version of the site such as web.archive.org/web/20080422021217/http://www.exif.org/…