2

I am having some trouble with Image control in WPF.

I have one jpg file, which loads with wrong rotation and even i rotate this picture in windows (right click and rotate left/right) there is no change in application.

Seems that there are some EXIF metadata in the image, which gets rotated together with a wrong image.

I'm reading the image from www so I do not have local file (and I don't want to have it). Here's how I'm converting byte[] to BitmapImage:

public static BitmapImage BitmapImageFromByteArray(Byte[] bytes)
{
    MemoryStream stream = new MemoryStream(bytes);
    BitmapImage image = new BitmapImage();
    image.BeginInit();
    image.CacheOption = BitmapCacheOption.OnLoad;
    image.StreamSource = stream;
    image.EndInit();
    stream.Close();
    stream.Dispose();
    return image;
}

So there are 2 ways of handling that:

  1. Set Image control to ignore EXIF metadata
  2. Remove EXIF metadata from BitmapImage

Can you help me with handling any of these?

soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
Tomasz
  • 2,051
  • 3
  • 31
  • 64
  • As a note, you don't need to call `stream.Close()` and `stream.Dispose()` at the same time. One is enough. Even better, use a `using` block, which automatically disposes the stream, like `using (var stream = new MemoryStream(bytes)) { ... }`. – Clemens Feb 06 '17 at 21:15
  • I know, this code is after tons of tries so it's not optimized :) – Tomasz Feb 06 '17 at 21:36
  • 1
    Did you try out the code on the link I posted or what happened? – mm8 Feb 07 '17 at 22:16

1 Answers1

0

Check out the code sample on the following link.

Remove Exif data from image files with C# and WPF libraries: http://www.techmikael.com/2009/07/remove-exif-data-from-image-files-with.html

Another option may be to use a RotateTransform to rotate the Image element:

How to do rotation around control's center in XAML

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88