none
Error on Bitmap.Save()

    Question

  • Hi,

    I'm using the JPEG Encoder with a Bitmap.Save()-call.
    I got the following code from the german MSDN:

        ImageCodecInfo ici = null;
        ImageCodecInfo[] iCodecs = ImageCodecInfo.GetImageEncoders();
        foreach (ImageCodecInfo ic in iCodecs)
        {
            if (ic.MimeType == "image/jpeg")
            {
                ici = ic;
                break;
            }
        }
        EncoderParameters ep = new EncoderParameters(1);
        ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (int)90);
        Result.Save(CurrentTargetFile(), ici, ep);

    After the execution of the last code line I get the following exception:

    "System.ArgumentException"
      Message="Invalid Parameter."
      Source="System.Drawing"
      StackTrace:
           bei System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
           bei imgproc.ImgProc.BatchProc()

    Can anybody find any Problem? The filename parameter is 100% OK.
    Thank you!

    Wednesday, July 26, 2006 11:31 AM

Answers

  •  MEder wrote:

        ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (int)90);

    Change the parameter (int)90 to (long)90.

        ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)90);

    That should do the trick.

    Wednesday, July 26, 2006 2:11 PM
    Moderator

All replies

  •  MEder wrote:

        ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (int)90);

    Change the parameter (int)90 to (long)90.

        ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)90);

    That should do the trick.

    Wednesday, July 26, 2006 2:11 PM
    Moderator
  • Thank you, that's it!
    Is it just my impression or are these "special" topics really never dealt with in the VS online help?
    I would at least expect a link "Description of the codecs"... not only an example (which doesn't work under some conditions) on the german MSDN website. To be honest I didn't read all trough ;-)

    Wednesday, July 26, 2006 7:20 PM
  • Well, I agree that generally the 'special' topics get little attention compared to the more common scenarios. To some respect that's understandable. We wouldn't want people hitting their heads constantly on database operations, ado.net, etc. so these topics are covered in more detail.

    As for this particular case, there is some documentation here. But looking at it closely, there's a little issue. For one, it doesn't explain the paramater type. All it says, it has be between 0 and 100. That can easily fit in an int. Look at the VB.NET sample:

    myEncoderParameter = New EncoderParameter(myEncoder, CType(25L, Int32))

    Here, the parameter is specifically cast to an int. The C# sample, however says:

    myEncoderParameter = new EncoderParameter(myEncoder, 25L);

    The parameter is set as a long. Now, that's confusing, and from the looks of it, the VB.NET code is wrong. However, when we take a look at the compiled code, from the VB.NET sample:

    EncoderParameter parameter1 = new EncoderParameter(encoder1, (long) 0x19);

    Now, that's confusing right? The VB compiler simply ignores the cast to Int32. So CType or not, the VB.NET sample will also work.

    Thursday, July 27, 2006 6:15 AM
    Moderator