使用图像:参数无效

时间:2012-09-14 10:16:42

标签: c# asp.net image image-processing

一切都始于a very useful piece of code, that I found here on Stackoverflow

然后,我决定进行自己的调整,并为此方法添加图像大小调整。 但是,在遇到这个问题后,我现在收到的信息是:“参数无效”

我还想强调一点,尽管有错误,图片仍然是成功上传的。但是,它们没有按预期进行优化。


这是我的“上传按钮”中代码的一部分:

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

这是我的SaveJpeg方法的完整代码:

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

跟进

似乎代码有几个错误。在编码中是一个,在图像保存中是另一个。

Aristos的跟进对于解决这个问题非常重要,因为它解决了我在保存文件时的糟糕错误。

3 个答案:

答案 0 :(得分:7)

我建议保存图像时更正确

ImageCodecInfo myImageCodecInfo = FindJpegEncoder();

EncoderParameters encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, cQuality);

imgFinal.Save(TheFileNameTosaveIt, myImageCodecInfo, encoderParameters);

这是从系统中找到编码器的功能

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

其中long cQuality = 65L并且确定很长,我认为实际上只认为必须改变,函数调用中的int为long 。也最好使用using(){}与需要dispose()

的函数进行扭曲

跟进

您尝试保存的NewImage上有一个错误,您没有从之前制作的实际图形中获取错误,这就是为什么没有任何改变。您的实际代码没有保存创建图像,但您创建了一个新代码,因此此代码

System.Drawing.Image imgFinal = (System.Drawing.Image)newImage;
newImage.Dispose();

imgFinal.Save(path, jpegCodec, encoderParams);
imgFinal.Dispose();

必须是

newImage.Save(path, jpegCodec, encoderParams);
newImage.Dispose();

答案 1 :(得分:5)

就我而言,我在Dispose之前不小心打电话给Save,这导致了同样的问题 “参数无效”错误

希望这有帮助!

答案 2 :(得分:5)

我能够通过指定Quality的数据类型来解决此问题。 它必须是“长”的,因此这解决了我的问题。

[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))

没有“长”,错误是:

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