title: Fast pixel drawing c++ windows forms
body: <p>I am trying to make a c++ emulator for the original space invaders arcade machine. Everything works just fine but it is slow. After timing several functions in my project i found that the biggest drain is my draw screen functions. It takes +100ms but i need 16ms/frame, e.g. 60Hz. This is my function:</p>
  
  <pre><code>unsigned char *fb = &amp;state-&gt;memory[0x2400];
  Bitmap ^ bmp = gcnew Bitmap(512, 448);
  
  for (int j = 1; j &lt; 224; j++)
  {
      for (int i = 0; i &lt; 256; i+=8)
      {
          unsigned char pix = fb[(j * 256 / 8) + i / 8];
          for (int p = 0; p &lt; 8; p++)
          {
              if (0 != (pix &amp; (1 &lt;&lt; p)))
              {
                  bmp-&gt;SetPixel(i + p, j, Color::White);
              }
              else
              {
                  bmp-&gt;SetPixel(i + p, j, Color::Black);
              }
          }
      }
  }
  bmp-&gt;RotateFlip(RotateFlipType::Rotate270FlipNone);
  this-&gt;pictureBox1-&gt;Image = bmp;
  </code></pre>
  
  <p>fb is my framebuffer, a byte array with 8 pixels per byte, 1 for white, 0 for black.</p>
  
  <p>Browsing the internet i found that the "slow part" is the SetPixel method, but i couldn't find a better method to do this.</p>