How to resize image after being uploaded in asp net core 2 0

Resizing Images in ASP.NET Core 2.0

One requirement in web is the ability to resize images after they have been uploaded. This can be useful for optimizing the size of images and improving the performance of your website. In this article, we will explore how to resize images in ASP.NET Core 2.0 using the System.Drawing namespace.

Step 1: Handling the Image Upload

Before we can resize the image, we need to the image upload. In ASP.NET Core 2.0, you can use the IFormFile interface to handle file uploads. Here's an example of how you can handle the image upload:


[]
public async Task UploadImage(IFormFile file)
{
    if (file == null || file.Length == 0)
    {
         BadRequest("No file uploaded.");
    }

    // Save the uploaded file to a temporary location
    var filePath = Path.GetTempFileName();
    using (var stream = new FileStream(filePath, FileMode.Create))
    {
        await file.CopyToAsync(stream);
    }

    // Resize the image
    // TODO: Implement image resizing 

    // Return a success 
    return Ok("Image uploaded successfully.");
}

In the above example, we first check if a file was uploaded. If not, we return a bad request response. Next, we save the uploaded file to a temporary location using the FileStream class. Now, let's move on to the image resizing logic.

Step 2: Resizing the Image

To resize the image, we will use the System.Drawing namespace, which provides a set of classes for image manipulation. Here's an example of how you can resize the image:


using System.Drawing;

// ...

public void ResizeImage(string imagePath, int width, int height)
{
    using (var image = Image.FromFile(imagePath))
    {
        var resizedImage = new Bitmap(width, height);
        using (var graphics = Graphics.FromImage(resizedImage))
        {
            graphics.DrawImage(image, 0, 0, width, height);
        }

        // Save the resized image
        resizedImage.Save(imagePath);
    }
}

In the above example, we first load the original image using the Image.FromFile method. Then, we create a new Bitmap object with the desired width and height. Next, we use the Graphics.DrawImage method to draw the original image onto the resized image. Finally, we save the resized image using the Save method.

Step 3: the ResizeImage Method

Now that we have the image resizing logic in , we can call the ResizeImage method from our UploadImage . Here's an example of how you can call the ResizeImage method:


[HttpPost]
public async Task UploadImage(IFormFile file)
{
    // ...

    // Resize the image
    var resizedImagePath = Path.Combine(Path.GetTempPath(), "resized_image.jpg");
    ResizeImage(filePath, 800, 600);

    // Return a success response
    return Ok("Image uploaded and resized successfully.");
}

In the above example, we first define the path for the resized image. Then, we call the ResizeImage method with the path of the original image and the desired width and height. Finally, we return a success response indicating that the image has been uploaded and resized successfully.

Conclusion

In this article, we have explored how to resize images in ASP.NET Core 2.0 using the System.Drawing namespace. By following the steps outlined in this article, you can easily handle image uploads and resize them to optimize the performance of your website.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents