Amit Mishra
Amit Mishra

Reputation: 69

Byte Image Rotation in .Net Core

I've an IFormFile image file (from postman as form data), which I convert into byte array. Before converting it into byte array, I want to rotate it into its actual position (if user input image as 90°(right). I'm implementing web api in asp.net core 2.0.

byte[] ImageBytes = Utils.ConvertFileToByteArray(model.Image);


public static byte[] ConvertFileToByteArray(IFormFile file)
{
    using (var memoryStream = new MemoryStream())
    {
        file.CopyTo(memoryStream);
        return memoryStream.ToArray();
    }
}

Any help, Thanks in advance.

Upvotes: 1

Views: 2684

Answers (3)

Akshay Bandhara
Akshay Bandhara

Reputation: 1

byte[] byt = System.IO.File.ReadAllBytes(filePath);
System.IO.MemoryStream ms = new System.IO.MemoryStream(byt);
using (Image img = Image.FromStream(ms))
{
    RotateFlipType r = angle == 90 ? RotateFlipType.Rotate90FlipNone : RotateFlipType.Rotate270FlipNone;
    img.RotateFlip(r);
    img.Save(filePath);
}

Using your existing code you can do the following

Upvotes: 0

Mark Redman
Mark Redman

Reputation: 24515

Magick.NET, The ImageMagick wrapper for .Net Core can be used for many file manipulations, see https://github.com/dlemstra/Magick.NET

Upvotes: 2

David Liang
David Liang

Reputation: 21476

In my project I need to crop and resize the images users upload. And I am using a fantastic library called ImageSharp from Six Labors. You can use its image processor to do the transformation such as Resize, Crop, Skew, Rotate and more!

Install via NuGet

I am actually using their nightly build through MyGet.

  1. Visual Studio -> Tools -> Options -> NuGet Package Manager -> Package Sources
  2. Hit the "Plus" button to add a new package resource
  3. I typed "ImageSharp Nightly" as the name and put "https://www.myget.org/F/sixlabors/api/v3/index.json" as the source url.
  4. On Browse, search "SixLabors.ImageSharp" (In my case I also need "SixLabors.ImageSharp.Drawing" but in your case you might only need to core library. Always refer back to their documentations).

Crop & Resize

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Transforms;
using SixLabors.Primitives;
using System.IO;

namespace DL.SO.Project.Services.ImageProcessing.ImageSharp
{
    public CropAndResizeResult CropAndResize(byte[] originalImage, 
        int offsetX, int offsetY, int croppedWidth, int croppedHeight, 
        int finalWidth, int finalHeight) : IImageProcessingService
    {
        IImageFormat format;
        using (var image = Image.Load(originalImage, out format))
        {
            image.Mutate(x => x

                // There is .Rotate() you can call for your case

                .Crop(new Rectangle(offsetX, offsetY, croppedWidth, croppedHeight))
                .Resize(finalWidth, finalHeight));

            using (var output = new MemoryStream())
            {
                image.Save(output, format);

                // This is just my custom class. But see you can easily
                // get the processed image byte[] using the ToArray() method.

                return new CropAndResizeResult
                {
                    ImageExtension = format.Name,
                    CroppedImage = output.ToArray()
                };
            }
        }
    }
}

Hope this helps you - from a big fan of ImageSharp library!

Upvotes: 2

Related Questions