c# - 从IOS图片上传到.net app : Rotate

标签 c# asp.net .net file-upload image

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

最佳答案

给你我的 friend :

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

关于c# - 从IOS图片上传到.net app : Rotate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17186568/

相关文章:

c# - 避免 dotnetopenauth url 中的 "must contain period"限制

c# - 异常处理中函数参数的一般记录

c# - 页面事件根本没有被触发

c# - asp.net 中的内容而不是 java 中的 httpsessionlistener

c# - Modal RadWindow 未在 Chrome 中关闭

c# - 如何在特定页面类型而不是父页面上要求 SSL

c# - 我应该如何处理 .NET 中的套接字断开连接?

c# - 使用阻止在 VB.NET 中编译的自定义文本创建错误(C# 中的#error)

c# - 读取值时是否应该锁定资源?

c# - DataTemplate 通过转换器绑定(bind)网格背景