当前位置:   article > 正文

c# .net linux ImageSharp+FastDFS+Base64上传图片,压缩图片大小,图像处理dcoker中使用也可以_skiasharp 图片压缩

skiasharp 图片压缩

.net 以前是用System.Drawing来处理图片,但是在dcoker 、linux上用不了

微软官方推荐用

1、SkiaSharp

如果项目运行到docker里,需要NUGET安装SkiaSharp.NativeAssets.Linux.NoDependencies

注意:如果你同时引用SkiaSharp.NativeAssets.Linux和SkiaSharp.NativeAssets.Linux.NoDependencies 可能会导致docker中运行报错,记得只能引用一个SkiaSharp.NativeAssets.Linux.NoDependencies

2、ImageSharp 

我感觉这个用起来简单一些

nuget安装SixLabors.ImageSharp

使用:

这里用ImageSharp 为例子

我这里是通过jquery蒋图片转为base64 ,用法见jquery把图片路径转成base64_mob649e815e258d的技术博客_51CTO博客

新建controller,接收前端提交过来的base64,并返回上传后的文件名

  1. public string addFileToServer(string base64stringdata, string oldfilename)
  2. {
  3. byte[] imgBytes;
  4. if (base64stringdata.Contains(","))
  5. {
  6. //前端用jQuery将图片路径转换为base64的话,这里需要
  7. // 或者在jquery取值时先将Data URL转换为base64字符串var base64String = dataURL.split(",")[1];
  8. imgBytes = Convert.FromBase64String(base64stringdata.Remove(0, base64stringdata.IndexOf(',') + 1));
  9. }
  10. else
  11. {
  12. imgBytes = Convert.FromBase64String(base64stringdata);
  13. }
  14. //取后缀名
  15. string strext = System.IO.Path.GetExtension(oldfilename);
  16. if (strext == ".jpg" || strext == ".gif" || strext == ".jpeg" || strext == ".bmp" || strext == ".png")
  17. { //图片自动压缩 并上传
  18. imgBytes = ImageSharpTools.ImageReSise(imgBytes, strext, 800, 800);
  19. }
  20. //上传文件
  21. string returnFileName = new FastDFSNetCoreHelper().Upload(imgBytes, strext);
  22. return returnFileName ;
  23. }

nuget安装SixLabors.ImageSharp

新建类 ImageSharpTools.cs

  1. public class ImageSharpTools
  2. {
  3. /// <summary>
  4. /// 调整图片尺寸
  5. /// </summary>
  6. /// <param name="imageBytes">字节流</param>
  7. /// <param name="ext">后缀名</param>
  8. /// <param name="towidth">设置宽度</param>
  9. /// <param name="toheight">设置高度</param>
  10. /// <returns></returns>
  11. public static byte[] ImageReSise(byte[] imageBytes,string ext,int towidth,int toheight)
  12. {
  13. var image = Image.Load(imageBytes);
  14. int imageWidh = image.Width;
  15. int imageHight = image.Height;
  16. if (imageWidh > imageHight)
  17. {//如果宽大于高,调整比例
  18. if (imageWidh > towidth)
  19. {
  20. toheight = (int)(imageHight * ((double)towidth / (double)imageWidh));
  21. imageWidh = towidth;
  22. }
  23. else
  24. {
  25. towidth = imageWidh;
  26. }
  27. }
  28. if (imageWidh < imageHight)
  29. { //如果宽小于高,调整比例
  30. if (imageHight > toheight)
  31. {
  32. towidth = (int)(imageWidh * ((double)toheight / (double)imageHight));
  33. imageHight = toheight;
  34. }
  35. else
  36. {
  37. toheight = imageHight;
  38. }
  39. }
  40. //调整图片尺寸
  41. image.Mutate(x => x.Resize(towidth, toheight, KnownResamplers.Spline));
  42. MemoryStream ms = new MemoryStream();
  43. image.SaveAsPngAsync(ms);
  44. var byteFile = ms.ToArray();
  45. ms.Close();
  46. ms.Dispose();
  47. image.Dispose();
  48. return byteFile;
  49. }
  50. }

nuget安装FastDFSNetCore

新建类:FastDFSNetCoreHelper.cs

  1. using FastDFS.Client;
  2. using System.Net;
  3. public class FastDFSNetCoreHelper
  4. {
  5. public string Upload(byte[] imgBytes, string ext)
  6. {
  7. if (ext.Contains("."))
  8. {
  9. ext = ext.Replace(".", "");
  10. }
  11. List<IPEndPoint> pEndPoints = new List<IPEndPoint>()
  12. {
  13. //设置dfs的服务器地址和端口
  14. new IPEndPoint(IPAddress.Parse("10.112.250.130"), 2315)
  15. };
  16. ConnectionManager.Initialize(pEndPoints);
  17. StorageNode storageNode = FastDFSClient.GetStorageNodeAsync().Result;
  18. var str = FastDFSClient.UploadFileAsync(storageNode, imgBytes, ext);
  19. return "/" + storageNode.GroupName + "/" + str.Result.ToString();
  20. }
  21. }

完美OK

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/125149
推荐阅读
相关标签
  

闽ICP备14008679号