MFC,C++ 截屏

来源:互联网 发布:独角兽全武装数据 编辑:程序博客网 时间:2024/06/05 15:40

分为win32代码和MFC代码,如下

复制代码
 1 void Test_captrueScreenwin32() 2 { 3     HDC hDesktopDc=CreateDC(_T("DISPLAY"), NULL, NULL, NULL); 4     int width=GetDeviceCaps(hDesktopDc,HORZRES); 5     int height=GetDeviceCaps(hDesktopDc,VERTRES); 6  7     HBITMAP hMemBmp; 8     HDC hMemDc; 9     hMemBmp=  CreateCompatibleBitmap(hDesktopDc,width,height);10     hMemDc=CreateCompatibleDC(hDesktopDc);11     ::SelectObject(hMemDc,hMemBmp);12 13     BitBlt(hMemDc,0,0,width,height,hDesktopDc,0,0,SRCCOPY);  //一定得先复制到内存中去14      15     BITMAP bmp;16     ::GetObject(hMemBmp,sizeof(bmp),&bmp);   //从HBITMAP 到BITMAP17     18     BITMAPINFOHEADER m_bihScreen;19     ZeroMemory(&m_bihScreen, sizeof(BITMAPINFOHEADER));//位图信息头20     m_bihScreen.biBitCount = bmp.bmBitsPixel;//每个像素字节大小21     m_bihScreen.biCompression = BI_RGB;22     m_bihScreen.biHeight = bmp.bmHeight;//高度23     m_bihScreen.biPlanes = 1;24     m_bihScreen.biSize = sizeof(BITMAPINFOHEADER);25     m_bihScreen.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小26     m_bihScreen.biWidth = bmp.bmWidth;//宽度27 28     byte *m_pbmScreenData = new byte[bmp.bmWidthBytes * bmp.bmHeight];29     GetDIBits(hMemDc,hMemBmp, 0, height, m_pbmScreenData,  30         (LPBITMAPINFO) &m_bihScreen, DIB_RGB_COLORS);//获取位图数据  从BITMAP到内存数据31 32     TwoDimesionArray<RGBQUAD> colors(width,height);33     for (int j=0;j<height;++j)34     {35         for (int i=0;i<width;++i)36         {37             byte *p=m_pbmScreenData+(height-1-j)*bmp.bmWidthBytes+i*4;38             RGBQUAD c={int(*p),int(*(p+1)),int(*(p+2))};   //这里获取每个位置上的像素39             colors.SetValue(i,j,c);40         }41     }42 43     ImageIO writer;44     writer.WriteBmp(colors,"D:\\z.bmp");45 }
复制代码
复制代码
 1 void Test_captureScreenMFC() 2 { 3     CDC *pDesktopDC = CDC::FromHandle(::GetDC(NULL));//获取当前整个屏幕DC 4     int width = pDesktopDC->GetDeviceCaps(HORZRES); 5     int height = pDesktopDC->GetDeviceCaps(VERTRES); 6  7     CBitmap    memBmp; 8     CDC memDC; 9     memBmp.CreateCompatibleBitmap(pDesktopDC, width, height);10     memDC.CreateCompatibleDC(pDesktopDC);11     memDC.SelectObject(&memBmp);  //将memBitmap选入内存DC12 13     memDC.BitBlt(0, 0, width, height, pDesktopDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC14     15     BITMAP bmp;16     memBmp.GetBitmap(&bmp); //CBitmap到BITMAP17 18     BITMAPINFOHEADER m_bihScreen;  19     ZeroMemory(&m_bihScreen, sizeof(BITMAPINFOHEADER));//位图信息头20     m_bihScreen.biBitCount = bmp.bmBitsPixel;//每个像素字节大小21     m_bihScreen.biCompression = BI_RGB;22     m_bihScreen.biHeight = bmp.bmHeight;//高度23     m_bihScreen.biPlanes = 1;24     m_bihScreen.biSize = sizeof(BITMAPINFOHEADER);25     m_bihScreen.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小26     m_bihScreen.biWidth = bmp.bmWidth;//宽度27 28     byte *m_pbmScreenData = new byte[bmp.bmWidthBytes * bmp.bmHeight];29     GetDIBits(/*pDesktopDC->m_hDC*/memDC.m_hDC, (HBITMAP)memBmp.m_hObject, 0, height, m_pbmScreenData,  30         (LPBITMAPINFO) &m_bihScreen, DIB_RGB_COLORS);//获取位图数据  从BITMAP到内存数据31 32     TwoDimesionArray<RGBQUAD> colors(width,height);33     for (int j=0;j<height;++j)34     {35         for (int i=0;i<width;++i)36         {37             byte *p=m_pbmScreenData+(height-1-j)*bmp.bmWidthBytes+i*4;38             RGBQUAD c={int(*p),int(*(p+1)),int(*(p+2))};39             colors.SetValue(i,j,c);40         }41     }42 43     ImageIO writer;44     writer.WriteBmp(colors,"D:\\z.bmp");45 }
复制代码
GetDIBits是DDB转化为DIB的关键函数。
TwoDimesionArray<T>是自定义的模板类,封装了二维数组的相关操作。
ImageIO 是保存图片到文件的类。

通过这两段代码显示了win32和MFC对应数据类型的不同


转自:http://www.cnblogs.com/xiangism/archive/2012/06/21/2557901.html

原创粉丝点击