I am trying to make my code faster by using tasks (Parallel.foreach). Here is my updated code:
int intImageW, intImageH;
Bitmap bmpDest = new Bitmap(1, 1);
DateTime creationTime, lastWriteTime, lastAccessTime;
Parallel.ForEach(strarrFileList, strJPGImagePath =>
{
creationTime = File.GetCreationTime(strJPGImagePath);
lastWriteTime = File.GetLastWriteTime(strJPGImagePath);
lastAccessTime = File.GetLastAccessTime(strJPGImagePath);
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
mSplash("This photo (" + Path.GetFileName(strJPGImagePath) + ") is smaller than the desired size!");
bmpDest = new Bitmap(bmpOrig, new Size(intImageW, intImageH));
}
bmpDest.Save(strJPGImagePath, jgpEncoder, myEncoderParameters);
File.SetCreationTime(strJPGImagePath, creationTime);
File.SetLastWriteTime(strJPGImagePath, lastWriteTime);
File.SetLastAccessTime(strJPGImagePath, lastAccessTime);
});
However, it gives me this exception:
An exception of type 'System.InvalidOperationException' occurred in System.Drawing.dll but was not handled in user code Additional information: Object is currently in use elsewhere. If there is a handler for this exception, the program may be safely continued.
The exception happens on this line:
bmpDest.Save(strJPGImagePath, jgpEncoder, myEncoderParameters);
any idea of how to solve this is appreciated.