调用System.Drawing.Image.Save时参数错误无效

时间:2012-09-03 10:11:04

标签: c# reduce image-size

调用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;
                            }

                        }
                    }

有人可以帮助我,我的问题还没有解决:(

1 个答案:

答案 0 :(得分:6)

因为您正在处置image对象。

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

编辑:方法Image.FromFile文件打开一个流,该文件将不会关闭,直到您的方法未终止。尝试使用MemoryStream。

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