Source code:
Using System;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. Runtime. InteropServices;
Namespace Nobugz {
Static class Util {
Public static Bitmap BitmapTo1Bpp (Bitmap img ){
Int w = img. Width;
Int h = img. Height;
Bitmap bmp = new Bitmap (w, h, PixelFormat. Format1bppIndexed );
BitmapData data = bmp. LockBits (new Rectangle (0, 0, w, h), ImageLockMode. ReadWrite, PixelFormat. Format1bppIndexed );
For (int y = 0; y Byte [] scan = new byte [(w + 7)/8];
For (int x = 0; x <w; x ++ ){
Color c = img. GetPixel (x, y );
If (c. GetBrightness ()> = 0.5) scan [x/8] | = (byte) (0x80> (x % 8 ));
}
Marshal. Copy (scan, 0, (IntPtr) (int) data. Scan0 + data. Stride * y), scan. Length );
}
Return bmp;
}
}
}
Call method:
Bitmap bmp = Nobugz. Util. BitmapTo1Bpp (new Bitmap (@ "c: \ temp \ test1.bmp "));
Bmp. Save (@ "c: \ temp \ test.bmp", System. Drawing. Imaging. ImageFormat. Bmp );
Note:
A few remarks with this code. sadly, I couldn't avoid the pointer arithmetic in the Marshal. copy () call, the code is not going to work in 64-bit apps. and it is not going to be terribly fast, Bitmap. getPixel () is slow. hope it works for you.