单色位图、颜色(色彩)位图相互转换

SetBkColor

The SetBkColor function sets the current background color to the specified color value, or to the nearest physical color if the device cannot represent the specified color value.

COLORREF SetBkColor(
  HDC hdc,           // handle to DC
  COLORREF crColor   // background color value
);

Remarks

If the background mode is OPAQUE, the background color is used to fill gaps between styled lines, gaps between hatched lines in brushes, and character cells. The background color is also used when converting bitmaps from color to monochrome and vice versa. 

SetTextColor

The SetTextColor function sets the text color for the specified device context to the specified color.

COLORREF SetTextColor(
  HDC hdc,           // handle to DC
  COLORREF crColor   // text color
);

 

Remarks

The text color is also used in converting bitmaps from color to monochrome and vice versa.

在MSDN中,以上两个函数(一个是设置背景色,一个是设置前景色)的Remarks下均有说到设置的颜色,在单色位图转色彩位图中会被用到,反之易然。

但是没有具体说明具体的转换原理,我也查找了很久,在官方文档上也没搜索到相关的说明,以下是来自网络朋友的总结,感觉总结的很好,特此引用过来。

以下内容全部来自:http://www.programgo.com/article/20281711249/

当目标dc的位图是颜色位图,源dc的位图是单色的时候,单色位图在实际的光栅操作(ROP)之前会被转换成颜色位图,对应的位如果是0,则被转换目标dc的前景色,如果该位是1,则被转换成目标dc的背景色。相反,如果目标dc的位图是单色位图,源dc的位图是颜色位图,则在实际光栅操作之前要把颜色位图转换成单色位图,转换规则是,颜色位图中所有和背景色一致的象素都变成1,其他的象素都被转换成0。

以上这些是本人自己的观察,理解和总结,至今还没有查到官方的描述。

原文地址:https://www.cnblogs.com/shanql/p/6580274.html