Reputation: 18799
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
Upvotes: 1
Views: 3073
Reputation: 18799
Got it... Replace
prop.Value[0] = 1;
with
ImportedPhotoMetaData.SetQuery("System.Photo.Orientation", 1);
Upvotes: 1