[英]Error on adjusting jpeg quality: Value cannot be null.Parameter name: structure
I have a method where I take an image, make a copy for resizing, and then return it as a byte array. 我有一个方法来拍摄图像,制作副本以调整大小,然后将其作为字节数组返回。 I have attempted to add the ability to adjust the quality of the image per this doc:
我尝试添加根据此文档调整图像质量的功能:
http://msdn.microsoft.com/en-us/library/bb882583(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/bb882583(v=vs.110).aspx
Image image = Image.FromFile(_filepath);
Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
}
MemoryStream memStream = new MemoryStream();
EncoderParameters myEncoderParameters = new EncoderParameters(1);
ImageCodecInfo encoder = ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.MimeType == contentType);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
newImage.Save(memStream, encoder, myEncoderParameters);
return memStream.ToArray();
When I call newImage.Save()
, I get this error: 当我调用
newImage.Save()
,出现以下错误:
An exception of type 'System.ArgumentNullException' occurred in System.Drawing.dll but was not handled in user code
System.Drawing.dll中发生类型为'System.ArgumentNullException'的异常,但未在用户代码中处理
Message is: 消息是:
Value cannot be null.
值不能为空。 Parameter name: structure
参数名称:结构
MSDN says that you have a ArgumentNullException when calling this Save() overload only if the stream is NULL . MSDN说,仅当流为NULL时,才在调用此Save()重载时具有ArgumentNullException。
Are you sure that no other (omitted?) code is not nullifying this stream? 您确定没有其他(省略的)代码没有使该流无效吗? Are you sure this is the site of the exception?
您确定这是例外的地点吗?
Forgot a step. 忘了一步。 I created the
EncoderParameter
object, but didn't include it in the EncoderParameters
collection. 我创建了
EncoderParameter
对象,但未将其包含在EncoderParameters
集合中。 The message confused me a bit. 消息使我有些困惑。
All resolved once I added: 我添加后,所有问题都解决了:
myEncoderParameters.Param[0] = myEncoderParameter;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.