【问题标题】:Byte Image Rotation in .Net Core.Net Core 中的字节图像旋转
【发布时间】:2018-04-26 06:04:20
【问题描述】:

我有一个 IFormFile 图像文件(来自邮递员的表单数据),我将其转换为字节数组。在将其转换为字节数组之前,我想将其旋转到其实际位置(如果用户输入图像为 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();
    }
}

任何帮助,提前致谢。

【问题讨论】:

    标签: asp.net-web-api asp.net-core-2.0


    【解决方案1】:

    Magick.NET,.Net Core 的 ImageMagick 包装器可用于许多文件操作,请参阅 https://github.com/dlemstra/Magick.NET

    【讨论】:

      【解决方案2】:

      在我的项目中,我需要裁剪用户上传的图片并调整其大小。我正在使用一个名为ImageSharp from Six Labors 的奇妙库。您可以使用它的图像处理器进行调整大小、裁剪、倾斜、旋转等转换!

      通过 NuGet 安装

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

      1. Visual Studio -> 工具 -> 选项 -> NuGet 包管理器 -> 包源
      2. 点击“加号”按钮添加新的包资源
      3. 我输入了“ImageSharp Nightly”作为名称,并将“https://www.myget.org/F/sixlabors/api/v3/index.json”作为源网址。
      4. 在浏览时,搜索“SixLabors.ImageSharp”(在我的情况下,我还需要“SixLabors.ImageSharp.Drawing”,但在您的情况下,您可能只需要核心库。请始终参考他们的文档)。

      裁剪和调整大小

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

      希望这对您有所帮助 - ImageSharp 库的忠实粉丝!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-27
        • 2011-06-25
        • 2014-10-23
        • 2020-06-25
        • 1970-01-01
        • 1970-01-01
        • 2018-10-12
        • 1970-01-01
        相关资源
        最近更新 更多