1

I've been stumped trying to figure this one out.

I'm trying to retrieve the "Jpeg Comment" out of a jpg file via C#. The code below works but I need the basic comment NOT the Exif comment. I'm using FastStone Image Viewer to set the basic comment. Help me retrieve it.

I can use the commandline program exiv2 to verify that the comment is there. exiv2 -pc c:\test.jpg (it spits out the basic comment). exiv2 -pa c:\test.jpg (it spits out the EXIF comment) I've used several C# libs to get at it but they get the EXIF data.

        Image x = Image.FromFile(@"c:\test.jpg");
        PropertyItem prop;
        prop = x.GetPropertyItem(0x9286);
        string Comment = Encoding.ASCII.GetString(prop.Value); 
user937036
  • 341
  • 1
  • 3
  • 15
  • I don't know if it will work or not but I notice that jpegbitmapdecoder (http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.jpegbitmapdecoder.aspx) has a MetaData property that has a comment property. It is possible that this is the actual jpeg comment rather than the exif comment but I've not checked. I'd guess that your problem comes from the fact that Image is a bit too generic a class and there doesn't seem to be any other comment property then that exif one according to http://msdn.microsoft.com/en-us/library/system.drawing.imaging.propertyitem.id.aspx. :( – Chris Sep 09 '11 at 15:24
  • Thanks for the feedback but that is not it. I ran the code on the page and all the values are null. – user937036 Sep 09 '11 at 15:40
  • Sorry then, I've not much experience of this and the documentation doesn't seem helpful. Only other suggestion I have is to ready the binary data from the file and find the text comment manually. Not for the faint hearted. Or if its an option set an exif comment instead of a standard one... – Chris Sep 09 '11 at 16:40
  • actually, I jsut browsed some related questions and came across http://code.google.com/p/exiv2net/ - again I've not looked closely but if the command line program does what you want it seems plausible that this will too. Its unmaintained which is not necessarily good but worth a look maybe. – Chris Sep 09 '11 at 16:44

2 Answers2

0

You could refer to this link.

(Thanks for those who already have answered the same question, although the answer was quite right but not 100% to solve this problem.)

Here are three steps you need to do:

  1. Be aware that you should have the Jpeg file cloned.
  2. Set the comment of the cloned file.
  3. Replace the file by deleting the original jpeg file.

Here is the sample code:

    public void addImageComment(string imageFlePath, string comments)
    {
        BitmapDecoder decoder = null;
        BitmapFrame bitmapFrame = null;
        BitmapMetadata metadata = null;
        FileInfo originalImage = new FileInfo(imageFlePath);

        if (File.Exists(imageFlePath))
        {
            // load the jpg file with a JpegBitmapDecoder    
            using (Stream jpegStreamIn = File.Open(imageFlePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
            {
                decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
            }

            bitmapFrame = decoder.Frames[0];
            metadata = (BitmapMetadata)bitmapFrame.Metadata;

            if (bitmapFrame != null)
            {
                BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();

                if (metaData != null)
                {
                    // modify the metadata   
                    metaData.Comment = comments;

                    // get an encoder to create a new jpg file with the new metadata.      
                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts));
                    //string jpegNewFileName = Path.Combine(jpegDirectory, "JpegTemp.jpg");

                    // Delete the original
                    originalImage.Delete();

                    // Save the new image 
                    using (Stream jpegStreamOut = File.Open(imageFlePath, FileMode.CreateNew, FileAccess.ReadWrite))
                    {
                        encoder.Save(jpegStreamOut);
                    }
                }
            }
        }
    }
Community
  • 1
  • 1
Frank Wang
  • 114
  • 2
  • 6
0

You can do this quite simply with the MetadataExtractor library (available via NuGet):

JpegCommentDirectory jpegCommentDirectory = ImageMetadataReader.ReadMetadata(imagePath)
     .OfType<JpegCommentDirectory>()
     .FirstOrDefault();

string comment = jpegCommentDirectory?.GetDescription(JpegCommentDirectory.TagComment);
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742