3

I am new to WPF and cannot find some workaround. I have a basic image controll defined in XAML. I am loading bitmap images dynamically to this controll. The problem is that some of the bitmaps get flipped in the Image controll, when it's loaded and I want to load all images with it's default orientation.

This is my XAML:

<StackPanel Width="Auto" DockPanel.Dock="Left" Margin="1,1,0,1" Orientation="Vertical" Background="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}" HorizontalAlignment="Left" VerticalAlignment="Stretch" Height="Auto" >
   <Image x:Name="Photo"  Stretch="Uniform"  Height="149" Width="134" Margin="21,25,0,20" HorizontalAlignment="Left" VerticalAlignment="Top" />
</StackPanel>

c# code:

BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(child.profilPoto.path, UriKind.Absolute);
img.EndInit();
Photo.Source = img;

thanks

EDIT:

Here is a link for image: img On the left side there is a how the image is displayed in WPF. On the right side is original image.

EDIT 2: Original image

SOLVED Thanks to all. It was metadata issue. I had a lot of photos with bad metadata. When i tried to load images from other camera it was ok. After saving broken images in some editor the photos loaded properly.

user2957509
  • 31
  • 1
  • 3
  • I dont see any problem with the above code. – Khushi Dec 31 '13 at 01:50
  • I cant reproduce it with code. Can you show how original image different when viewed in WPF image control? – AjS Dec 31 '13 at 02:58
  • 2
    Is Any Scale Transform or any other transform applied to its parent? – Sankarann Dec 31 '13 at 05:39
  • Please post the original image file – Thomas Levesque Dec 31 '13 at 09:17
  • I added picture to my question and edited XAML. @Sankarann - No transform is applied to it's parent. – user2957509 Dec 31 '13 at 09:17
  • @user2957509, this is not the original image, it's a screenshot of what you're seeing... I need to see the original file to see its metadata. – Thomas Levesque Dec 31 '13 at 09:23
  • @Thomas Levesque, I added Original image. – user2957509 Dec 31 '13 at 09:34
  • 2
    @user2957509, I supected that the image specified a non-default orientation in its metadata, but it doesn't. I tried to display it in a WPF application, and it's displayed correctly (no rotation). – Thomas Levesque Dec 31 '13 at 09:40
  • Maybe it's problem with my layout. I'll try to load image in new project and if it will be ok, then I'll modify my layout. – user2957509 Dec 31 '13 at 09:52
  • Problem solved. Thanks to all. It was metadata issue. I had a lot of photos with bad metadata. When i tried to load images from other camera it was ok. After saving broken images in some editor the photos loaded properly. – user2957509 Dec 31 '13 at 12:05
  • You may want to delete the question, or edit it to say you've found the problem. (Your comment in which you say you've fixed it is actually so far down that it doesn't appear on screen by default - people have to click to see extra comments.) I just wasted 5 minutes attempting to repro this... – Ian Griffiths Dec 31 '13 at 14:51

1 Answers1

6

Be cause BitmapMetadata, This is my code, And it works corectly

private static string _orientationQuery = "System.Photo.Orientation";
public static BitmapSource LoadImageFile(String path)
{
  Rotation rotation = Rotation.Rotate0;
  using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
  {
    BitmapFrame bitmapFrame = BitmapFrame.Create(fileStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
    BitmapMetadata bitmapMetadata = bitmapFrame.Metadata as BitmapMetadata;

    if ((bitmapMetadata != null) && (bitmapMetadata.ContainsQuery(_orientationQuery)))
    {
      object o = bitmapMetadata.GetQuery(_orientationQuery);

      if (o != null)
      {
        switch ((ushort)o)
        {
          case 6:
            {
              rotation = Rotation.Rotate90;
            }
            break;
          case 3:
            {
              rotation = Rotation.Rotate180;
            }
            break;
          case 8:
            {
              rotation = Rotation.Rotate270;
            }
            break;
        }
      }
    }
  }

  BitmapImage _image = new BitmapImage();
  _image.BeginInit();
  _image.UriSource = new Uri(path);
  _image.Rotation = rotation;
  _image.EndInit();
  _image.Freeze();

  return _image;
}