Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate/Flip image using EXIF data of image in aspnetcore web api

I have a web application on which users can upload profile picture by taking the picture directly by mobile phones. Because of EXIF orientation of the images, the frontend must transform image (rotate/flip) before showing the picture. However, I want the perform the transformation in the backend before saving the picture to avoid the transformation in the frontend. Backend is .net core 2.0 application written in c#.

Does anyone have suggestion regarding the library which can be used to access EXIF data and do the transformation.

As of now, I have found https://github.com/SixLabors/ImageSharp and going through its documentation on how to use it.

like image 609
dudedev Avatar asked Apr 24 '18 17:04

dudedev


People also ask

Can you rotate an image just by changing its Exif data?

i'm pretty sure you cant rotate an image just by changing its exif data. Agreed, EXIF is just metadata. It does not affect the way an image is displayed to a user unless the opening program is specifically looking for it. @Mudd'Dib - Any idea how can this be done?

What is image rotation and flipping in ASP NET?

This article explains image rotation and flipping using ASP.NET. Image rotation and flipping is also called mirror image. This article will tell you about the RotateFlipType Enumerator of the System.Drawing namespace. The rotation and flipping operation can be done using Enum values that are available inside the System.Drawing Namespace.

How do I add a flip and rotate web form?

Select "Web Form" and provide the name of the Web Form (like: "FlipAndRotate.aspx") and click on the "Add" button.

What are the types of enumerators for flipping and rotating images?

Various types of enumerators for flipping and rotating images are listed below in the following table: This rotates 180 degree Clockwise but not flip the image. This rotates 180 degree Clockwise with a horizontal flip. This rotates 180 degree Clockwise with a horizontal flip and vertical flip.


2 Answers

There's a method for that already in the ImageSharp library. AutoOrient() that handles everything for you.

Docs

private byte[] TransformAvatarIfNeeded(byte[] imageInBytes)
{
    using (var image = Image.Load(imageInBytes, out var imageFormat ))
    {
        image.Mutate(x => x.AutoOrient());
        return ImageToByteArray(image, imageFormat);
    }
}
like image 154
James South Avatar answered Oct 16 '22 07:10

James South


Finally, I got it working with ImageSharp library. It might be helpful for others.

    private byte[] TransformAvatarIfNeeded(byte[] imageInBytes)
    {
        using (var image = Image.Load(imageInBytes))
        {
            ExifValue exifOrientation = image.MetaData?.ExifProfile?.GetValue(ExifTag.Orientation);

            if (exifOrientation == null) return imageInBytes;

            RotateMode rotateMode;
            FlipMode flipMode;
            SetRotateFlipMode(exifOrientation, out rotateMode, out flipMode);

            image.Mutate(x => x.RotateFlip(rotateMode, flipMode));
            image.MetaData.ExifProfile.SetValue(ExifTag.Orientation, (ushort)1);

            var imageFormat = Image.DetectFormat(imageInBytes);

            return ImageToByteArray(image, imageFormat);
        }
    }

    private byte[] ImageToByteArray(Image<Rgba32> image, IImageFormat imageFormat)
    {
        using (var ms = new MemoryStream())
        {
            image.Save(ms, imageFormat);
            return ms.ToArray();
        }
    }

    private void SetRotateFlipMode(ExifValue exifOrientation, out RotateMode rotateMode, out FlipMode flipMode)
    {
        var orientation = exifOrientation.Value.ToString();

        switch (orientation)
        {
            case "2":
                rotateMode = RotateMode.None;
                flipMode = FlipMode.Horizontal;
                break;
            case "3":
                rotateMode = RotateMode.Rotate180;
                flipMode = FlipMode.None;
                break;
            case "4":
                rotateMode = RotateMode.Rotate180;
                flipMode = FlipMode.Horizontal;
                break;
            case "5":
                rotateMode = RotateMode.Rotate90;
                flipMode = FlipMode.Horizontal;
                break;
            case "6":
                rotateMode = RotateMode.Rotate90;
                flipMode = FlipMode.None;
                break;
            case "7":
                rotateMode = RotateMode.Rotate90;
                flipMode = FlipMode.Vertical;
                break;
            case "8":
                rotateMode = RotateMode.Rotate270;
                flipMode = FlipMode.None;
                break;
            default:
                rotateMode = RotateMode.None;
                flipMode = FlipMode.None;
                break;
        }
like image 11
dudedev Avatar answered Oct 16 '22 09:10

dudedev