0

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));
        }
MADMAX
  • 152
  • 1
  • 3
  • 17
  • Can you give an example of something you tried that didn't work - because obviously you aren't doing any resizing in this code. – see sharper Mar 02 '20 at 05:00
  • Does this answer your question? [ASP.NET Image uploading with Resizing](https://stackoverflow.com/questions/254419/asp-net-image-uploading-with-resizing) – see sharper Mar 02 '20 at 05:02
  • No i removed the code because nothing is working in my case. – MADMAX Mar 02 '20 at 05:04
  • Given that there is an accepted answer for exactly this case, you really need to provide the error or other problem that occurred when you tried to use that solution. – see sharper Mar 02 '20 at 05:06
  • please help me man – MADMAX Mar 02 '20 at 05:20
  • 1
    We can't help you if you fail to provide enough context of what is actually going wrong. – Jeremy Lakeman Mar 02 '20 at 05:25
  • @markharringson the link I gave you actually gives you a fully functional method to resize an image. All you need to do is copy, paste and call it. So it's hard to help you more than that unless you provide the problem that occurs when you try it. – see sharper Mar 02 '20 at 05:27
  • Resizing images is substantially outside of the normal workloads undertaken by a webserver. If this is anything more than a trivial number of image uploads, you'll have to consider moving this workload onto another machine so that it doesn't affect the smooth-running of the website. – spender Mar 02 '20 at 10:00
  • The code in the question will execute on the server, *after* the files have already been received. What kind of problem are you trying to solve here? Reducing the amount of data sent to your server, or reducing the amount of data stored on the server? – Lasse V. Karlsen Mar 02 '20 at 10:03

1 Answers1

1

You can try. using 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);
    }