简体   繁体   English

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

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

I tried to compress the image using Imagesharp Nuget package and save to my sql database as a Blob file but the problem is image is compressed successfully.. but not the compressed image only store Original one , Example: Pictureone.jpg image size is 3mb..after uploading the image size is compressed like 40kb but when i applied to save in database that compressed file its save original one (3mb).我尝试使用 Imagesharp Nuget package 压缩图像并将图像作为 Blob 文件保存到我的 sql 数据库中,但问题是图像已成功压缩,示例:Picture.jpg仅存储一个 3 mb 大小的压缩图像。 .上传后图像大小被压缩为40kb,但是当我申请保存在数据库中时,压缩文件保存原始文件(3mb)。 Here is my code这是我的代码

Model BlobImage.cs Model 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图像控制器.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();
            }
        }

You compress the image on this line:您在这一行压缩图像:

img.Mutate(h => h.Resize(Convert.ToInt32(aSize[1]), Convert.ToInt32(aSize[0])));

Then you don't reference it again in any of subsequent code and (I assume) save everything in HttpContext.Request.Form.Files instead.然后,您不会在任何后续代码中再次引用它,而是(我假设)将所有内容保存在 HttpContext.Request.Form.Files 中。 Rather than doing this, save the "img" object to the database instead.与其这样做,不如将“img”object 保存到数据库中。 I can't see any code which actually saves anything to the database regardless but I suspect this is the problem based on your description.我看不到任何实际上将任何内容保存到数据库中的代码,但我怀疑这是基于您的描述的问题。 So your code is most likely fine you're just saving the wrong thing.所以你的代码很可能很好,你只是保存了错误的东西。

Try this, you'll need to determine the image format yourself:试试这个,你需要自己确定图像格式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ASP.NET CORE 3 在 Linux (Debian) 上保存图像 - How to save image on Linux (Debian) using ASP.NET CORE 3 ImageSharp.Web - 在 Asp.Net Core 3.1 中通过查询字符串调整图像大小 - ImageSharp.Web - resize image by querystring in Asp.Net Core 3.1 在asp.net中使用javascript在Azure BLOB存储上载期间,如何压缩视频? - How can i compress video during upload on Azure BLOB storage using javascript in asp.net? 使用ASP.NET中的File Uploader将图像上传到Azure Blob? - Uploading Image to Azure Blob using File Uploader in ASP.NET? ASP.NET压缩映像 - ASP.NET compress image 如何使用 ASP.NET Core 将图像保存到数据库中的不同文件夹? - How to save Images to different folders in database using ASP.NET Core? 从数据库中提取数据并使用 ASP.NET Core MVC 将其保存为 Excel 文件 - Extract data from the database and save it as an Excel file using ASP.NET Core MVC ASP.NET Core从Azure和数据库中删除图像Blob - ASP.NET Core delete image blob from azure & from database 如何使用 C# 使用 ASP.NET Core 6 MVC 将 Base64 / byte [] 图像上传到 Azure Blob? - How to Uploading Base64 / byte[] Image to Azure Blob with ASP.NET Core 6 MVC using C#? 使用 HttpClient 压缩对 asp.net core 2 站点的请求的最佳方法是什么? - What is the best way to compress a request to asp.net core 2 site using HttpClient?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM