Bitmap bitmap = new Bitmap(imgStream);
int bytesPrefix = 2 * (Marshal.SizeOf(typeof(double)) + Marshal.SizeOf(typeof(int)));
int bytesPerLine = (bitmap.Width - 1) / 8 + 1;
int dataLen = bytesPrefix + bitmap.Height * bytesPerLine;
byte[] dataBuf = new byte[dataLen];
Array.Clear(dataBuf, 0, dataLen);
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
Color thePixel = bitmap.GetPixel(x, y);
if (thePixel.R == 0 && thePixel.G == 0 && thePixel.B == 0)
dataBuf[bytesPrefix + y * bytesPerLine + x / 8] |= (byte)(1 << (7 - x % 8));
}
}