图像在asp核心中调整大小

时间:2016-12-22 04:08:57

标签: asp.net-core image-uploading image-resizing

我们在上传时需要大家的帮助来调整图片大小

 try
                {
                    var uploads = Path.Combine(_environment.WebRootPath, "images");
                    if (files != null)
                    {
                        string ext = Path.GetExtension(files.FileName);
                        string folderImageName = "menu_" + menu.menuId + ext;
                        using (var fileStream = new FileStream(Path.Combine(uploads, folderImageName), FileMode.Create))
                        {                           
                            await files.CopyToAsync(fileStream);
                        }
                        menu.image = folderImageName;
                    }
                    _context.Update(menu);
                    await _context.SaveChangesAsync();
                    return RedirectToAction("MenuItemManagement", new { msg = "editSuccess" });
                }

这些代码用于上传图片

1 个答案:

答案 0 :(得分:2)

我建议在.Net Framework上使用.Net Core或ImageProcessor(https://github.com/JimBobSquarePants/ImageSharp)/ ImageResizer(http://imageprocessor.org/)上的ImageSharp(https://imageresizing.net/

简单的使用ImageSharp

using (FileStream stream = File.OpenRead("oryginal.jpg"))
using (FileStream output = File.OpenWrite("resized.jpg"))
{
    Image image = new Image(stream);
    image.Resize(yourWidth, yourHeight)
         .Save(output);
}