title: Access Violation Using BitmapData in Image Mask Function
body: <p>I wrote a version of this function which takes a grayscale bitmap as a mask and a source bitmap and outputs a bitmap with the mask applied using <code>SetPixel</code> and <code>GetPixel</code> but it was very slow, so instead I tried to write one using BitmapData and pointer arithmetic but I get an access violation and I'm not sure why.</p>
  
  <p>I get an <code>AccessViolationException</code> when writing to the pixel in the result bitmap - I think this is caused by incorrect indexing, but I can't see where I've gone wrong.</p>
  
  <pre><code>public static Bitmap ApplyAlphaMask2(Bitmap source, Bitmap mask)
  {
      if (source.Size != mask.Size)
      {
          throw new NotImplementedException("Applying a mask of a different size to the source image is not yet implemented");
      }
      Bitmap result = new Bitmap(source.Width, source.Height);
      unsafe
      {
          BitmapData source_data = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
          BitmapData mask_data = mask.LockBits(new Rectangle(0, 0, mask.Width, mask.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
          BitmapData result_data = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
          for(int column = 0; column &lt; source.Height; column++)
          {
              Int32* source_column_ptr = (Int32*)source_data.Scan0 + (column * Math.Abs(source_data.Stride));
              Int32* mask_column_ptr = (Int32*)mask_data.Scan0 + (column * Math.Abs(mask_data.Stride));
              Int32* result_column_ptr = (Int32*)result_data.Scan0 + (column * Math.Abs(mask_data.Stride));
              for (int row = 0; row &lt; source.Width; row++)
              {
                  Color source_color = Color.FromArgb(source_column_ptr[row]);
                  Color mask_color = Color.FromArgb(mask_column_ptr[row]);
                  Int32* result_pixel_ptr = &amp;result_column_ptr[row];
                  Color result_color = Color.FromArgb((int)(255.0f * mask_color.GetBrightness()), source_color);
                  *result_pixel_ptr = result_color.ToArgb(); //Access violation!
              }
          }
          source.UnlockBits(source_data);
          mask.UnlockBits(mask_data);
          result.UnlockBits(result_data);
      }
      return result;
  }
  </code></pre>
  
  <p>Any help would be appreciated.</p>
  
  <p>EDIT: This doesn't always occur on the same column, although the row appears always to be 0</p>