从手机上传图片是横着显示

New*_*bie 4 c#

我可以选择上传图片

<input class="images" type="file" id="item" name="Images" />
Run Code Online (Sandbox Code Playgroud)

然后它会像这样保存到我的项目中

Guid g = Guid.NewGuid();
                Images.SaveAs(Server.MapPath("~/Uploads/" + g + ".jpg"));
                fileNames = g.ToString() + ".jpg";
Run Code Online (Sandbox Code Playgroud)

出于某种原因,当有人从手机将图片上传到网站时,它会侧身显示?

Jos*_*ons 5

您可以查看文件的元数据以了解它的旋转方式。

具体来说,将图像作为 .NET Image 类型拉入,然后调用 img.GetPropertyItem(&H112).Value(0)。

这将返回一个整数,表示图像的“旋转值”。

1 = Landscape
3 = Upside-down
6 = Rotated 90 degrees left
8 = Rotated 90 degrees right
Run Code Online (Sandbox Code Playgroud)

知道这一点后,您就可以使用 img.RotateFlip 方法旋转图像。

下面是我写的一个类来解决非常相似的问题。

相关代码在 RotateImage 方法中。

注意:这是在 VB.NET 中,我通过 Telerik 代码转换器运行它,因此对于任何奇怪的语法,我深表歉意

//get the image from the file they gave us, resize it, and rotate it if needed
    OnlineImage onlineImageHelper = new OnlineImage(Context.Request.Files(0).InputStream);
    byte[] pictureLarger = onlineImageHelper.StraightenedThumbnail(new Size(180, 180));
    byte[] pictureSmaller = onlineImageHelper.StraightenedThumbnail(new Size(80, 80));

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

public class OnlineImage
{
    public OnlineImage()
    {
        throw new NotImplementedException();
    }

    public OnlineImage(Stream imageStream)
    {
        _ImageFromUser = Image.FromStream(imageStream);
        RotateImage();
    }

    private Image _ImageFromUser;
    private Image _RotatedImage;
    private Image _ResizedAndRotatedImage;

    private void RotateImage()
    {
        if (_RotatedImage == null && _ImageFromUser != null && _ImageFromUser.PropertyIdList != null && _ImageFromUser.PropertyIdList.Contains(0x112)) {
            int rotationValue = _ImageFromUser.GetPropertyItem(0x112).Value(0);
            switch (rotationValue) {
                case 1:
                    // landscape, do nothing
                    break;
                case 8:
                    // rotated 90 right
                    // de-rotate:
                    _ImageFromUser.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone);
                    break;
                case 3:
                    // bottoms up
                    _ImageFromUser.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone);
                    break;
                case 6:
                    // rotated 90 left
                    _ImageFromUser.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone);
                    break;
            }
            _RotatedImage = _ImageFromUser;
        }
    }

    private void ResizeImage(Size size, bool preserveAspectRatio = true)
    {
        int newWidth = 0;
        int newHeight = 0;
        if (preserveAspectRatio) {
            int originalWidth = _ImageFromUser.Width;
            int originalHeight = _ImageFromUser.Height;
            float percentWidth = Convert.ToSingle(size.Width) / Convert.ToSingle(originalWidth);
            float percentHeight = Convert.ToSingle(size.Height) / Convert.ToSingle(originalHeight);
            float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
            newWidth = Convert.ToInt32(originalWidth * percent);
            newHeight = Convert.ToInt32(originalHeight * percent);
        } else {
            newWidth = size.Width;
            newHeight = size.Height;
        }

        _ResizedAndRotatedImage = new Bitmap(newWidth, newHeight);

        using (Graphics graphicsHandle = Graphics.FromImage(_ResizedAndRotatedImage)) {
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphicsHandle.DrawImage(_ImageFromUser, 0, 0, newWidth, newHeight);
        }
    }

    public byte[] StraightenedThumbnail(Size resizedDimensions)
    {
        byte[] result = null;
        MemoryStream msPicture = new MemoryStream();
        ResizeImage(resizedDimensions);
        if (_ResizedAndRotatedImage != null) {
            _ResizedAndRotatedImage.Save(msPicture, ImageFormat.Png);
            result = msPicture.ToArray();
            return result;
        }

        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)