.NET C # library for lossless rewrite of Exif?

I found various codes and libraries for editing Exif .

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

I am looking for a library (or even a way to do it myself) to edit only part of Exif in a JPEG file (or add Exif data if it does not already exist), leaving the other data unchanged. Is it impossible?

So far, I could only find the Exif part (starting with 0xFFE1), but I don't understand how to read the data.

+10
c # jpeg exif
Jun 24. '09 at 13:01
source share
4 answers

Here are the specifications for the Exif interchange format if you plan to encode your own tag editing library.

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

Here's a Perl library that fits your needs with which you can learn:

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

Here's a decent .NET library for evaluating Exif from Draft Code :

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

+8
Jun 24 '09 at 13:30
source share
β€” -

You can do this without an external library:

// 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); 

A list of all possible PropertyItem identifiers (including exif) can be found here .

Update: Agreed, this method will re-encode the image when saving. But I remembered another method, in WinXP SP2, and then new image components - WIC - were added, and you can use them for lossless metadata - How-to: Transcode a JPEG image with metadata .

+8
Jun 24 '09 at 19:12
source share

The exiv2net library (the .NET wrapper on top of exiv2) may be what you are looking for.

+4
Nov 30 '09 at 22:03
source share

I wrote a small test where I compress one file several times to see quality deterioration, and you can see it in the third or fourth compression, which is very bad.

But, fortunately, if you always use the same QualityLevel with JpegBitmapEncoder, then there is no degradation.

In this example, I rewrite 100x keywords in metadata, and the quality does not seem to change.

 private void LosslessJpegTest() { var original = "d:\\!test\\TestInTest\\20150205_123011.jpg"; var copy = original; const BitmapCreateOptions createOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile; for (int i = 0; i < 100; i++) { using (Stream originalFileStream = File.Open(copy, FileMode.Open, FileAccess.Read)) { BitmapDecoder decoder = BitmapDecoder.Create(originalFileStream, createOptions, BitmapCacheOption.None); if (decoder.CodecInfo == null || !decoder.CodecInfo.FileExtensions.Contains("jpg") || decoder.Frames[0] == null) continue; BitmapMetadata metadata = decoder.Frames[0].Metadata == null ? new BitmapMetadata("jpg") : decoder.Frames[0].Metadata.Clone() as BitmapMetadata; if (metadata == null) continue; var keywords = metadata.Keywords == null ? new List<string>() : new List<string>(metadata.Keywords); keywords.Add($"Keyword {i:000}"); metadata.Keywords = new ReadOnlyCollection<string>(keywords); JpegBitmapEncoder encoder = new JpegBitmapEncoder {QualityLevel = 80}; encoder.Frames.Add(BitmapFrame.Create(decoder.Frames[0], decoder.Frames[0].Thumbnail, metadata, decoder.Frames[0].ColorContexts)); copy = original.Replace(".", $"_{i:000}."); using (Stream newFileStream = File.Open(copy, FileMode.Create, FileAccess.ReadWrite)) { encoder.Save(newFileStream); } } } } 
0
Feb 11 '16 at 21:37
source share



All Articles