从手机上传图片正在向侧面显示

时间:2017-04-19 17:37:59

标签: c#

我可以选择上传图片

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

ax.imshow(np.random.rand(5,5))
fig.canvas.toolbar.set_message = lambda x: ""

plt.show() 

然后将其保存到我的项目中

<input class="images" type="file" id="item" name="Images" />

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

1 个答案:

答案 0 :(得分:2)

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

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

这将返回一个整数,表示&#34;旋转值&#34;图像。

1 = Landscape
3 = Upside-down
6 = Rotated 90 degrees left
8 = Rotated 90 degrees right

一旦你知道,你就可以使用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;
    }
}