netcore使用ImageSharp裁切图片
项目中需要对上传的图片进行裁切处理,发现之前的裁切方法不能跨平台使用了,于是乎找到了ImageSharp项目,项目地址https://github.com/SixLabors/ImageSharp,参考项目说明中的方法,很简单的几句话就完成了需求,感谢各位大牛做的的杰出贡献。
#region 图片处理 /// <summary> /// 裁切图片大小 /// </summary> /// <param name="imagePath">图片路径</param> /// <param name="maxSize">裁切尺寸</param> /// <param name="isCreateNew">是否另存</param> public static void CutImage(string imagePath, int maxSize, bool isCreateNew = false) { using (Image<Rgba32> image = Image.Load(imagePath)) { if (isCreateNew == true) { imagePath = imagePath.Insert(imagePath.Length - 4, "_" + maxSize); } if (image.Width > image.Height) { if (image.Width > maxSize) { image.Mutate(x => x .Resize(maxSize, maxSize / image.Width * image.Height)); image.Save(imagePath); } } else { if (image.Height > maxSize) { image.Mutate(x => x .Resize(maxSize / image.Height * image.Width, maxSize)); image.Save(imagePath); } } }; } #endregion