C# Net内核:使用ImageSharp压缩图像,并使用Blob保存到数据库
我尝试使用Imagesharp Nuget包压缩图像,并将其作为Blob文件保存到我的sql数据库中,但问题是图像已成功压缩。。但是压缩后的图像不存储原始图像, 示例:Pictureone.jpg图像大小为3mb。上传后,图像大小被压缩为40kb,但当我应用于在数据库中保存压缩文件时,其保存原始文件(3mb)。 这是我的密码 模型BlobImage.csC# Net内核:使用ImageSharp压缩图像,并使用Blob保存到数据库,c#,asp.net,asp.net-mvc,asp.net-core,asp.net-core-mvc,C#,Asp.net,Asp.net Mvc,Asp.net Core,Asp.net Core Mvc,我尝试使用Imagesharp Nuget包压缩图像,并将其作为Blob文件保存到我的sql数据库中,但问题是图像已成功压缩。。但是压缩后的图像不存储原始图像, 示例:Pictureone.jpg图像大小为3mb。上传后,图像大小被压缩为40kb,但当我应用于在数据库中保存压缩文件时,其保存原始文件(3mb)。 这是我的密码 模型BlobImage.cs namespace ImageToDb.Models { public class BlobImg { pub
namespace ImageToDb.Models
{
public class BlobImg
{
public int id { get; set; }
public string name { get; set; }
public byte[] image { get; set; }
}
}
ImageController.cs
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
[HttpPost]
public async Task<IActionResult> Index(IFormFile file, BlobImg bolb)
{
string fileName = string.Empty;
string path = string.Empty;
var files = HttpContext.Request.Form.Files;
if (file.Length > 0)
{
using (var img = Image.Load(file.OpenReadStream()))
{
string newSize = ResizeImage(img, 500, 500);
string[] aSize = newSize.Split(',');
img.Mutate(h => h.Resize(Convert.ToInt32(aSize[1]), Convert.ToInt32(aSize[0])));
//This section save the image to database using blob
foreach (var item in files)
{
if (item.Length > 0)
{
using (var stream = new MemoryStream())
{
await item.CopyToAsync(stream);
bolb.image = stream.ToArray();
}
}
}
}
;
}
return View();
}
public string ResizeImage(Image img, int maxWidth, int maxHeight)
{
if (img.Width > maxWidth || img.Height > maxHeight)
{
double widthRatio = (double)img.Width / (double)maxWidth;
double heightRatio = (double)img.Height / (double)maxHeight;
double ratio = Math.Max(widthRatio, heightRatio);
int newWidth = (int)(img.Width / ratio);
int newHeight = (int)(img.Height / ratio);
return newHeight.ToString() + "," + newWidth.ToString();
}
else
{
return img.Height.ToString() + "," + img.Width.ToString();
}
}
使用SixLabors.ImageSharp;
使用六个人工进行图像处理;
[HttpPost]
公共异步任务索引(IFormFile文件,BlobImg bolb)
{
字符串文件名=string.Empty;
字符串路径=string.Empty;
var files=HttpContext.Request.Form.files;
如果(file.Length>0)
{
使用(var img=Image.Load(file.OpenReadStream()))
{
字符串newSize=ResizeImage(img,500500);
字符串[]aSize=newSize.Split(',');
img.Mutate(h=>h.Resize(Convert.ToInt32(aSize[1]),Convert.ToInt32(aSize[0]));
//本节使用blob将图像保存到数据库
foreach(文件中的var项)
{
如果(item.Length>0)
{
使用(var stream=new MemoryStream())
{
等待项。CopyToAsync(流);
bolb.image=stream.ToArray();
}
}
}
}
;
}
返回视图();
}
公共字符串大小图像(图像img、int-maxWidth、int-maxHeight)
{
如果(img.Width>maxWidth | | img.Height>maxHeight)
{
双宽比=(双)img.Width/(双)maxWidth;
双倍高度比=(双倍)最大高度/(双倍)最大高度;
双倍比=数学最大值(宽比、高比);
int newWidth=(int)(img.宽度/比率);
int新高度=(int)(img.高度/比率);
返回newHeight.ToString()+”,“+newWidth.ToString();
}
其他的
{
返回img.Height.ToString()+”,“+img.Width.ToString();
}
}
压缩此行上的图像:
img.Mutate(h => h.Resize(Convert.ToInt32(aSize[1]), Convert.ToInt32(aSize[0])));
然后,您不会在后续的任何代码中再次引用它,而是(我假设)将所有内容保存在HttpContext.Request.Form.Files中。与其这样做,不如将“img”对象保存到数据库中。我看不到任何代码实际上会将任何内容保存到数据库中,但根据您的描述,我怀疑这就是问题所在。因此,您的代码很可能很好—您只是保存了错误的内容
尝试此操作,您需要自己确定图像格式:
public async Task<IActionResult> Index(IFormFile file, BlobImg bolb)
{
if (file.Length > 0)
{
using (var img = Image.Load(file.OpenReadStream()))
{
string newSize = ResizeImage(img, 500, 500);
string[] aSize = newSize.Split(',');
img.Mutate(h => h.Resize(Convert.ToInt32(aSize[1]), Convert.ToInt32(aSize[0])));
//This section save the image to database using blob
using (var ms = new MemoryStream())
{
img.Save(ms, imageFormat);
bolb.image = ms.ToArray();
}
}
}
return View();
}
公共异步任务索引(ifformfile文件,BlobImg bolb)
{
如果(file.Length>0)
{
使用(var img=Image.Load(file.OpenReadStream()))
{
字符串newSize=ResizeImage(img,500500);
字符串[]aSize=newSize.Split(',');
img.Mutate(h=>h.Resize(Convert.ToInt32(aSize[1]),Convert.ToInt32(aSize[0]));
//本节使用blob将图像保存到数据库
使用(var ms=new MemoryStream())
{
图像保存(ms,图像格式);
bolb.image=ms.ToArray();
}
}
}
返回视图();
}
运行后,在我的代码上方,当我在数据库中保存图像时,保存原始图像不是压缩图像,但我要保存压缩图像。请指出将图像保存到数据库的行?即索引操作的每个区域。。我在代码上给它定尺寸。foreach区域没有引用“img”对象,这是您需要保存的对象。您正在保存“files”(即“HttpContext.Request.Form.files”)的内容,而不是保存压缩图像“img”。因此,如果我调用var image=img.Mutate(h=>h.Resize(Convert.ToInt32(aSize[1]),Convert.ToInt32(aSize[0])代码>其获取错误或直接调用它foreach(img中的var项)
也获取错误