MFC快速绘制点阵图形

方法一:使用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入门

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值