title: Invalid Parameter Error when calling System.Drawing.Image.Save
body: <p>i am getting Invalid Parameter Error when calling System.Drawing.Image.Save function.
  i google and found a few suggestions but nothing works.
  what i am trying to do is that, when i upload an image and if it's lager than 100kb i would like to reduce the image size to half. please help.</p>
  
  <pre><code>     System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(realpath);
  
  
                          FullsizeImage = System.Drawing.Image.FromFile(realpath);                    
                          int fileSize = (int)new System.IO.FileInfo(realpath).Length;
                          while (fileSize &gt; 100000) //If Larger than 100KB
                          {
                              SaveJpeg(realpath, FullsizeImage);
                              fileSize = (int)new System.IO.FileInfo(realpath).Length;
                          }
  
  
  
   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 &lt; codecs.Length; i++)
              if (codecs[i].MimeType == mimeType)
                  return codecs[i];
          return null;
      }
  
      public static void SaveJpeg(string path, Image img)
          {
  
  
              Image NewImage = img;
              img.Dispose();
  
              EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, 85L);
              EncoderParameters encoderParams = new EncoderParameters(1);
              encoderParams.Param[0] = qualityParam;
  
              ImageCodecInfo jpegCodec = GetEncoderInfo(GetMimeType(path.Substring(path.LastIndexOf('.'), path.Length - path.LastIndexOf('.'))));
  //THE ERROR IS HERE!!!!!!
              NewImage.Save(path, jpegCodec, encoderParams); 
  //THE ERROR IS HERE!!!!!!
  
          }
  
  
  
  
      public static string GetMimeType(string extension)
          {
              if (extension == null)
              {
                  throw new ArgumentNullException("extension");
              }
  
              if (!extension.StartsWith("."))
              {
                  extension = "." + extension;
              }
  
              switch (extension.ToLower())
              {
                  #region Big freaking list of mime types
                  // combination of values from Windows 7 Registry and 
                  // from C:\Windows\System32\inetsrv\config\applicationHost.config
                  // some added, including .7z and .dat
  
                  case ".323": return "text/h323";
  
   // more extension here..
  
                  #endregion
  
                  default:
                      // if you have logging, log: "No mime type is registered for extension: " + extension);
                      return "application/octet-stream";
              }
          }
  </code></pre>
  
  <p>EDIT : I modified the code as below, now the image is saving without any exception! Thanks! but another problem here. the file size is not getting reduced. which mean my while loop can never exit. please help and thanks again.</p>
  
  <pre><code>using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(realpath)))
                      {
                          using (Image FullsizeImage = Image.FromStream(ms))
                          {
                              //code here                           
                              int fileSize = (int)new System.IO.FileInfo(realpath).Length;
                              while (fileSize &gt; 100000) //If Larger than 100KB
                              {
                                  SaveJpeg(realpath, FullsizeImage, 85L);
                                  fileSize = (int)new System.IO.FileInfo(realpath).Length;
                              }
  
                          }
                      }
  </code></pre>
  
  <p>Can someone help me please, my problem is not solved yet :(</p>