一、nuget
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
二、代码
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;namespace pdfdemo.Controllers
{public class HomeController : Controller{private readonly ILogger<HomeController> _logger;public HomeController(ILogger<HomeController> logger){_logger = logger;}public IActionResult Index(){var oriPath = $"{AppDomain.CurrentDomain.BaseDirectory}/1336349309181998041.png";var newpath = $"{AppDomain.CurrentDomain.BaseDirectory}/1336349309181998042.png";using (Image image = Image.Load(oriPath)){Rectangle cropArea = new Rectangle(0, 60, image.Width, image.Height-60);using (Image croppedImage = image.Clone(ctx => ctx.Crop(cropArea))){croppedImage.Save(newpath);}}return View();}}
}
原图:
截取后: