1

I am importing a picture into my application and rotating the image according to the EXIF metadata. After this I am saving the rotated image to my disk, but as I have still left the rotated image metadata on the image and windows thinks it should rotate it again... basically meaning my image ends up upside down.

So far I have got:

            using (Stream sourceStream = File.Open(dlg.FileName, FileMode.Open, FileAccess.Read))
            {
                BitmapDecoder sourceDecoder = BitmapDecoder.Create(sourceStream, createOptions, BitmapCacheOption.OnLoad);
                // Check source is has valid frames 
                if (sourceDecoder.Frames[0] != null && sourceDecoder.Frames[0].Metadata != null)
                {
                    sourceDecoder.Frames[0].Metadata.Freeze();
                    // Get a clone copy of the metadata
                    BitmapMetadata sourceMetadata = sourceDecoder.Frames[0].Metadata.Clone() as BitmapMetadata;
                    ImportedPhotoMetaData = sourceMetadata;
                }
            }

and

                using (var image = Image.FromFile(dlg.FileName))
                {
                    foreach (var prop in image.PropertyItems)
                    {
                        if (prop.Id == 0x112)
                        {
                            if (prop.Value[0] == 6)
                                rotate = 90;
                            if (prop.Value[0] == 8)
                                rotate = -90;
                            if (prop.Value[0] == 3)
                                rotate = 180;
                            prop.Value[0] = 1;
                        }
                    }
                }

but the prop.Value[0] = 1; line does not seem to reset the image metadata. I need to reset the image orientation on the ImportedPhotoMetaData property

JKennedy
  • 18,150
  • 17
  • 114
  • 198
  • Possible duplicate of [Problem reading JPEG Metadata (Orientation)](http://stackoverflow.com/questions/6222053/problem-reading-jpeg-metadata-orientation) – Erik Oct 24 '15 at 01:42

1 Answers1

1

Got it... Replace

prop.Value[0] = 1;

with

ImportedPhotoMetaData.SetQuery("System.Photo.Orientation", 1);
JKennedy
  • 18,150
  • 17
  • 114
  • 198
  • I'm trying to use your answer to help me solve a problem but I'm having difficulty finding the relevant docs. If you would update your answer to include a link to the docs, and a bit more exposition I'd give you an up-vote. – Erik Oct 23 '15 at 23:01
  • For future readers: `ImportedPhotoMetaData` obviously lives in the [`System.Photo` namespace](https://msdn.microsoft.com/en-us/library/windows/desktop/ee872007(v=vs.85).aspx) and might not be available for your target platform or framework version - e.g. I can't use it for .NET4.0. I found [this SO question that has a good answer that is working for me](http://stackoverflow.com/questions/6222053/problem-reading-jpeg-metadata-orientation) – mfeineis Jun 15 '16 at 09:56