How to compress image size while saving to folder c sharp asp net

Introduction

When working with ASP.NET, it is common to encounter scenarios where you need to save images to a . However, saving images in their original size can lead to large file sizes, which can impact the performance of your application. In this article, we will explore how to compress size while saving to a folder in C# ASP.NET.

Using the System. namespace

The System.Drawing namespace in C# provides various and methods for working with images. One of the classes that we can utilize for image compression is the Image class.

Let's consider an example where we have an image file that to be to a folder. We can use the code snippet to compress the image size saving it:


using System.Drawing;
using System.Drawing.Imaging;

 void SaveCompressedImage(string imagePath, string savePath, int compressionLevel)
{
    using (Image image = Image.FromFile(imagePath))
    {
        EncoderParameters encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, compressionLevel);

        ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

        image.Save(savePath, jpegCodec, encoderParameters);
    }
}

private ImageCodecInfo GetEncoderInfo(string mimeType)
{
    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

    foreach (ImageCodecInfo codec in codecs)
    {
        if (codec.MimeType == mimeType)
        {
            return codec;
        }
    }

    return null;
}

In the above code, we define a SaveCompressedImage that takes the path of the original image, the path where the compressed image should be saved, and the compression level as parameters. The compression level is an integer value ranging from 0 to 100, where 0 represents the maximum compression and 100 represents the best quality.

We use the Image.FromFile method to load the original image into an Image object. Then, we create an instance of the EncoderParameters class and set the compression level using the EncoderParameter class. Next, we obtain the ImageCodecInfo for the JPEG format using the GetEncoderInfo method.

Finally, we save the compressed image to the specified path using the Save method of the Image class, passing the ImageCodecInfo and EncoderParameters as parameters.

Example Usage

Let's see an example of how to use the SaveCompressedImage method:


string imagePath = "path/to/original/image.jpg";
string savePath = "path/to/save/compressed/image.jpg";
int compressionLevel = 50;

SaveCompressedImage(imagePath, savePath, compressionLevel);

In the above example, we specify the path of the original image, the path where the compressed image should be saved, and the compression level of 50. The SaveCompressedImage method will compress the image and save it to the specified path.

Conclusion

Compressing image size while saving to a folder in C# ASP.NET can help optimize the performance of your application by reducing file sizes. By utilizing the System.Drawing namespace and the Image class, you can easily implement image compression functionality in your ASP.NET projects.

Remember to adjust the compression level based on your specific requirements, balancing between file size and image quality. Experiment with different compression levels to find the optimal balance for your application.

Rate this post

Leave a Reply

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

Table of Contents