2013-06-19 72 views
7

我有下面的代码,用于将图片从IOS设备上传并调整到我的.net应用程序。用户使用纵向拍照,然后所有照片以错误的旋转显示在我的应用程序中。任何建议如何解决这个问题?从IOS图片上传到.net应用程序:旋转

  string fileName = Server.HtmlEncode(FileUploadFormbilde.FileName); 
      string extension = System.IO.Path.GetExtension(fileName); 
      System.Drawing.Image image_file = System.Drawing.Image.FromStream(FileUploadFormbilde.PostedFile.InputStream); 
      int image_height = image_file.Height; 
      int image_width = image_file.Width; 
      int max_height = 300; 
      int max_width = 300; 

      image_height = (image_height * max_width)/image_width; 
      image_width = max_width; 

      if (image_height > max_height) 
      { 
       image_width = (image_width * max_height)/image_height; 
       image_height = max_height; 
      } 

      Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height); 
      System.IO.MemoryStream stream = new System.IO.MemoryStream(); 

      bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Png); 
      stream.Position = 0; 

      byte[] data = new byte[stream.Length + 1]; 
      stream.Read(data, 0, data.Length); 

回答

1

必须从Image.PropertyItems集合中的EXIF数据中读取图像的方向值,并相应地旋转。

+0

任何代码示例?我无法找到如何在我的代码中成功实现这一点。 –

+2

对不起。你已经在正确的图像对象,我给你一个链接来阅读。我坚信重要的是要通过做,而不是通过复制粘贴代码来学习。 (不是说有数百个代码示例在那里。) – Alexander

25

在这里你去我的朋友:

Image originalImage = Image.FromStream(data); 

if (originalImage.PropertyIdList.Contains(0x0112)) 
     { 
      int rotationValue = originalImage.GetPropertyItem(0x0112).Value[0]; 
      switch (rotationValue) 
      { 
       case 1: // landscape, do nothing 
        break; 

       case 8: // rotated 90 right 
        // de-rotate: 
        originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone); 
        break; 

       case 3: // bottoms up 
        originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone); 
        break; 

       case 6: // rotated 90 left 
        originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone); 
        break; 
      } 
     } 
+1

仅供参考,要使用'PropertyIdList.Contains()',您必须包含'using System.Linq'。 –

+0

你在这里保存了我的萌芽! –

+1

某些值缺失,此属性可以包含1到8之间的任何值:http://www.impulseadventure.com/photo/exif-orientation.html – Guillaume

相关问题