7

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Marco
  • 1,272
  • 6
  • 22
  • 33
  • 3
    This other question might be related to : http://stackoverflow.com/questions/384593/parameter-is-not-valid-when-using-saving-bitmap – Larry Sep 14 '12 at 10:58
  • @Laurent Thanks. I'm still trying to solve this, but now I can count with the help of your link. – Marco Sep 14 '12 at 11:48

4 Answers4

9

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();
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • Thanks for your post. However, after using your suggestion, I still haven't done any progression. The image is still being uploaded (but not resized or its quality being reduced). So basically, after applying your changes, I'm still in the same spot. :( – Marco Sep 14 '12 at 11:47
  • @Marco You say that got an error, not that the image is show with out applied any change. – Aristos Sep 14 '12 at 11:48
  • Maybe I haven't explained myself correctly. Sorry. What I meant is: "The parameter is not valid" information is still being shown as described in the original post. And as you can read on the original post: " despite the error, the images are being sucessfully uploaded. However, they are not being optmized as intended.". – Marco Sep 14 '12 at 11:50
5

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!

Branden Barber
  • 1,717
  • 1
  • 17
  • 15
5

I was able to fix this issue by specifying the datatype of the Quality. It must be a "long", therefore this solved my problem.

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$bmp = New-Object System.Drawing.Bitmap $imagePath

#Encoder parameter for image quality 
$myEncoder = [System.Drawing.Imaging.Encoder]::Quality
$encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1) 
$encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($myEncoder, [long]$quality)

# get codec
$myImageCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders()|where {$_.MimeType -eq 'image/jpeg'}

#save to file
$bmp.Save($imageOutPut,$myImageCodecInfo, $($encoderParams))

Without "long", the error was:

Exception calling "Save" with "3" argument(s): "Parameter is not valid."
At C:\Projects\Image_Comparison\ImageComparison.ps1:81 char:49
+     $bmp.Save($imageOutPut,$myImageCodecInfo, $($encoderParams))
+                                                 ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException
Thomas Wolf
  • 51
  • 1
  • 1
0

In my case, similar to the example, the problem was that quality variable was an int but need to be a long. That's the reason why it returns "Parameter is not valid".

Gustavo Cantero
  • 440
  • 6
  • 9