方法一:使用GDI
参数说明:
1.CDC * pDC为绘图设备的指针
2.CRect rect为绘制区域, CenterRect是让绘图居中
3.UCHAR * szcolor 是每个点的RGB值,格式时szColor[0] = R,szColor[1] = G,szColor[2] = B依次类推.
4.int nColorLen是szColor的长度
void CShowFontList::DrawColorToBitmap(CDC * pDC, CRect rect, UCHAR * szColor, int nColorLen)
{
if(pDC->GetSafeHdc() == NULL || szColor == NULL || nColorLen < 1)
return;
CRect rectCenter;
CenterRect(rect, rectCenter);
BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = rectCenter.Width();
bmi.bmiHeader.biHeight = -rectCenter.Height(); //当图像是倒立显示的时候,把biHeight改为对应的负值
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
SetDIBitsToDevice( pDC->m_hDC,
rectCenter.left,
rectCenter.top,
rectCenter.Width(),
rectCenter.Height(),
0,
0,
0,
rectCenter.Height(),
szColor,
&bmi,
DIB_RGB_COLORS);
}
方法二:使用GDI+
参数说明:
1.CDC * pDC为绘图设备的指针
2.CRect rect为绘制区域, CenterRect是让绘图居中
3.UCHAR * szcolor 是每个点的ARGB值,格式时szColor[0] =A,szColor[1] = R,szColor[2] = G,szColor[3] = B依次类推.
4.int nColorLen是szColor的长度
5.m_nCharWidth与m_nCharHeight是绘制区实际的宽度和长度
6.int nStride表示的是实际绘制区的每行在szColor中所占的字节数
void CShowFontList::DrawColorToBitmap(CDC * pDC, CRect rect, UCHAR * szColor, int nColorLen)
{
if(pDC->GetSafeHdc() == NULL || szColor == NULL || nColorLen < 1)
return;
Graphics g(pDC->GetSafeHdc());
if(g.GetLastStatus() != Ok)
return;
CRect rectCenter;
CenterRect(rect, rectCenter);
int nStride = m_nCharWidth * 4;
Bitmap * pBmp = new Bitmap(m_nCharWidth, m_nCharHeight, nStride, PixelFormat32bppARGB, szColor);
Rect rectBit(rectCenter.left, rectCenter.top, m_nCharWidth, m_nCharHeight);
if(pBmp->GetLastStatus() != Ok)
return;
g.DrawImage(pBmp, rectBit);
}
第二种绘制方式详细解释可以参考:Bitmap入门