title: Why does Bitmap.LockBits fail on VM's
body: <p>I use an XP VM and a Vista VM for smoke testing my applications before I deploy them. Both of these VM's are using 32bit color. Not sure if it makes any difference but I'm using VirtualBox. Each machine is also allocated 2GB of ram, 2 processors. XP has 128MB of video ram, Vista 256 (in each case the max I can set them to). The desktop running the machines has 6cores and 16GB of ram. Again, not sure if this information is relevant but who knows. </p>
  
  <p>The following is an extension method I use in order to convert a <code>Bitmap</code> into a bunch of pixels that I can work with directly. Because, after all we need speed. Speed's what we need. Greasy, fast speed! And Bitmap.GetPixel is the antithesis of that.</p>
  
  <pre><code>public static ArgbColor[] GetPixels(this Bitmap bitmap)
  {
    ArgbColor[] results;
    int width;
    int height;
    BitmapData bitmapData;
  
    width = bitmap.Width;
    height = bitmap.Height;
    results = new ArgbColor[width * height];
    bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
  
    unsafe
    {
      ArgbColor* pixelPtr;
  
      pixelPtr = (ArgbColor*)(void*)bitmapData.Scan0;
  
      for (int row = 0; row &lt; height; row++)
      {
        for (int col = 0; col &lt; width; col++)
        {
          results[row * width + col] = *pixelPtr;
  
          pixelPtr++;
        }
      }
    }
  
    bitmap.UnlockBits(bitmapData);
  
    return results;
  }
  </code></pre>
  
  <p>This code works perfectly on my Windows 8.1 "real" machine, as it did when I was using Windows 8 and Windows 7 previously.</p>
  
  <p>But on the VM's, it fails with a "Parameter is not valid." <code>ArgumentException</code>:</p>
  
  <pre><code>System.Drawing.Bitmap.LockBits(Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData)
  System.Drawing.Bitmap.LockBits(Rectangle rect, ImageLockMode flags, PixelFormat format)
  Cyotek.Drawing.ImageExtensions.GetPixels(Bitmap bitmap)
  </code></pre>
  
  <p>According to the <a href="http://msdn.microsoft.com/en-us/library/vstudio/039882h3%28v=vs.90%29.aspx" rel="nofollow">docs</a> this is because either <em>PixelFormat value is not a specific bits-per-pixel value</em> or <em>The incorrect PixelFormat is passed in for a bitmap</em>.</p>
  
  <p>I haven't got VS installed on these VM's but I did drop a test program on that opened an image and printed it's format... which was the expected <code>PixelFormat.Format32bppArgb</code>. I'm using the same set of test images on all 3 systems.</p>
  
  <p>My next assumption is it's limitation of the video hardware of the VM, but at the end of the day it's a bitmap not a 3D scene... I'm pretty sure I should be able to work with it.</p>
  
  <p>Can anyone shed some light on this issue? I'd really like to be able to test some of this code properly instead of just relying on "it works on my machine" and I'd rather not have to install VS on a VM anyway (not that I'm even sure it'll install on XP (I prefer that one over the Vista as it's visibly faster to work with)). </p>
  
  <p>Hopefully someone else has come across this issue and has a solution - Google searching has let me down.</p>