none
对于图片在元数据上有旋转的情况下 怎么在image空间中正常显示 RRS feed

  • 问题

  • http://nim.nos.netease.com/NDE0NTY5MQ==/bmltYV8xNTczMTI4OTNfMTQ5MjMxODExMDk1MV80MjZiYjVlMi02OTFjLTQ3YWQtOTAwOC1lNWQzNDk4ZGUzMzI=

    orientation of image 90° CCW(right/top)

    直接绑定在空间上 <Image Source="http://nim.nos.netease.com/NDE0NTY5MQ==/bmltYV8xNTczMTI4OTNfMTQ5MjMxODExMDk1MV80MjZiYjVlMi02OTFjLTQ3YWQtOTAwOC1lNWQzNDk4ZGUzMzI=" ></Image>也是颠倒显示。

    因为BitmapImage无法设置元数据  那该怎么正常显示图片呢  ?  我无法判断元数据里到底该不该旋转

    2017年6月7日 4:14

答案

  • Hi,

    尝试使用下面的代码来判断是否需要旋转。

    /// <summary>
    	/// A converter used to automatically orient a picture based on orientation metadata embedded in the image.
    	/// </summary>
    	/// <remarks>
    	/// Refer to http://www.impulseadventure.com/photo/exif-orientation.html for details on orientation values.
    	/// </remarks>
    	public sealed class BitmapOrientationConverter : IValueConverter
    	{
    		private const string _orientationQuery = "System.Photo.Orientation";
    
    		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    		{
    			string path = value as string;
    
    			if (path != null)
    			{
    				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)
    						{
    							//refer to http://www.impulseadventure.com/photo/exif-orientation.html for details on orientation values
    							switch ((ushort) o)
    							{
    								case 6:
    									return 90D;
    								case 3:
    									return 180D;
    								case 8:
    									return 270D;
    							}
    						}
    					}
    				}
    			}
    
    			return DependencyProperty.UnsetValue;
    		}
    
    		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    		{
    			return DependencyProperty.UnsetValue;
    		}
    	}

    Reference: http://intuipic.codeplex.com/SourceControl/changeset/view/7348#114514

    另外,参考: http://www.techmikael.com/2009/07/remove-exif-data-from-image-files-with.html

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年6月7日 7:54
    版主