`
zgymm2008
  • 浏览: 12001 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

DirectDraw实现屏幕截图的原代码

 
阅读更多
DirectDraw实现屏幕截图的原代码

HBITMAP CDialogcapDlg::CopyScreenToBitmapDX(LPRECT lpRect)
{
    HBITMAP hBitmap;
    HDC hDC=NULL;
    int height,width,bitsize,palettebit;
    HRESULT hres;

    height = lpRect->bottom - lpRect->top;
    width  = lpRect->right  - lpRect->left;
    InitDirectX(); //初始化DirectDraw环境,使lpDDSPrimay为主页面,lpDDSBack为页面在内存的备份。具体代码可在论坛其它文章了找到。

    hres=lpDDSBack->BltFast(lpRect->left,lpRect->top,lpDDSPrimay,lpRect,DDBLTFAST_NOCOLORKEY|DDBLTFAST_WAIT);
    lpDDSBack->GetDC(&hDC);
    hBitmap=CreateCompatibleBitmap(hDC,width,height);
    DeleteDC(hDC);
    DDSURFACEDESC desc;
    ZeroMemory(&desc,sizeof(desc));
    desc.dwSize=sizeof(desc);
    lpDDSBack->Lock(lpRect,&desc,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);
    bitsize=((desc.dwWidth*desc.ddpfPixelFormat.dwRGBBitCount+31)/32)*4*desc.dwHeight;

    palettebit=0; 
    if (desc.ddpfPixelFormat.dwRGBBitCount <=
       palettebit = (1 << desc.ddpfPixelFormat.dwRGBBitCount) *  sizeof(RGBQUAD);

    LPBITMAPINFOHEADER lpbi;
    LPBYTE lpbit;
    lpbi=(LPBITMAPINFOHEADER)GlobalLock(hBitmap);
    lpbi->biSize=sizeof(BITMAPINFOHEADER);
    lpbi->biWidth=desc.dwWidth;
    lpbi->biHeight=desc.dwHeight;
    lpbi->biPlanes=1;
    lpbi->biBitCount=desc.ddpfPixelFormat.dwRGBBitCount;
    lpbi->biCompression=BI_BITFIELDS;
    lpbi->biSizeImage=bitsize;
    lpbi->biXPelsPerMeter=0;
    lpbi->biYPelsPerMeter=0;
    lpbi->biClrImportant=0;
    lpbi->biClrImportant=0;
    lpbit=(LPBYTE)(lpbi+1)+palettebit;
    memcpy(lpbit,desc.lpSurface,bitsize);


    lpDDSBack->Unlock(NULL);

    return hBitmap;

}

回复人: wuliangge2001(就这么回事儿) ( ) 信誉:100  2004-3-2 10:50:46  得分:25
 
我也发一个从视频流界图的吧
    CString   strFilter = "BMP File (*.bmp)|*.bmp|";
        CFileDialog dlgOpen(FALSE, ".bmp", NULL, OFN_HIDEREADONLY, strFilter, NULL);
        if (IDOK == dlgOpen.DoModal())
        {

            CString chFilePath;
            chFilePath = dlgOpen.GetFileName();

            if (pBV)
            {
                long bitmapSize = 0;
                if (SUCCEEDED(pBV->GetCurrentImage(&bitmapSize, 0)))
                {
                    bool pass = false;
                    //unsigned char * buffer = new unsigned char[bitmapSize];
                    if (SUCCEEDED(pBV->GetCurrentImage(&bitmapSize, (long *)bmpbuffer)))
                    {
                        BITMAPFILEHEADER    hdr;
                        LPBITMAPINFOHEADER    lpbi;
                       
                        lpbi = (LPBITMAPINFOHEADER)bmpbuffer;
                       
                        int nColors = 1 << lpbi->biBitCount;
                        if (nColors > 256)
                            nColors = 0;
                       
                        hdr.bfType        = ((WORD) (''M'' << | ''B'');    //always is "BM"
                        hdr.bfSize        = bitmapSize + sizeof( hdr );
                        hdr.bfReserved1     = 0;
                        hdr.bfReserved2     = 0;
                        hdr.bfOffBits        = (DWORD) (sizeof(BITMAPFILEHEADER) + lpbi->biSize +
                            nColors * sizeof(RGBQUAD));
                       
                        FILE * file;
                        file = fopen(chFilePath,"a""b");
                        fwrite(&hdr,sizeof(BITMAPFILEHEADER),1,file);
               
                        fwrite(bmpbuffer,  1,bitmapSize, file);
                        fclose(file);
                                       
                    }
                //    delete [] buffer;
                }
            }

 

Top

回复人: GodSpeed513(GodSpeed513) ( ) 信誉:97  2004-3-3 1:27:47  得分:0
这些都没有用到DirectX技术,不能够扑捉到视屏图象!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics