0

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.

1
  • When you find yourself initializing a variable with a nonsense object like new Bitmap(1, 1) think: There must be a better way. Commented Aug 2, 2014 at 22:38

1 Answer 1

2

All your Tasks access to the same shared Bitmap bmpDest.

Move the definition of it to Parallel.ForEach block so that every task can use its own Bitmap..

Parallel.ForEach(strarrFileList, strJPGImagePath =>
{
     Bitmap bmpDest = new Bitmap(1, 1);
     ........
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but then where should I dispose it? Currently I dispose it after the loop.
@M0HS3N Save it and then dispose it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.