获取屏幕显示

CDC* pDeskDC =  GetDesktopWindow()->GetDC(); //获取桌面画布对象

CRect rc;
GetDesktopWindow()->GetClientRect(rc); //获取屏幕的客户区域


int width  = 567; //获取屏幕的宽度
int height = 452; //获取屏幕的高度


CDC  memDC; //定义一个内存画布
memDC.CreateCompatibleDC(pDeskDC); //创建一个兼容的画布
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDeskDC,width,height); //创建兼容位图
memDC.SelectObject(&bmp); //选中位图对象

BITMAP bitmap;
bmp.GetBitmap(&bitmap);

int panelsize  = 0; //记录调色板大小
if (bitmap.bmBitsPixel<16) //判断是否为真彩色位图
panelsize = (int)(pow(2,bitmap.bmBitsPixel*sizeof(RGBQUAD)));//2的N次方


BITMAPINFO *pBInfo = (BITMAPINFO*)LocalAlloc(LPTR,sizeof(BITMAPINFO)+panelsize);
pBInfo->bmiHeader.biBitCount      = bitmap.bmBitsPixel;
pBInfo->bmiHeader.biClrImportant  = 0;
pBInfo->bmiHeader.biCompression   = 0;
pBInfo->bmiHeader.biWidth         = width;
pBInfo->bmiHeader.biHeight        = height;
pBInfo->bmiHeader.biPlanes        = bitmap.bmPlanes;
pBInfo->bmiHeader.biSize          = sizeof(BITMAPINFO);
pBInfo->bmiHeader.biSizeImage     = bitmap.bmWidthBytes*bitmap.bmHeight;
pBInfo->bmiHeader.biXPelsPerMeter = 0;
pBInfo->bmiHeader.biYPelsPerMeter = 0;
m_X = m_RecvX;
m_Y = m_RecvY;
memDC.BitBlt(0,0,width,height,pDeskDC,m_X,m_Y,SRCCOPY);
char* pData = new char[bitmap.bmWidthBytes* bitmap.bmHeight];
::GetDIBits(memDC.m_hDC,bmp,0,bitmap.bmHeight,pData,pBInfo,DIB_RGB_COLORS);


int BufSize = panelsize+ sizeof(BITMAPINFO)+bitmap.bmWidthBytes* bitmap.bmHeight;


memcpy(pSendBuf,pBInfo,sizeof(BITMAPINFO));
char* pHead = pSendBuf;
pSendBuf += sizeof(BITMAPINFO);
memcpy(pSendBuf,pData,bitmap.bmWidthBytes* bitmap.bmHeight);


pSendBuf = pHead;


addr1.sin_family = AF_INET;
addr1.sin_port   = htons(5002);
addr1.sin_addr.S_un.S_addr = inet_addr(m_ServerIP);
//定义数据报的格式
/*序号2位||结束标记2位||位图数据||位图数据大小4位||屏幕X坐标2位||屏幕Y坐标2位||数据报大小4位*/


//定义每次发送位图数据的大小
bmpsize = GraphSize;
//计算每个位图发送的次数
count = BufSize / GraphSize;
mod   = BufSize % GraphSize;
if (mod != 0)
count+=1;


m_FrameIndex = 0;
int ret = SendData(m_FrameIndex,mod,bmpsize,count,pSendBuf,addr1);


pSendBuf = pHead;
delete []pData;
LocalFree(pBInfo);


pDeskDC->DeleteDC();
bmp.DeleteObject();
memDC.DeleteDC();

你可能感兴趣的:(位图抓取)