.net?以前是用System.Drawing來處理圖片,但是在dcoker 、linux上用不了
微軟官方推薦用
1、SkiaSharp
如果項目運(yùn)行到docker里,需要NUGET安裝SkiaSharp.NativeAssets.Linux.NoDependencies
注意:如果你同時引用SkiaSharp.NativeAssets.Linux和SkiaSharp.NativeAssets.Linux.NoDependencies?可能會導(dǎo)致docker中運(yùn)行報錯,記得只能引用一個SkiaSharp.NativeAssets.Linux.NoDependencies
2、ImageSharp?
我感覺這個用起來簡單一些
nuget安裝SixLabors.ImageSharp
使用:
這里用ImageSharp?為例子
我這里是通過jquery蔣圖片轉(zhuǎn)為base64 ,用法見jquery把圖片路徑轉(zhuǎn)成base64_mob649e815e258d的技術(shù)博客_51CTO博客
新建controller,接收前端提交過來的base64,并返回上傳后的文件名
public string addFileToServer(string base64stringdata, string oldfilename)
{
byte[] imgBytes;
if (base64stringdata.Contains(","))
{
//前端用jQuery將圖片路徑轉(zhuǎn)換為base64的話,這里需要
// 或者在jquery取值時先將Data URL轉(zhuǎn)換為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>
/// 調(diào)整圖片尺寸
/// </summary>
/// <param name="imageBytes">字節(jié)流</param>
/// <param name="ext">后綴名</param>
/// <param name="towidth">設(shè)置寬度</param>
/// <param name="toheight">設(shè)置高度</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)
{//如果寬大于高,調(diào)整比例
if (imageWidh > towidth)
{
toheight = (int)(imageHight * ((double)towidth / (double)imageWidh));
imageWidh = towidth;
}
else
{
towidth = imageWidh;
}
}
if (imageWidh < imageHight)
{ //如果寬小于高,調(diào)整比例
if (imageHight > toheight)
{
towidth = (int)(imageWidh * ((double)toheight / (double)imageHight));
imageHight = toheight;
}
else
{
toheight = imageHight;
}
}
//調(diào)整圖片尺寸
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文章來源:http://www.zghlxwxcb.cn/news/detail-726407.html
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>()
{
//設(shè)置dfs的服務(wù)器地址和端口
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文章來源地址http://www.zghlxwxcb.cn/news/detail-726407.html
到了這里,關(guān)于c# .net linux ImageSharp+FastDFS+Base64上傳圖片,壓縮圖片大小,圖像處理dcoker中使用也可以的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!