Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with images: Parameter is not valid

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.

like image 890
Marco Avatar asked Sep 14 '12 10:09

Marco


2 Answers

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();
like image 50
Aristos Avatar answered Nov 15 '22 22:11

Aristos


In my case, I was accidentally calling Dispose before Save which was resulting in the same "The parameter is not valid" error

Hope this helps!

like image 35
Branden Barber Avatar answered Nov 15 '22 21:11

Branden Barber