Loading an image from a mobile phone is displayed on the side

I have the option to upload an image

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

      

it is then saved in my project like this

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

      

for some reason, when someone uploads a picture to the site from a mobile phone, is it displayed sideways?

+3


source to share


1 answer


You can look at the file's metadata to see how it rotates.

Specifically, pull the image as a .NET Image type, then call img.GetPropertyItem (& H112) .Value (0).

This will return an integer that represents the "rotation value" of the image.

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

      

Once you know this, you can rotate the image using the img.RotateFlip method.



Below is a class I wrote to solve very similar problems.

The corresponding code is in the RotateImage method.

Note: this was in VB.NET and I ran it through telerik's code converter, so my apologies for any weird syntax

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

      

+2


source







All Articles