'System.ArgumentException'发生在System.Drawing.dll中,但未在用户代码中处理

时间:2018-06-19 19:29:33

标签: c# asp.net-mvc exception-handling

我收到此错误

  

System.ArgumentException:参数无效。      在System.Drawing.Bitmap..ctor(字符串文件名)      在PressRoom.ImageHandler.getResizedImage(String path,Int32 width,Int32 height)

在线

byte[] getResizedImage(String path, int width, int height)
{
        if (path!=null)
        {
            Bitmap imgIn = new Bitmap(path); // exception is thrown
            double y = imgIn.Height;
            double x = imgIn.Width;
}

如何处理此异常?

1 个答案:

答案 0 :(得分:0)

为什么不用try-catch包围代码? 通常在路径无效时引发异常,请检查文件是否存在两次或路径格式正确

Bitmap imgIn;
try
{
    imgIn = new Bitmap(path);
    double y = imgIn.Height;
    double x = imgIn.Width;
}
catch (ArgumentException e)
{
    Console.WriteLine(e.Message);
    return null;
}