Converting an RGB image to 1 bit-per-pixel monochrome(源码)

One bit per pixel images are essentially an indexed format image having two entries in the colour palette. Unlike a GIF or other 256 colour indexed format which uses a single byte to access one of up-to 256 colours in the palette, the one bit per pixel format uses only a single bit to select colour A or colour B from the palette.

Often used in Fax formats, one bit per pixel images are very compact with one byte of information representing eight pixels.

Very often, a more complex RGB image can be used for the basic raw-image that is used to define the 1 bpp array but this operation in GDI+ and .NET systems is a little more complex than one would hope because you cannot simply get the Graphics object for an indexed image and draw on it. In order to do this conversion, you must manipulate the individual bits of the image by locking it's byte array and selecting the correct bit for the desired pixel.

The LockBits method can be used to obtain a BitmapData object which contains the address of the bitmap in memory, the dimensions and particularly, the stride of the image. All images are stored in an array that fills memory to a four-byte boundary so the stride is used to calculate where each successive line begins in the array. A specific line may be addressed by the formula LineAddress=stride*linenumber and NOT LineAddress = pixeldepth*imagewidth*linenumber as many people assume.

For our single bit per pixel image, the pixel indexing is further complicated by the need to select a specific bit in a byte that contains up to 8 pixels (it may have less than 8 pixels because it may be on the end of a scan-line). To perform this selection, a bit mask is used and rotated into position so that the correct bit may be set or reset as desired.

The simple application shown in listing 1 enables you to load any image into a PictureBox control situated on the left of the form. Once loaded, the image is scanned pixel b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值