Click here to Skip to main content
14,382,664 members

Reduce Image File Size to 150 KB

Bala4 . V asked:

Open original thread
Hi friends

I am developing a Image resize project
my client wants the image size should be in 240*320 and size should be 50-150 kb

but the size is not in control the size exist more than 150 kb for the image more than 1 MB

my code is:
protected void uploadButton_Click(object sender, EventArgs e)
        Bitmap bmp1 = new Bitmap(fileUpload.FileContent);
        ImageCodecInfo jgpEncoder = 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", jgpEncoder, myEncoderParameters);

        //myEncoderParameter = new EncoderParameter(myEncoder, 100L);
        //myEncoderParameters.Param[0] = myEncoderParameter;
        //bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters);

        // Save the bitmap as a JPG file with zero quality level compression.
        EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 25L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        bmp1.Save(@"c:\TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters);

        if (fileUpload.HasFile)
            // Find the fileUpload control
            string filename = fileUpload.FileName;

            // Check if the directory we want the image uploaded to actually exists or not
            if (!Directory.Exists(MapPath(@"Uploaded-Files")))
                // If it doesn't then we just create it before going any further

            // Specify the upload directory
            string directory = Server.MapPath(@"Uploaded-Files\");

            // Create a bitmap of the content of the fileUpload control in memory
            Bitmap originalBMP = new Bitmap(@"c:\TestPhotoQualityZero.jpg");

            // Calculate the new image dimensions
            int origWidth = originalBMP.Width;
            int origHeight = originalBMP.Height;
            int sngRatio = origWidth / origHeight;
            int newWidth = 240;
            //int newHeight = newWidth / sngRatio;
            int newHeight = 320;

            // Create a new bitmap which will hold the previous resized bitmap
            Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
            // Create a graphic based on the new bitmap
            Graphics oGraphics = Graphics.FromImage(newBMP);

            // Set the properties for the new graphic file
            oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            // Draw the new graphic based on the resized bitmap
            oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);

            // Save the new graphic file to the server
            newBMP.Save(directory + "tn_" + filename);

            // Once finished with the bitmap objects, we deallocate them.

            // Write a message to inform the user all is OK
            label.Text = "File Name: ">" + filename + "<br>";
            label.Text += "Content Type: ">" + fileUpload.PostedFile.ContentType + "<br>";
            label.Text += "File Size: ">" + fileUpload.PostedFile.ContentLength.ToString() + "";
            // Display the image to the user
            Image1.Visible = true;
            Image1.ImageUrl = @"/Uploaded-Files/tn_" + filename;
            label.Text = "No file uploaded!";

    private ImageCodecInfo GetEncoder(ImageFormat format)

        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

        foreach (ImageCodecInfo codec in codecs)
            if (codec.FormatID == format.Guid)
                return codec;
        return null;
Tags: C#, ASP.NET


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100