鉴于大家对其他类别十分关注,我们编辑小组在此为大家搜集整理了“基于DirectDraw设计的数字语音教学系统”一文,供大家参考学习
客服咨询,网学网竭诚为您服务,本站永久域名:myeducs.cn |
3.2.2位图的保存 根据位图的句柄将位图写入文件,可以使用如下代码[14]: void CatchFull::WriteBmpToFile(HBITMAP hBitmap) { HDC hDC =::CreateDC("DISPLAY", NULL, NULL, NULL); //当前分辨率下每个像素所占字节数 int iBits = ::GetDeviceCaps(hDC, BITSPIXEL) * ::GetDeviceCaps(hDC, PLANES); ::DeleteDC(hDC); WORD wBitCount; //位图中每个像素所占字节数 if (iBits <= 1) wBitCount = 1; else if (iBits <= 4) wBitCount = 4; else if (iBits <= 8) wBitCount = 8; else if (iBits <= 24) wBitCount = 24; else wBitCount = iBits; DWORD dwPaletteSize = 0; //调色板大小,位图中像素字节大小 if (wBitCount <= 8) dwPaletteSize = (1 << wBitCount) * sizeof(RGBQUAD); BITMAP bm; //位图属性结构 ::GetObject(hBitmap, sizeof(bm), (LPSTR)&bm); BITMAPINFOHEADER bi; //位图信息头结构 bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth = bm.bmWidth; bi.biHeight = bm.bmHeight; bi.biPlanes = 1; bi.biBitCount = wBitCount; bi.biCompression = BI_RGB; //BI_RGB表示位图没有压缩 bi.biSizeImage = 0; bi.biXPelsPerMeter = 0; bi.biYPelsPerMeter = 0; bi.biClrUsed = 0; bi.biClrImportant = 0; DWORD dwBmBitsSize = ((bm.bmWidth * wBitCount+31)/32) * 4 * bm.bmHeight; //为位图内容分配内存 HANDLE hDib = ::GlobalAlloc(GHND,dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER)); LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib); *lpbi = bi; HANDLE hPal = ::GetStockObject(DEFAULT_PALETTE); //处理调色板 HANDLE hOldPal = NULL; if (hPal) { hDC = ::GetDC(NULL); hOldPal = SelectPalette(hDC,(HPALETTE)hPal, FALSE); RealizePalette(hDC); } // 获取该调色板下新的像素值 ::GetDIBits(hDC, hBitmap, 0, (UINT) bm.bmHeight,(LPSTR)lpbi + sizeof(BITMAPINFOHEADER)+dwPaletteSize,(BITMAPINFO*)lpbi, DIB_RGB_COLORS); if (hOldPal) //恢复调色板 { SelectPalette(hDC, (HPALETTE)hOldPal, TRUE); RealizePalette(hDC); ::ReleaseDC(NULL, hDC); } BITMAPFILEHEADER bmfHdr; //位图文件头结构 bmfHdr.bfType = 0x4D42; //设置位图文件头 DWORD dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize; bmfHdr.bfSize = dwDIBSize; bmfHdr.bfReserved1 = 0; bmfHdr.bfReserved2 = 0; bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize; //创建位图文件 HANDLE hFile = CreateFile(m_strFilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); DWORD dwWritten; //写入位图文件头 WriteFile(hFile, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL); WriteFile(hFile, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);// 写入位图文件其余内容 GlobalUnlock(hDib); //清除 GlobalFree(hDib); CloseHandle(hFile); } 3.2.3区域截屏 区域截屏,需要指定当前屏幕要截取的矩形区域的左上角点(X1,Y1)和右下角点(X2,Y2)的X和Y坐标,为的是得到指定屏幕区域的位图句柄,具体实现代码如下: HBITMAP CaptureSelect::CopyScreenToBitmap(LPRECT lpRect)//lpRect 代表选定区域 { HDC hSrcDC, hMemDC; HBITMAP hBitmap, hOldBitmap; // 屏幕和内存设备描述表 int nX1, nY1, nX2, nY2; // 选定区域坐标 int nWidth, nHeight; // 位图宽度和高度 int xScr, yScr; // 屏幕分辨率 if(IsRectEmpty(lpRect)) // 确保选定区域不为空矩形 { return NULL; } //为屏幕创建设备描述表 hSrcDC=CreateDC("DISPLAY", NULL, NULL, NULL); //为屏幕设备描述表创建兼容的内存设备描述表 hMemDC=CreateCompatibleDC(hSrcDC); // 获得选定区域坐标 nX1=lpRect->left; nY1=lpRect->top; nX2=lpRect->right; nY2=lpRect->bottom; // 获得屏幕分辨率 xScr=GetDeviceCaps(hSrcDC, HORZRES); yScr=GetDeviceCaps(hSrcDC, VERTRES); //确保选定区域是可见的 if(nX1 < 0) nX1 = 0; if(nX2 > xScr) nX2 = xScr; if(nY1 < 0) nY1 = 0; if (nY2 > yScr) nY2 = yScr; nWidth = nX2 - nX1; nHeight = nY2 - nY1; // 创建一个与屏幕设备描述表兼容的位图 hBitmap = CreateCompatibleBitmap(hSrcDC, nWidth, nHeight); // 把新位图选到内存设备描述表中 hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap); // 把屏幕设备描述表拷贝到内存设备描述表中 BitBlt(hMemDC, 0, 0, nWidth, nHeight, hSrcDC, nX1, nY1, SRCCOPY); hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap); //得到屏幕位图的句柄 //清除 DeleteDC(hMemDC); DeleteDC(hSrcDC); return hBitmap; // 返回位图句柄 } 通过这一步将得到指定屏幕区域的位图句柄hBitmap,若要将这位图写到文件,亦可以通过函数WriteBmpToFile(HBITMAP hBitmap)来实现。
|
本站发布的计算机毕业设计均是完整无错的全套作品,包含开题报告+程序+论文+源代码+翻译+答辩稿PPT |
本文选自计算机毕业设计http://myeducs.cn |