'System.ArgumentException' 发生在 System.Drawing.dll 但未在用户代码中处理
'System.ArgumentException' occurred in System.Drawing.dll but was not handled in user code
我遇到了这个错误
System.ArgumentException: Parameter is not valid.
at System.Drawing.Bitmap..ctor(String filename)
at 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;
}
如何处理这个异常?
你为什么不用 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;
}
我遇到了这个错误
System.ArgumentException: Parameter is not valid. at System.Drawing.Bitmap..ctor(String filename) at 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;
}
如何处理这个异常?
你为什么不用 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;
}