简体   繁体   English

System.OverflowException:'算术运算导致溢出。'

[英]System.OverflowException: 'Arithmetic operation resulted in an overflow.'

I created .NET Core console app and added reference to System.Drawing.Common nuget. 我创建了.NET Core控制台应用程序,并添加了对System.Drawing.Common nuget的引用。 I'm trying to run the following code from ms docs , but it fails on bitmap1.GetEncoderParameterList with System.OverflowException : 我正在尝试从ms docs运行以下代码,但在带有System.OverflowException bitmap1.GetEncoderParameterList上失败:

'Arithmetic operation resulted in an overflow.' 算术运算导致溢出。

What is wrong with this code? 此代码有什么问题? Do I make something illegal? 我会违法吗?

class Program
{

   public static void Main(string[] args)
   {
      new Program().GetSupportedParameters();
   }
   private void GetSupportedParameters()
   {
       Bitmap bitmap1 = new Bitmap(1, 1);
       ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
       //this one also does not work:
       //Bitmap bitmap1 = new Bitmap(Image.FromFile(@"C:\temp\0\2.png"));
       //ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Png);
       EncoderParameters paramList = bitmap1.GetEncoderParameterList(jpgEncoder.Clsid);
       EncoderParameter[] encParams = paramList.Param;
       StringBuilder paramInfo = new StringBuilder();

       for (int i = 0; i < encParams.Length; i++)
       {
           paramInfo.Append("Param " + i + " holds " + encParams[i].NumberOfValues +
                    " items of type " +
                encParams[i].ValueType + "\r\n" + "Guid category: " + encParams[i].Encoder.Guid + "\r\n");

       }            
   }

   private ImageCodecInfo GetEncoder(ImageFormat format)
   {
       ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

       foreach (ImageCodecInfo codec in codecs)
       {
          if (codec.FormatID == format.Guid)
          {
               return codec;
          }
       }
       return null;
    }
 }

StackTrace: 堆栈跟踪:

at System.Drawing.Imaging.EncoderParameters.ConvertFromMemory(IntPtr memory)
at System.Drawing.Image.GetEncoderParameterList(Guid encoder)
at ConsoleApp16.Program.GetSupportedParameters() in Program.cs:line 19
at ConsoleApp16.Program.Main(String[] args)
in Program.cs:line 13

UPDATE This one also crashes: 更新这也崩溃了:

Bitmap bitmap1 = new Bitmap(Image.FromFile(@"C:\temp\0\2.png"));
ImageCodecInfo pngEncoder = GetEncoder(ImageFormat.Png);

EDIT: Seems like it's working fine on .NET Framework and it throws an exception as OP with .NET Core 2.1. 编辑:似乎它在.NET Framework上工作正常,并且在.NET Core 2.1中作为OP引发异常。

EDIT2: Seems like it's a known issue, listed here: https://github.com/dotnet/docs/issues/5607 EDIT2:似乎是一个已知问题,在此处列出: https : //github.com/dotnet/docs/issues/5607

这是.NET Core中的错误,并且已针对.NET Core 5.0解决:请参见https://github.com/dotnet/corefx/pull/40181

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

相关问题 NUnit System.OverflowException:算术运算导致溢出 - NUnit System.OverflowException: Arithmetic operation resulted in an overflow C#肥皂客户端System.OverflowException:算术运算导致溢出 - C# soap client System.OverflowException: Arithmetic operation resulted in an overflow SQL错误“算术运算导致溢出。” - Sql error “Arithmetic operation resulted in an overflow.” 算术运算导致溢出。 在oAuthTwitterWrapper中 - Arithmetic operation resulted in an overflow. in oAuthTwitterWrapper 算术运算导致溢出。 (加整数) - Arithmetic operation resulted in an overflow. (Adding integers) IDataRecord.IsDBNull导致System.OverflowException(算术溢出) - IDataRecord.IsDBNull causes an System.OverflowException (Arithmetic Overflow) 算术运算导致溢出。 在AS400中删除数据时 - Arithmetic operation resulted in an overflow. When Delete Data in AS400 protobuf-net反序列化:“算术运算导致溢出。” - protobuf-net deserializing: “Arithmetic operation resulted in an overflow.” WinForm控件ListView“算术运算导致溢出。” - WinForm Control ListView “Arithmetic operation resulted in an overflow.” OdbcDataAdapter填充引发OverflowException算术运算导致溢出 - OdbcDataAdapter Fill throws OverflowException Arithmetic operation resulted in an overflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM