调用system.drawing.image.save函数时出现无效参数错误。
我搜索了一下,发现了一些建议,但没有任何效果。
我想做的是,当我上传一个图片时,如果大于100kb,我想把图片大小缩小到一半。请帮忙。

     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 > 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 < 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";
            }
        }

编辑:我修改了代码如下,现在图像保存没有任何异常!谢谢!但这里还有一个问题。文件大小没有减小。这意味着我的while循环永远无法退出。请帮忙,再次感谢。
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 > 100000) //If Larger than 100KB
                            {
                                SaveJpeg(realpath, FullsizeImage, 85L);
                                fileSize = (int)new System.IO.FileInfo(realpath).Length;
                            }

                        }
                    }

有人能帮帮我吗?我的问题还没解决。(

最佳答案

因为你正在处理一个image对象。

public static void SaveJpeg(string path, Image img)
 {
  Image NewImage = img;
  img.Dispose(); <------- Here
  ...
 }

编辑:methodImage.FromFile文件打开一个流,在方法未终止之前,该文件不会关闭。尝试使用内存流。
using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(realPath)))
  {
   using (Image img = Image.FromStream(ms))
    {
     ImageCodecInfo myImageCodecInfo;
     Encoder myEncoder;
     EncoderParameter myEncoderParameter;
     EncoderParameters myEncoderParameters;


     myImageCodecInfo = GetEncoderInfo("image/jpeg");
     myEncoder = Encoder.Quality;

     myEncoderParameters = new EncoderParameters(1);

     myEncoderParameter = new EncoderParameter(myEncoder, 85L);
     myEncoderParameters.Param[0] = myEncoderParameter;
     img.Save(realPath, myImageCodecInfo, myEncoderParameters);
     }
  }

09-26 14:36