C# “参数无效。” 使用保存位图时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/384593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Parameter is not valid." when using saving bitmap
提问by CruelIO
Im trying to save a bitmap jpg format with a specified encoding quality. However im getting an exception ("Parameter is not valid.") when calling the save method.
我试图用指定的编码质量保存位图 jpg 格式。但是我在调用 save 方法时遇到异常(“参数无效。”)。
If i leave out the two last parameters in the bmp.save it works fine.
如果我在 bmp.save 中省略了最后两个参数,它工作正常。
EncoderParameters eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 16);
ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
string outfile = outputpath + "\" + fileaddition + sourcefile.Name;
bmp.Save(outfile,ici,eps );
bmp.Dispose();
image.Dispose();
return true;
}
ImageCodecInfo GetEncoderInfo(string mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
}
Thank you
谢谢
采纳答案by Hans Passant
GDI+ is pretty flaky. You'll need to use 16L for the value or cast to (long).
GDI+ 非常脆弱。您需要使用 16L 作为值或转换为(长)。
回答by Mahdi
You should cast quality value to long, like this:
您应该将质量值转换为 long,如下所示:
eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)16);