内容简介:翻译自:https://stackoverflow.com/questions/12422514/working-with-images-parameter-is-not-valid
一切都始于 a very usefull 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的后续跟踪对于解决这个问题非常重要,因为它解决了我在保存文件时的糟糕错误.
我建议保存图像时更正确的是
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.使用(){}需要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();
翻译自:https://stackoverflow.com/questions/12422514/working-with-images-parameter-is-not-valid
以上所述就是小编给大家介绍的《c# – 使用图像:参数无效》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 谷歌开源新模型 EfficientNet:图像识别效率提升 10 倍,参数减少 88%
- TypeScript 中的命名参数、可选参数、默认参数
- PXC状态参数与变量参数
- Opencv图像处理系列(六)—— 图像梯度
- opencv笔记(2):图像剪切和图像移位
- Opencv图像处理系列(九)—— 图像轮廓
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。