Reduce image quality by C#

You can reduce the quality of photos to speed up the loading of your site

Reducing the size of the image is important for the speed of the site because the loading speed of the site increases
Today I will teach you how to increase the size of the site

While I was creating this site, I was researching on the same subject when I came across a link that wrote the code.


private void VaryQualityLevel()  
        // Get a bitmap. The using statement ensures objects  
        // are automatically disposed from memory after use.  
        using (Bitmap bmp1 = new Bitmap(@"C:\TestPhoto.jpg"))  
            ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);  
            // Create an Encoder object based on the GUID  
            // for the Quality parameter category.  
            System.Drawing.Imaging.Encoder myEncoder =  
            // Create an EncoderParameters object.  
            // An EncoderParameters object has an array of EncoderParameter  
            // objects. In this case, there is only one  
            // EncoderParameter object in the array.  
            EncoderParameters myEncoderParameters = new EncoderParameters(1);  
            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);  
            myEncoderParameters.Param[0] = myEncoderParameter;  
            bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jpgEncoder, myEncoderParameters);  
            myEncoderParameter = new EncoderParameter(myEncoder, 100L);  
            myEncoderParameters.Param[0] = myEncoderParameter;  
            bmp1.Save(@"C:\TestPhotoQualityHundred.jpg", jpgEncoder, myEncoderParameters);  
            // Save the bitmap as a JPG file with zero quality level compression.  
            myEncoderParameter = new EncoderParameter(myEncoder, 0L);  
            myEncoderParameters.Param[0] = myEncoderParameter;  
            bmp1.Save(@"C:\TestPhotoQualityZero.jpg", jpgEncoder, myEncoderParameters);  


Please use these codes

using System.Drawing;

using System.Drawing.Imaging;

This code has different parts, but I will explain the important part
For example:

This code will take the desired photo from you


using (Bitmap bmp1 = new Bitmap(@"C:\TestPhoto.jpg"))


And the important part of this part is creating the volume of the new photo


EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);  
            myEncoderParameters.Param[0] = myEncoderParameter;  
            bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jpgEncoder, myEncoderParameters);

That number at the end of the first line adjusts the quality and can be adjusted from 0 to 100 (50L)


Well if you use this code in Asp.Net MVC it will probably give an error

And now my suggestion

My suggestion is to change this code a bit

var GuidRan = Guid.NewGuid();

var Format = Path.GetExtension(imgUp.FileName);

    page.ImageName = GuidRan + Path.GetExtension(imgUp.FileName);

    imgUp.SaveAs(Server.MapPath("/PageImages/" + page.ImageName));

FileInfo imgInfo = new FileInfo(Server.MapPath("/PageImages/" + page.ImageName));

    if (imgInfo.Length > 1000000)


        using (Bitmap bmp1 = new Bitmap(Server.MapPath("/PageImages/" + page.ImageName)))


System.Drawing.Imaging.ImageCodecInfo GetEncoder(System.Drawing.Imaging.ImageFormat format)


                var codecs = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders();

                foreach (var codec in codecs)


       if (codec.FormatID == format.Guid)


                        return codec;



                return null;


var jpgEncoder = GetEncoder(System.Drawing.Imaging.ImageFormat.Jpeg);

System.Drawing.Imaging.Encoder myEncoder =


EncoderParameters myEncoderParameters = new EncoderParameters(1);

EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);

myEncoderParameters.Param[0] = myEncoderParameter;

bmp1.Save(Server.MapPath(@"/PageImages/" + GuidRan), jpgEncoder, myEncoderParameters);


        System.IO.File.Delete(Server.MapPath("/PageImages/" + page.ImageName));

        System.IO.File.Copy(Server.MapPath("/PageImages/") + GuidRan,

            Server.MapPath("/PageImages/") + GuidRan + Format, true);

        System.IO.File.Delete(Server.MapPath("/PageImages/") + GuidRan);


Use this here as well

using System.IO;

This is the first code that has changed a bit

Well, let's go to the changes that have been made to explain to you

Here I used Guid to make the photos look the same and I used a variable called GuidRan

Then I had to get an image format and keep it in a variable with an error

var Format = Path.GetExtension(imgUp.FileName);

Then I save the photo

And in the next line, I give the address of the saved file to the volume reduction code

Inside the section, you will notice another change that is in the addressing location


This goes to the project location


While testing the project, I noticed that if the image is below 1 MG, the image looks like the image below

This photo is a total of 261KB and after reducing the size of the photo, it looks like this

The rest of the parts are the same as before, except for the last code you remember that I pointed to an error that pointed to the format. Well, I solved this error with a little creativity.

Here we have the steps of taking photos and saving ...
To tell you dear ones with a number


  • Taking a photo of the user
  • Save the second photo

The file extension is not entered when saving the second image

After the System.IO we get help

First we delete the photo we took from the user (quality photo)

After the photo, copy the extension and convert it to a photo with the extension

Then we delete the photo without the extension

Just as easily

Friends, I hope you enjoyed this article

Please support us to remove the word Beta from the name of our site

Thanks to MicrosoftšŸ™



reducing image size Reduce image size with C# Small size photo Increase site load Photo size Photo size control Photo size control with C# C# Asp.Net Mvc
You must be logged in to post a comment

This post is one of the best posts on the site, thank you Admin