当前位置:首页 > 编程笔记 > 正文
已解决

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

来自网友在路上 169869提问 提问时间:2023-10-30 23:45:56阅读次数: 69

最佳答案 问答题库698位专家为你答疑解惑

.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,并返回上传后的文件名

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

nuget安装SixLabors.ImageSharp

新建类 ImageSharpTools.cs

 public class ImageSharpTools{/// <summary>/// 调整图片尺寸/// </summary>/// <param name="imageBytes">字节流</param>/// <param name="ext">后缀名</param>/// <param name="towidth">设置宽度</param>/// <param name="toheight">设置高度</param>/// <returns></returns>public static byte[] ImageReSise(byte[] imageBytes,string ext,int towidth,int toheight){var image = Image.Load(imageBytes);int imageWidh = image.Width;int imageHight = image.Height;if (imageWidh > imageHight){//如果宽大于高,调整比例if (imageWidh > towidth){toheight = (int)(imageHight * ((double)towidth / (double)imageWidh));imageWidh = towidth;}else{towidth = imageWidh;}}if (imageWidh < imageHight){ //如果宽小于高,调整比例if (imageHight > toheight){towidth = (int)(imageWidh * ((double)toheight / (double)imageHight));imageHight = toheight;}else{toheight = imageHight;}}//调整图片尺寸image.Mutate(x => x.Resize(towidth, toheight, KnownResamplers.Spline));MemoryStream ms = new MemoryStream();image.SaveAsPngAsync(ms);var byteFile = ms.ToArray();ms.Close();ms.Dispose();image.Dispose();return byteFile;}
}

nuget安装FastDFSNetCore

新建类:FastDFSNetCoreHelper.cs

using FastDFS.Client;
using System.Net;public class FastDFSNetCoreHelper{public string Upload(byte[] imgBytes, string ext){if (ext.Contains(".")){ext = ext.Replace(".", "");}           List<IPEndPoint> pEndPoints = new List<IPEndPoint>(){//设置dfs的服务器地址和端口new IPEndPoint(IPAddress.Parse("10.112.250.130"), 2315)};ConnectionManager.Initialize(pEndPoints);StorageNode storageNode = FastDFSClient.GetStorageNodeAsync().Result;var str = FastDFSClient.UploadFileAsync(storageNode, imgBytes, ext);return "/" + storageNode.GroupName + "/" + str.Result.ToString();}}

完美OK

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"c# .net linux ImageSharp+FastDFS+Base64上传图片,压缩图片大小,图像处理dcoker中使用也可以":http://eshow365.cn/6-28154-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!