"参数无效." 使用保存位图时

Cru*_*lIO 3 c# exception bitmap

我试图保存具有指定编码质量的位图jpg格式.但是,在调用save方法时,我得到一个异常("参数无效.").

如果我省略了bmp.save中的最后两个参数,它可以正常工作.

        EncoderParameters eps = new EncoderParameters(1);
        eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 16);
        ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
        string outfile = outputpath + "\\" + fileaddition + sourcefile.Name;
        bmp.Save(outfile,ici,eps );

        bmp.Dispose();
        image.Dispose();
        return true;
    }
    ImageCodecInfo GetEncoderInfo(string mimeType)
    {
        int j;
        ImageCodecInfo[] encoders;
        encoders = ImageCodecInfo.GetImageEncoders();
        for (j = 0; j < encoders.Length; ++j)
        {
            if (encoders[j].MimeType == mimeType)
                return encoders[j];
        }
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Han*_*ant 26

GDI +非常不稳定.您需要使用16L作为值或转换为(long).

  • 它是.文字"16"的类型是C#中的字节,VB.NET中的整数.EncoderParameter的构造函数采用byte,short和long,但不是int.你会在VB中得到正确的,但在C#中得不到. (2认同)
  • nobugz:不,那是错的.标准在§9.4.4.2中说"文字没有后缀,它有第一种类型,其值可以表示:`int`,`uint`,`long`,`ulong` "它永远不会*`byte`.或者这可能是Microsoft C#编译器中的错误? (2认同)