title: Can I parallelize image processing with PLINQ?
body: <p>I want to compare the alpha channel from one image to ~1000 other images. My compare method looks like this:</p>
  
  <pre><code>public static unsafe double Similiarity (Bitmap a, Bitmap b)
  {
      BitmapData aData = a.LockBits (
          new Rectangle (0, 0, a.Width, a.Height), 
          System.Drawing.Imaging.ImageLockMode.ReadOnly, a.PixelFormat);
      BitmapData bData = b.LockBits (
          new Rectangle (0, 0, b.Width, b.Height), 
          System.Drawing.Imaging.ImageLockMode.ReadOnly, b.PixelFormat);
      int PixelSize = 4;
  
      double sum = 0;
      for (int y=0; y&lt;aData.Height; y++) {
          byte* aRow = (byte *)aData.Scan0 + (y * aData.Stride);
          byte* bRow = (byte *)bData.Scan0 + (y * bData.Stride);
          for (int x=0; x&lt;aData.Width; x++) {
              byte aWeight = aRow [x * PixelSize + 3];
              byte bWeight = bRow [x * PixelSize + 3];
              sum += Math.Abs (aWeight - bWeight);
          }
      }
      a.UnlockBits (aData);
      b.UnlockBits (bData);
  
      return 1 - ((sum / 255) / (a.Width * a.Height));
  }
  </code></pre>
  
  <p>I thought the simplest way to speed up the computation would be by using PLINQ:</p>
  
  <pre><code>var list = from Bitmap img in imageList.AsParallel where (Similiarity (referenceImage, img) &gt; 0.5) select img;
  </code></pre>
  
  <p>But on execution there is an Exception in gdiplus:</p>
  
  <pre><code>System.InvalidOperationException: The operation is invalid [GDI+ status: Win32Error]
    at System.Drawing.GDIPlus.CheckStatus (Status status) [0x00000] in &lt;filename unknown&gt;:0 
    at System.Drawing.Bitmap.LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format, System.Drawing.Imaging.BitmapData bitmapData) [0x00000] in &lt;filename unknown&gt;:0 
  </code></pre>
  
  <p>I understand that gdiplus must be executed in different processes, but I thought PLINQ is doing so. What is wrong with my assumptions?</p>