Asp.net web api Net内核中的字节图像旋转

Asp.net web api Net内核中的字节图像旋转,asp.net-web-api,asp.net-core-2.0,Asp.net Web Api,Asp.net Core 2.0,我有一个IFormFile图像文件(作为表单数据来自postman),我将其转换为字节数组。在将其转换为字节数组之前,我想将其旋转到实际位置(如果用户输入图像为90°(右)。我正在asp.net core 2.0中实现web api byte[] ImageBytes = Utils.ConvertFileToByteArray(model.Image); public static byte[] ConvertFileToByteArray(IFormFile file) { us

我有一个IFormFile图像文件(作为表单数据来自postman),我将其转换为字节数组。在将其转换为字节数组之前,我想将其旋转到实际位置(如果用户输入图像为90°(右)。我正在asp.net core 2.0中实现web api

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


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

任何帮助,请提前感谢。

在我的项目中,我需要裁剪和调整用户上传的图像。我正在使用一个很棒的库,名为。您可以使用它的图像处理器进行变换,如调整大小、裁剪、倾斜、旋转等

通过NuGet安装 我实际上是通过MyGet使用他们的夜间构建

  • Visual Studio->工具->选项->NuGet包管理器->包源
  • 点击“加号”按钮添加新的包资源
  • 我键入“ImageSharp Nightly”作为名称,并将“”作为源url
  • 浏览时,搜索“SixLabors.ImageSharp”(在我的情况下,我也需要“SixLabors.ImageSharp.Drawing”,但在您的情况下,您可能只需要核心库。始终参考他们的文档)
  • 裁剪和调整大小
    希望这对您有所帮助-来自ImageSharp library的忠实粉丝!

    Magick.NET,ImageMagick包装器for.NET Core可用于许多文件操作,请参阅

    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()
                    };
                }
            }
        }
    }