简体   繁体   English

参数在保存位图时无效

[英]parameter not valid at saving bitmap

I'm resizing an image using the following code: 我正在使用以下代码调整图像的大小:

private void ResizeImage(string path)
    {
        var maxWidth = 700;
        var ms = new MemoryStream(System.IO.File.ReadAllBytes(Server.MapPath(path)));
        var imgToResize = Bitmap.FromStream(ms);
        var sourceWidth = imgToResize.Width;
        var sourceHeight = imgToResize.Height;
        if (sourceWidth > maxWidth)
        {
            var nPercent = ((float)maxWidth / (float)sourceWidth);
            var newWidth = (int)(sourceWidth * nPercent);
            var newHeight = (int)(sourceHeight * nPercent);

            var newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
            using (Graphics graphics = Graphics.FromImage(newImage))
            {
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.DrawImage(imgToResize, 0, 0, newWidth, newHeight);

            }
            imgToResize.Dispose();
            var codecInfo = this.GetEncoderInfo(ImageFormat.Jpeg);

            var encoder = System.Drawing.Imaging.Encoder.Quality;
            var encParams = new EncoderParameters(1);

            var encParam = new EncoderParameter(encoder, 60);
            encParams.Param[0] = encParam;
            newImage.Save(Server.MapPath(path), codecInfo, encParams);
        }
    }
private ImageCodecInfo GetEncoderInfo(ImageFormat format)
    {
        return ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == format.Guid);
    }

at the last line [newImage.Save(...)] it throws an exception saying Parameter is not valid . 在最后一行[newImage.Save(...)]会引发异常,指出Parameter无效 what exactly is wrong here ? 这到底是怎么了?

Your new EncoderParameter(encoder, 60); 您的new EncoderParameter(encoder, 60); does not work properly. 不能正常工作。 There is no signature with a single Int32. 单个Int32没有签名。 Use 60L instead. 改用60L

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM