Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8525749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:04:29+00:00 2026-06-11T08:04:29+00:00

It all started with a very useful piece of code, that I found here

  • 0

It all started with a very useful piece of code, that I found here on Stackoverflow.

Then, I decided to make my own tweaks, and add image resizing to this method.
However, after struggling with this problem I am now being presented with the information of: “The parameter is not valid”.

I would also like to highlight that, despite the error, the images are being sucessfully uploaded. However, they are not being optmized as intended.


This is the part of the code in my “upload button”:

fuOne.SaveAs(Server.MapPath("~/imgFolder/temp/") + fuOne.FileName);

System.Drawing.Image imgUploaded = System.Drawing.Image.FromFile(Server.MapPath("~/imgFolder/temp/") + fuOne.FileName);

SaveJpeg(Server.MapPath("~/imgFolder/temp/") + fuOne.FileName, imgUploaded, 60, 300, 300);

This is the full code of my SaveJpeg method:

public static void SaveJpeg(string path, System.Drawing.Image imgUploaded, int quality, int maxWidth, int maxHeight)
    {
        if (quality < 0 || quality > 100)
            throw new ArgumentOutOfRangeException("quality must be between 0 and 100.");

        // resize the image
        int newWidth = imgUploaded.Width;
        int newHeight = imgUploaded.Height;
        double aspectRatio = (double)imgUploaded.Width / (double)imgUploaded.Height;

        if (aspectRatio <= 1 && imgUploaded.Width > maxWidth)
        {
            newWidth = maxWidth;
            newHeight = (int)Math.Round(newWidth / aspectRatio);
        }
        else if (aspectRatio > 1 && imgUploaded.Height > maxHeight)
        {
            newHeight = maxHeight;
            newWidth = (int)Math.Round(newHeight * aspectRatio);
        }


        Bitmap newImage = new Bitmap(imgUploaded, newWidth, newHeight);

        Graphics g = Graphics.FromImage(imgUploaded);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
        g.DrawImage(imgUploaded, 0, 0, newImage.Width, newImage.Height);

        g.Dispose();
        imgUploaded.Dispose();

        // Lets start to change the image quality
        EncoderParameter qualityParam =
            new EncoderParameter(Encoder.Quality, quality);
        // Jpeg image codec 
        ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;

        System.Drawing.Image imgFinal = (System.Drawing.Image)newImage;
        newImage.Dispose();

        imgFinal.Save(path, jpegCodec, encoderParams);
        imgFinal.Dispose();
    }

    /// <summary> 
    /// Returns the image codec with the given mime type 
    /// </summary> 
    private static ImageCodecInfo GetEncoderInfo(string mimeType)
    {
        // Get image codecs for all image formats 
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

        // Find the correct image codec 
        for (int i = 0; i < codecs.Length; i++)
            if (codecs[i].MimeType == mimeType)
                return codecs[i];
        return null;
    } 

Follow up

It seems that the code had a couple of errors. Being one in the encoding and another in the image saving.

The follow up of Aristos is very important to solving this problem, because it fixes my lousy mistake when saving the file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:04:30+00:00Added an answer on June 11, 2026 at 8:04 am

    What I suggest that is more correct when you save the image is

    ImageCodecInfo myImageCodecInfo = FindJpegEncoder();
    
    EncoderParameters encoderParameters = new EncoderParameters(1);
    encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, cQuality);
    
    imgFinal.Save(TheFileNameTosaveIt, myImageCodecInfo, encoderParameters);
    

    and this the function to find the Encoder from the system

    internal static ImageCodecInfo FindJpegEncoder()
    {
        // find jpeg encode text
        foreach (ImageCodecInfo info in ImageCodecInfo.GetImageEncoders())
        {
            if (info.FormatID.Equals(ImageFormat.Jpeg.Guid))
            {
                return info;
            }
        }
    
        Debug.Fail("Fail to find jPeg Encoder!");
        return null;
    }
    

    where the long cQuality = 65L and be sure that is long, and I think that actually only thinks must change, the int to long on the function call. Also is better to warp with using(){} the functions that need dispose()

    Follow up

    You have a bug on the NewImage that you try to save, you do not get it from the actually graphics that you made before, that why nothing is change. The actually code of you did not save the create image but you make a new one, so this code

    System.Drawing.Image imgFinal = (System.Drawing.Image)newImage;
    newImage.Dispose();
    
    imgFinal.Save(path, jpegCodec, encoderParams);
    imgFinal.Dispose();
    

    must be

    newImage.Save(path, jpegCodec, encoderParams);
    newImage.Dispose();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been reading StackOverflow too much and started doubting all the code I've ever
EDIT I've just started skimming Codd's famous 1970 paper that started it all, that
I just started using Qt and noticed that all the example class definitions have
I'm very new to all this (started this week), so be kind :) I
I'm very new to web-development (I feel like all my posts lately have started
Is it possible to stop all started services when the user hits the Home
On my new Dell XPS laptop, I've just started loading all of my goodies
I started using NetBeans today, and it was all going swimmingly, until I came
when started, jetty per default loads all directories and war-files in its webapps directory,
I just started to use doxygen and may not be familiar with all available

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.