我有一个转换几个JPG文件的代码。这是代码:
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随机出现异常。例外是:
类型' System.ArgumentException'的例外情况发生在mCPanel.exe但未在用户代码中处理 附加信息:参数无效。
当我将目标操作系统更改为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);
});