简体   繁体   English

调整JPEG品质时出错:值不能为空参数名称:结构

[英]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.

相关问题 值不能为null。参数名称:读取嵌入文件内容时出现流错误 - Value cannot be null.Parameter name: stream error while reading the contents of embeddded file 无法使用 ASP.NET Identity 登录,值不能为空。参数名称:manager - Can not sign in using ASP.NET Identity, Value cannot be null.Parameter name: manager 值不能为null参数名称:在ASP.NET中进行数据绑定时的键 - Value cannot be null.Parameter name: key when databind in ASP.NET “row”参数不能为空。参数名称:row - 'row' argument cannot be null.Parameter name: row PDF Checkbox Error:值不能为null。 参数名称:值 - PDF Checkbox Error : Value cannot be null. Parameter name: value WPF 设计器错误值不能为空。 参数名称:模式 - WPF designer error Value cannot be null. Parameter name: pattern Watin错误:值不能为null。 参数名称:domContainer - Watin error: Value cannot be null. Parameter name: domContainer 收到错误值不能为null或为空。 参数名称:URL - Getting an error Value cannot be null or empty. Parameter name: URL 反序列化错误:值不能为null。 参数名称:类型 - Deserialization error: value cannot be null. Parameter name: type 值不能为空。 参数名称:extent - Value cannot be null. Parameter name: extent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM