C# 从IOS图片上载到.net应用程序:旋转

C# 从IOS图片上载到.net应用程序:旋转,c#,asp.net,.net,file-upload,image,C#,Asp.net,.net,File Upload,Image,我有下面的代码,用于将图片从IOS设备上载到我的.net应用程序并调整其大小。用户习惯于以纵向方式拍照,然后所有照片都以错误的旋转显示在我的应用程序中。有什么建议可以解决这个问题吗 string fileName = Server.HtmlEncode(FileUploadFormbilde.FileName); string extension = System.IO.Path.GetExtension(fileName);

我有下面的代码,用于将图片从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);

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

给您,我的朋友:

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;
            }
        }

下面是一个更好的解决方案,他写了一个简单的helper类来完成所有这些:

private System.Drawing.Image ResizeAndDraw(System.Drawing.Image objTempImage)
{
//调用图像帮助器修复方向问题
var temp=ImageHelper.RotateImageByExiForOrientationData(objTempImage,true);
尺寸objSize=新尺寸(150200);
位图objBmp;
objBmp=新位图(objSize.Width、objSize.Height);
Graphics g=Graphics.FromImage(objBmp);
g、 SmoothingMode=SmoothingMode.HighQuality;
g、 插值模式=插值模式。高质量双三次;
g、 PixelOffsetMode=PixelOffsetMode.HighQuality;
//矩形rect=新矩形(x,y,thumbSize.Width,thumbSize.Height);
矩形rect=新矩形(0,0150200);
//g、 DrawImage(objTempImage、rect、0、0、objTempImage.Width、objTempImage.Height、GraphicsUnit.Pixel);
g、 DrawImage(objTempImage,rect);
返回objBmp;
}
使用系统图;
使用系统、绘图、成像;
使用System.Linq;
公共静态类ImageHelper
{
/// 
///根据Exif方向数据旋转给定的图像文件
/// 
///源文件的路径
///目标文件的路径
///目标格式
///将其设置为TRUE以在旋转后更新图像Exif数据(默认值为TRUE)
///与应用的旋转相对应的RotateFlipType值。如果未发生旋转,将返回RotateFlipType.RotateNoneFlipNone。
public static RotateFlipType rotateImageByExiForOrientationData(string sourceFilePath、string targetFilePath、ImageFormat targetFormat、bool updateExifData=true)
{
//根据EXIF数据旋转图像
var bmp=新位图(sourceFilePath);
RotateFlipType fType=RotateImageByExifOrientationData(bmp,updateExifData);
if(fType!=RotateFlipType.RotateNoneFlipNone)
{
保存(targetFilePath,targetFormat);
}
返回fType;
}
/// 
///根据Exif方向数据旋转给定位图
/// 
///源图像
///将其设置为TRUE以在旋转后更新图像Exif数据(默认值为TRUE)
///与应用的旋转相对应的RotateFlipType值。如果未发生旋转,将返回RotateFlipType.RotateNoneFlipNone。
公共静态RotateLipType RotateImageByExiForOrientationData(图像img,bool updateeExiFdata=true)
{
int方向ID=0x0112;
var fType=RotateFlipType.RotateNoneFlipNone;
if(img.PropertyIdList.Contains(orientationId))
{
var pItem=img.GetPropertyItem(方向ID);
fType=GetRotateFlipTypeByExifOrientationData(pItem.Value[0]);
if(fType!=RotateFlipType.RotateNoneFlipNone)
{
img.RotateFlip(fType);
//移除Exif方向标记(如有要求)
if(updateeExifData)img.RemovePropertyItem(定向ID);
}
}
返回fType;
}
/// 
///根据给定的方向EXIF元数据返回正确的System.Drawing.RotateFlipType
/// 
///进出口银行“方向”
///对应的System.Drawing.RotateFlipType枚举值
公共静态RotateLipType GetRotateLipTypeByExiForIentationData(整数方向)
{
开关(方向)
{
案例1:
违约:
返回RotateFlipType.RotateNoneFlipNone;
案例2:
返回RotateFlipType.RotateNoneFlipX;
案例3:
返回RotateFlipType.Rotate180FlipNone;
案例4:
返回RotateFlipType.Rotate180FlipX;
案例5:
返回RotateFlipType.Rotate90FlipX;
案例6:
返回RotateFlipType.Rotate90FlipNone;
案例7:
返回RotateFlipType.Rotate270FlipX;
案例8:
返回RotateFlipType.Rotate270FlipNone;
}
}
}

有代码示例吗?我无法在代码中找到如何成功实现此功能。抱歉。你已经在正确的图像对象上了,我给了你一个阅读链接。我坚信在实践中学习是很重要的,而不是复制粘贴代码。(并不是说有数百个代码示例。)仅供参考,要使用
PropertyIdList.Contains()
您必须使用System.Linq包含
。您在这里保存了我的备份!缺少某些值,此属性可以包含1到8之间的任何值:我的图像显示为挤压。还有其他人经历过吗?
    private System.Drawing.Image ResizeAndDraw(System.Drawing.Image objTempImage)
        {
          // call image helper to fix the orientation issue 
            var temp = ImageHelper.RotateImageByExifOrientationData(objTempImage, true);
            Size objSize = new Size(150, 200);
            Bitmap objBmp;
            objBmp = new Bitmap(objSize.Width, objSize.Height);

            Graphics g = Graphics.FromImage(objBmp);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            //Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
            Rectangle rect = new Rectangle(0,0,150,200);
            //g.DrawImage(objTempImage, rect, 0, 0, objTempImage.Width, objTempImage.Height, GraphicsUnit.Pixel);
            g.DrawImage(objTempImage, rect);
            return objBmp;
        }


using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;

public static class ImageHelper
{
    /// <summary>
    /// Rotate the given image file according to Exif Orientation data
    /// </summary>
    /// <param name="sourceFilePath">path of source file</param>
    /// <param name="targetFilePath">path of target file</param>
    /// <param name="targetFormat">target format</param>
    /// <param name="updateExifData">set it to TRUE to update image Exif data after rotation (default is TRUE)</param>
    /// <returns>The RotateFlipType value corresponding to the applied rotation. If no rotation occurred, RotateFlipType.RotateNoneFlipNone will be returned.</returns>
    public static RotateFlipType RotateImageByExifOrientationData(string sourceFilePath, string targetFilePath, ImageFormat targetFormat, bool updateExifData = true)
    {
        // Rotate the image according to EXIF data
        var bmp = new Bitmap(sourceFilePath);
        RotateFlipType fType = RotateImageByExifOrientationData(bmp, updateExifData);
        if (fType != RotateFlipType.RotateNoneFlipNone)
        {
            bmp.Save(targetFilePath, targetFormat);
        }
        return fType;
    }

    /// <summary>
    /// Rotate the given bitmap according to Exif Orientation data
    /// </summary>
    /// <param name="img">source image</param>
    /// <param name="updateExifData">set it to TRUE to update image Exif data after rotation (default is TRUE)</param>
    /// <returns>The RotateFlipType value corresponding to the applied rotation. If no rotation occurred, RotateFlipType.RotateNoneFlipNone will be returned.</returns>
    public static RotateFlipType RotateImageByExifOrientationData(Image img, bool updateExifData = true)
    {
        int orientationId = 0x0112;
        var fType = RotateFlipType.RotateNoneFlipNone;
        if (img.PropertyIdList.Contains(orientationId))
        {
            var pItem = img.GetPropertyItem(orientationId);
            fType = GetRotateFlipTypeByExifOrientationData(pItem.Value[0]);
            if (fType != RotateFlipType.RotateNoneFlipNone)
            {
                img.RotateFlip(fType);
                // Remove Exif orientation tag (if requested)
                if (updateExifData) img.RemovePropertyItem(orientationId);
            }
        }
        return fType;
    }

    /// <summary>
    /// Return the proper System.Drawing.RotateFlipType according to given orientation EXIF metadata
    /// </summary>
    /// <param name="orientation">Exif "Orientation"</param>
    /// <returns>the corresponding System.Drawing.RotateFlipType enum value</returns>
    public static RotateFlipType GetRotateFlipTypeByExifOrientationData(int orientation)
    {
        switch (orientation)
        {
            case 1:
            default:
                return RotateFlipType.RotateNoneFlipNone;
            case 2:
                return RotateFlipType.RotateNoneFlipX;
            case 3:
                return RotateFlipType.Rotate180FlipNone;
            case 4:
                return RotateFlipType.Rotate180FlipX;
            case 5:
                return RotateFlipType.Rotate90FlipX;
            case 6:
                return RotateFlipType.Rotate90FlipNone;
            case 7:
                return RotateFlipType.Rotate270FlipX;
            case 8:
                return RotateFlipType.Rotate270FlipNone;
        }
    }
}