GDI+双缓冲绘图是非常简单的,但是用GDI绘图,就麻烦一些;
但是GDI绘图有一定的优势:速度更快一些;而且适应范围更宽,最近发现用GDI+绘图,在有些win7系统上无法绘制,当然有可能是各类盗版系统引起的,但是没有更好的办法,这里用GDI双缓冲绘图就可以了;
示例如下,根据需要修改应用:
这个示例汇中,主要说明的GDI的双缓冲绘图,也说明了,GDI和GDI+的混合绘图;
void DrawImageToMainDlg( int width, int height, unsigned char*imageData )
{
{
CDC* pDC;
CDC memDC;
CBitmap memBitmap;//内存绘图
CBitmap* pOldBmp = NULL;//内存绘图
CRect rect;
CWnd* pWnd = this;//控件窗口
pWnd->GetClientRect(&rect);//控件客户区
pDC = pWnd->GetDC();
//创建内存绘图设备
memDC.CreateCompatibleDC(pDC);
//memBitmap.CreateCompatibleBitmap( pDC, rect.right, rect.bottom);
memBitmap.CreateCompatibleBitmap( pDC, width, height);
pOldBmp = memDC.SelectObject(&memBitmap);
//GDI绘图
unsigned char * pixels = imageData;
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = -height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
SetStretchBltMode(
memDC.m_hDC,
COLORONCOLOR );
::StretchDIBits(
memDC.m_hDC,
0, 0, rect.Width(), rect.Height(),
0, 0, width, height,
pixels, &bmi, DIB_RGB_COLORS, SRCCOPY );
//GDI+ 绘图
Graphics gimage(memDC.m_hDC);
Gdiplus::Bitmap * pNewImage = GetNewImage();
gimage.DrawImage( pNewImage , 0, 0 );
//绘制到对话框;
SetStretchBltMode(
pDC->m_hDC,
COLORONCOLOR );
:: StretchBlt(pDC->m_hDC,
0, 0,
rect.Width() , rect.Height() ,
memDC.m_hDC,
0, 0,
width, height,
SRCCOPY);
memDC.SelectObject(pOldBmp);
memDC.DeleteDC();
memBitmap.DeleteObject();
return ;
}
GDI 绘图到窗口 方法:
void draw_imageBuf_to_wnd( char * pbuf, int widht ,int height, HWND m_hWnd )
{
HDC hdc = ::GetDC( m_hWnd );
static BITMAPINFO bmi;
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
SetStretchBltMode(hdc,COLORONCOLOR);
::StretchDIBits(
hdc,
0, 0,
rectdlg.Width(),
rectdlg.Height(),
0, 0, width, height,
pbuf, &bmi, DIB_RGB_COLORS, SRCCOPY);
::ReleaseDC(m_hWnd, hdc);
}
上述讲述了双缓冲绘图;
GDI绘图;
GDI+绘图;
上述的绘图,一般是32位绘图;
还有常用的8位图,想要回执8位位图,一般的思路是构造8位相关文件头,然后绘制图像,其实这样比较麻烦;
比较简单的思路是,将8位的图像转为32位图像直接用上面函数绘制;
uint8_t * convert_OneByteImage_to_ThreeByteImage( uint8_t * pOneByteBuf, int widht, int height)
{
int nSize = widht* height * 3;
static uint8_t * pRGB = new uint8_t[nSize];
uint8_t * prgbTemp = pRGB;
for (int i = 0; i < nSize / 3; i++)
{
for (int n = 0; n < 3; n++)
{
prgbTemp[n] = pOneByteBuf[i];
}
prgbTemp = prgbTemp + 3;
}
return pRGB;
}