Asp.Net 核心:使用 ImageSharp 压缩图像并使用 Blob 保存到数据库

Asp.Net Core : Compress the image using ImageSharp and save to database using Blob

我尝试使用 Imagesharp Nuget 包压缩图像并将其作为 Blob 文件保存到我的 sql 数据库,但问题是图像压缩成功..但 不仅仅是压缩图像存储原始的, 示例:Pictureone.jpg 图像大小为 3mb..上传后图像大小被压缩为 40kb,但是当我申请将压缩文件保存在数据库中时,它会保存原始文件(3mb)。 这是我的代码

型号BlobImage.cs

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();
            }
        }

你在这一行压缩图像:

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();
    }