简体   繁体   English

如何在上传时调整图像大小以减小图像大小?

[英]How do i resize images on Upload to reduce the image size?

I am trying to resize the images before saving them to the server.我试图在将图像保存到服务器之前调整它们的大小。 I want to reduce the image size before it gets to the server.我想在图像到达服务器之前减小图像大小。 Here i am uploading multiple images separated by ";".在这里,我上传了多个以“;”分隔的图像。

public async Task<IActionResult> Create(Venue v){

var files = HttpContext.Request.Form.Files;
 foreach (var Image in files)
     {
       if (Image != null && Image.Length > 0)
          {
            var file = Image;
            var filename = file.FileName;
            var uploads = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/venue");
                    if (file.Length > 0)
                    {
                     using (var fileStream = new FileStream(Path.Combine(uploads,filename),FileMode.Create))
                        {
                            await file.CopyToAsync(fileStream);
                            v.Image = v.Image + ";" + filename;
                        }
                     }
           }
       }

            _context.Venues.Add(v);
            _context.SaveChanges();
            return RedirectToAction(nameof(Index));
        }

You can try.你可以试试。 using System.Drawing;使用 System.Drawing;

    public static Image resizeImage(IFormFile imgToResize, int newWidth)
    {
        Image image = Image.FromStream(imgToResize.OpenReadStream(), true, true);
        Size size = new Size()
        {
            Width = newWidth,
            Height = newWidth //(image.Height * newWidth) / image.Width 
        };
        return new Bitmap(image, size);
    }

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

相关问题 如何缩小图像尺寸 - How can I reduce the size of an image 我无法通过调整大小来减小表单的大小 - I can't reduce the size of the form with resize 减少图像大小(以字节为单位)而无需调整大小,并且在C#中质量下降 - reduce image size in bytes without resize and quality lose in c# 如何从给定的图像路径(实时URL)直接将图像上传到FTP - How do I directly upload the images to FTP from the given image path(Live URL) 如何通过对话框将图像上传到 SQLite 数据库,并将图像输出到 XAML? - How do I upload images to the SQLite database, via a dialog box, and output the image to XAML? 更改表单大小后,如何自动调整控件的大小? - How do I resize the controls automatically when form size is changed? 如何在调整大小时将默认值设置为表单的最小尺寸? - How do I set default to the minimum size my form on resize? 如何根据窗口的大小调整元素的大小 - How do I resize elements based on the size of the window 如何将扫描图像缩小为一致的哈希? - How do I reduce a scanned image to a consistent hash? 如何从硬盘获取图像,调整图像大小并将其添加到列表中 <image> 快速? - How can i get images from the hard disk resize the images and add them to a list<image> fast?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM