tags:

views:

1916

answers:

3

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.

+5  A: 

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

LBushkin
A: 

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.

arbiter
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
+2  A: 

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

Pawel Marciniak