【问题标题】:'System.ArgumentException' occurred in System.Drawing.dll but was not handled in user codeSystem.Drawing.dll 中发生“System.ArgumentException”,但未在用户代码中处理
【发布时间】:2018-06-19 19:29:33
【问题描述】:

我收到这个错误

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;
}

我该如何处理这个异常?

【问题讨论】:

  • if (File.Exists(path))
  • 它没有帮助。还是一样的错误。
  • 我认为您的文件不是有效图片。
  • 我有有效的图像文件。当我调试路径显示为“\\Images\\Releases\\Penguins.jpg”
  • @Hima,你在 you_exe_dir\Images\Releases\Penguins.jpg 中有图片吗?

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


【解决方案1】:

为什么不用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;
}

【讨论】:

  • OP 已经有错误消息。您的回答对回答为什么 OP 收到错误没有任何帮助。
  • @Steve,问题是“我该如何处理异常”,这就是
猜你喜欢
  • 2016-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-31
  • 2014-01-07
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多