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


    .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. ///
    4. /// 调整图片尺寸
    5. ///
    6. /// 字节流
    7. /// 后缀名
    8. /// 设置宽度
    9. /// 设置高度
    10. ///
    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 pEndPoints = new List()
    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

  • 相关阅读:
    有没有人声和背景音乐分离的神器?
    【YesPMP】众包平台,4月26日最新项目
    C++模版初阶
    @Autowired与@Resource区别
    耗时半月,把牛客网最火Java面试题总结成PDF,涵盖所有面试高频题
    Linux- 自定义一个ARP请求
    git使用杂记
    Bio-MOF-100 金属有机骨架材料
    英语学术论文简短语句摘抄
    Xilinx FPGA----FIFO缓存IP核创建和使用
  • 原文地址:https://blog.csdn.net/qq_16005627/article/details/134073351