1

I created .NET Core console app and added reference to System.Drawing.Common nuget. I'm trying to run the following code from ms docs, but it fails on bitmap1.GetEncoderParameterList with System.OverflowException:

'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);
Access Denied
  • 8,723
  • 4
  • 42
  • 72
  • 1
    My guess is that the image you're trying to load is corrupt, or not an image, or of an unsupported format. From experience, the image loading code in .NET is rather short on this kind of error handling so it may just be that it tried to construct memory for a terabyte-sized image because it got very high width and height values from the file and during the calculation to figure out how much memory to allocate the total amount overflowed. – Lasse V. Karlsen Nov 01 '18 at 07:33
  • @LasseVågsætherKarlsen I tried another bitmap, the same story Bitmap bitmap1 = new Bitmap(Image.FromFile(@"C:\temp\0\2.png")); ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Png); – Access Denied Nov 01 '18 at 07:38
  • @LasseVågsætherKarlsen no, for png I used pngdecoder. – Access Denied Nov 01 '18 at 08:49

2 Answers2

2

EDIT: Seems like it's working fine on .NET Framework and it throws an exception as OP with .NET Core 2.1.

EDIT2: Seems like it's a known issue, listed here: https://github.com/dotnet/docs/issues/5607

Julius R
  • 155
  • 8
  • I don't see how it's related to windows forms, except from a tag on an issue, but it doesn't matter if you call that method on windows forms or anywhere else. From my point of view it seems that it's just lack of up-to-date documentation from microsoft for .net core. – Julius R Nov 01 '18 at 07:53
  • thanks for confirming, I created bug in corefx repo. The bug you referenced is related to docs. But my sample is related to code, not docs. This should not happen. – Access Denied Nov 01 '18 at 08:02
  • I still don't understand this part of your answer: You are calling your method GetSupporterParameters wrong. It's not really wrong. You can make all methods static or you can do like me. My approach does not require making all methods static. – Access Denied Nov 01 '18 at 08:23
  • I am getting the same error in .NET Framework 4.8 on Windows 10 (new-style WPF project). – Mike Rosoft Nov 23 '20 at 12:09
1

This is a bug in .NET Core and is being addressed for .NET Core 5.0: see https://github.com/dotnet/corefx/pull/40181.

Frederik Carlier
  • 4,606
  • 1
  • 25
  • 36