4

I need to change the size of the photo and save it to a new size when the user selects a photo for the upload, before saving the photo.

i using this code but it not save with new size . whats the problem ?

public async Task<IActionResult> UploadNewsPic()
    {
        var file = Request.Form.Files[0];
        try
        {
            if (file.Length > 0)
            {
                string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                string fullPath = Path.Combine(_applicationRoot.UploadNewPath(), file.Name);
                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    if (file.IsImage())
                    {
                        await file.ResizeImage(3, 3);
                        file.CopyTo(stream);
                    }
                }
            }
            return Ok();
        }
        catch (Exception e)
        {
            return BadRequest();
        }
    }

and this is Resize Extention :

 public async static Task<Image> ResizeImage(this IFormFile file, int width, int height)
    {
        using (var memoryStream = new MemoryStream())
        {
            await file.CopyToAsync(memoryStream);
            using (var img = Image.FromStream(memoryStream))
            {
                return img.Resize(width, height);
            }
        }
    }

    public static Image Resize(this Image image, int width, int height)
    {
        var res = new Bitmap(width, height);
        using (var graphic = Graphics.FromImage(res))
        {
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphic.SmoothingMode = SmoothingMode.HighQuality;
            graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphic.CompositingQuality = CompositingQuality.HighQuality;
            graphic.DrawImage(image, 0, 0, width, height);
        }
        return res;
    }
2
  • "but it not save with new size" this information is not really helpful., i.e. did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing. Commented May 16, 2019 at 7:32
  • Please don’t tag technologies that don’t relate. I don’t see anything relating to EF here. You should tag ASP.NET Core though. Also using the graphics APIs you use on a server system isn’t really supported, even if it works (if I’m not entirely mistaken). There are libraries like ImageSharp which work Commented May 16, 2019 at 7:42

2 Answers 2

4

You need to save the result from ResizeImage to the stream. Right now you are just copying the original file.

var img = await file.ResizeImage(3, 3);
img.Save(stream, SomeFormat);
Sign up to request clarification or add additional context in comments.

1 Comment

By the way, you don't need to copy the FormFile to a memory stream in ResizeImage, just use OpenReadStream instead. Image.FromStream(file.OpenReadStream())
2

Your ResizeImage() function return a resized image, it doesn't edit the image itself, so you must set it to a variable.

if (file.IsImage())
{
    Image imageResized = await file.ResizeImage(3, 3);
    // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.