2011-10-24 120 views
0

我正在使用GDI +進行圖像旋轉。根據圖像元數據,應該有8種不同的方向(http://www.impulseadventure.com/photo/exif-orientation.html)。但是對於所有圖像,無論水平還是垂直,我都獲得相同的方向。任何人都可以提出問題在哪裏,或者我錯過了什麼?使用GDI +旋轉圖像+

謝謝!

+0

FYI GDI +和ASP.NET [不一定發揮好(http://msdn.microsoft.com/en-us/library/system.drawing.aspx)。圖形命名空間不支持在Windows或ASP.NET服務中使用。試圖從這些應用程序類型中使用這些類可能會產生意想不到的問題,例如服務性能下降和運行時異常。有關受支持的替代方法,請參閱Windows映像組件。# –

+0

Windows映像組件也不受支持,文檔不正確。我也100%確定System.Drawing不被支持,因爲它非常難以正確使用,如果使用不當,它會導致內存和處理泄漏。但可以正確使用它,並且安全。 –

+0

[說明性] Windows映像組件僅在Windows Server 2008和更高版本的發行版本2以及Windows 7上受支持。早期版本受到與GDI +相同的問題影響。 WPF不支持,期間。他們仍然發現新的內存泄漏。如果您遵循所有規則並知道所有錯誤,那麼GDI +和WIC(在這兩個平臺上)都可以安全地用於Web服務。 [imageresizing.net](http://imageresizing.net)庫允許你使用任何一個。 –

回答

0

幾天前我添加了自動旋轉到the imageresizing.net library作爲AutoRotate插件。我包含了相關的源代碼,希望能對你有所幫助。

if (!"true".Equals(settings["autorotate"], StringComparison.OrdinalIgnoreCase)) return RequestedAction.None; 

int propertyId = 0x0112; 
PropertyItem pi; 
try { 
    pi = b.GetPropertyItem(propertyId); 
} catch (ArgumentException) { 
    return RequestedAction.None; 
} 
if (pi == null) return RequestedAction.None; 

int total = 0; 

foreach (byte by in pi.Value) total += by; //Does not handle values larger than 255, but it doesn't need to, and is endian-agnostic. 

if (total == 8) b.RotateFlip(RotateFlipType.Rotate270FlipNone); 
if (total == 3) b.RotateFlip(RotateFlipType.Rotate180FlipNone); 
if (total == 6) b.RotateFlip(RotateFlipType.Rotate90FlipNone); 

if (total == 2) b.RotateFlip(RotateFlipType.RotateNoneFlipX); 
if (total == 4) b.RotateFlip(RotateFlipType.Rotate180FlipX); 
if (total == 5) b.RotateFlip(RotateFlipType.Rotate270FlipY); 
if (total == 7) b.RotateFlip(RotateFlipType.Rotate90FlipY); 

b.RemovePropertyItem(propertyId); 

作爲一個僅供參考,如果你在ASP.NET,you should read this article如何找到安全的做法,或者使用ImageResizing.Net library,而不是做圖像大小調整。

1

只是一個小的改進,

我不會測試一個ArgumentException,,成本CPU週期。在系統內`類:

var orientation_index = Array.IndexOf(b.PropertyIdList, propertyId); 

     if (orientation_index <0) return RequestedAction.None; 
     byte total =0; 
     foreach (byte b in b.GetPropertyItem(OrientationId).Value) 
     { 
      total += b; 
     }