C# C中的System.ArgumentException(参数无效)#
我有一个代码来转换几个JPG文件。代码如下:C# C中的System.ArgumentException(参数无效)#,c#,exception,bitmap,C#,Exception,Bitmap,我有一个代码来转换几个JPG文件。代码如下: Parallel.ForEach(strarrFileList, strJPGImagePath => { Bitmap bmpDest = new Bitmap(1, 1); creationTime = File.GetCreationTime(strJPGImagePath); lastWrit
Parallel.ForEach(strarrFileList, strJPGImagePath =>
{
Bitmap bmpDest = new Bitmap(1, 1);
creationTime = File.GetCreationTime(strJPGImagePath);
lastWriteTime = File.GetLastWriteTime(strJPGImagePath);
lastAccessTime = File.GetLastAccessTime(strJPGImagePath);
try
{
using (Bitmap bmpOrig = new Bitmap(strJPGImagePath))
{
intImageW = bmpOrig.Width;
intImageH = bmpOrig.Height;
if ((intImageW > intImageH) && (intImageW > intLongSide))
{
intImageH = (int)((double)intImageH / ((double)intImageW / (double)intLongSide));
intImageW = intLongSide;
}
else if ((intImageH > intImageW) && (intImageH > intLongSide))
{
intImageW = (int)((double)intImageW / ((double)intImageH / (double)intLongSide));
intImageH = intLongSide;
}
else if ((intImageH == intImageW) && (intImageW > intLongSide))
{
intImageH = intLongSide;
intImageW = intLongSide;
}
else
{
//something
}
try
{
bmpDest = new Bitmap(bmpOrig, new Size((Int32)intImageW, (Int32)intImageH));
}
catch (Exception ex1)
{
throw;
}
}
}
catch (Exception ex2)
{
throw;
}
bmpDest.Save(strJPGImagePath, jgpEncoder, myEncoderParameters);
bmpDest.Dispose();
File.SetCreationTime(strJPGImagePath, creationTime);
File.SetLastWriteTime(strJPGImagePath, lastWriteTime);
File.SetLastAccessTime(strJPGImagePath, lastAccessTime);
});
它适用于少于50个JPG文件,但当文件数量增加时,它会随机给我一个异常ex1或ex2。例外情况是:
mCPanel.exe中发生“System.ArgumentException”类型的异常,但未在用户代码中处理
其他信息:参数无效。
当我将目标操作系统更改为X64时,没有任何异常。但我需要在X86上解决这个问题。我在stackoverflow上找到了一些帖子,但并没有找到答案
谢谢你的帮助
编辑:这是仍然存在相同问题的更新代码:
Parallel.ForEach(strarrFileList, strJPGImagePath =>
{
creationTime = File.GetCreationTime(strJPGImagePath);
lastWriteTime = File.GetLastWriteTime(strJPGImagePath);
lastAccessTime = File.GetLastAccessTime(strJPGImagePath);
Bitmap bmpOrig = new Bitmap(strJPGImagePath);
intImageW = bmpOrig.Width;
intImageH = bmpOrig.Height;
if ((intImageW > intImageH) && (intImageW > intLongSide))
{
intImageH = (int)((double)intImageH / ((double)intImageW / (double)intLongSide));
intImageW = intLongSide;
}
else if ((intImageH > intImageW) && (intImageH > intLongSide))
{
intImageW = (int)((double)intImageW / ((double)intImageH / (double)intLongSide));
intImageH = intLongSide;
}
else if ((intImageH == intImageW) && (intImageW > intLongSide))
{
intImageH = intLongSide;
intImageW = intLongSide;
}
else
{
//do something
}
Bitmap bmpDest = new Bitmap(bmpOrig, new Size((Int32)intImageW, (Int32)intImageH));
bmpOrig.Dispose();
GC.SuppressFinalize(bmpOrig);
bmpDest.Save(strJPGImagePath, jgpEncoder, myEncoderParameters);
bmpDest.Dispose();
GC.SuppressFinalize(bmpDest);
File.SetCreationTime(strJPGImagePath, creationTime);
File.SetLastWriteTime(strJPGImagePath, lastWriteTime);
File.SetLastAccessTime(strJPGImagePath, lastAccessTime);
});
很可能是内存问题,非常确定x86进程在内存达到1GB时会进行调试,在x64中更高,批处理可能是一个更好的解决方案,因为在RAMIt较少的系统中,这可能会更快地消失。这听起来肯定像内存问题。x86有其局限性,肯定无法与x64操作系统的行为/模式相匹配。我认为,仅仅为了测试,您可以在调用dispose
GC.SuppressFinalize(bmpDest)之后添加以下代码代码>它将立即释放对象bmpDest使用的所有非托管资源。@P.K.谢谢。里面的那个怎么样?有时候异常发生在ex1?我是否应该删除“using”而改为使用bmpOrig.Dispose();然后使用GC.SuppressFinalize(bmpOrig)@P.K.我更新了代码(在主帖子中)。它仍然不工作,并显示相同的异常。还有其他解决方案吗?@M0HS3N您可能想尝试一下代码GC.Collect(2,GCCollectionMode.Forced)代码>就在dispose之后,而不是GC.SuppressFinalize(对象)代码>这不应被视为完美解决方案。Dispose仅发出内存释放的信号。只有当GC运行时才会发生这种情况,这是非常不可预测的。在此之前,即使您已调用dispose且对象不存在,编译器也无法使用内存。Collect强制垃圾收集器在dispose之后立即运行。