title: Reading monochrome bitmap pixel colors
body: <p>I don't know a better title, but I'll describe the problem.</p>
  
  <p>A piece of hardware we use has the ability to display images.
  It can display a black and white image with a resolution of 64 x 256.</p>
  
  <p>The problem is the format of the image we have to send to the device.
  It is not a standard bitmap format, but instead it is simply an array of
  bytes representing each pixel of the image. </p>
  
  <p>0 = black, 1 = white.</p>
  
  <p>So if we had an image with the size: 4 x 4 the byte array might look something like:</p>
  
  <p>1000 0100 0010 0001</p>
  
  <p>And the image would look like:</p>
  
  <p><a href="http://www.mediafire.com/imgbnc.php/6ee6a28148d0170708cb10ec7ce6512e4g.jpg" rel="nofollow noreferrer">Bitmap http://www.mediafire.com/imgbnc.php/6ee6a28148d0170708cb10ec7ce6512e4g.jpg</a></p>
  
  <p>The problem is that we need to create this image by creating a monochrome bitmap
  in C# and then convert it to the file format understood by the device.</p>
  
  <p>For example, one might to display text on the device. In order to do so he would
  have to create a bitmap and write text to it:</p>
  
  <pre><code>var bitmap = new Bitmap(256, 64);
  
  using (var graphics = Graphics.FromImage(bitmap))
  {
      graphics.DrawString("Hello World", new Font("Courier", 10, FontStyle.Regular), new SolidBrush(Color.White), 1, 1);
  }
  </code></pre>
  
  <p>There are 2 problems here:</p>
  
  <ol>
  <li>The generated bitmap isn't monochrome</li>
  <li>The generated bitmap has a different binary format</li>
  </ol>
  
  <p>So I need a way to:</p>
  
  <ol>
  <li>Generate a monochrome bitmap in .NET</li>
  <li>Read the individual pixel colors for each pixel in the bitmap</li>
  </ol>
  
  <p>I have found that you can set the pixel depth to 16, 24, or 32 bits, but haven't found monochrome and I have no idea how to read the pixel data.</p>
  
  <p>Suggestions are welcome.</p>
  
  <p><strong>UPDATE:</strong> I cannot use Win32 PInvokes... has to be platform neutral!</p>
  
  <p><strong>FOLLOW UP:</strong> The following code works for me now. (Just in case anybody needs it)</p>
  
  <pre><code>private static byte[] GetLedBytes(Bitmap bitmap)
  {
      int threshold = 127;
      int index = 0;
      int dimensions = bitmap.Height * bitmap.Width;
  
      BitArray bits = new BitArray(dimensions);
  
      //Vertically
      for (int y = 0; y &lt; bitmap.Height; y++)
      {
          //Horizontally
          for (int x = 0; x &lt; bitmap.Width; x++)
          {
              Color c = bitmap.GetPixel(x, y);
              int luminance = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
              bits[index] = (luminance &gt; threshold);
              index++;
          }
      }
  
      byte[] data = new byte[dimensions / 8];
      bits.CopyTo(data, 0);
      return data;
  }
  </code></pre>