用DirectDraw实现屏幕截图

时间:2021-09-13 15:21:06
  在DirectDraw的非独占模式中,主表面即为当前屏幕。你可以直接Lock住主表面,从而取得主表面图象数据。但如果你要对大量的数据进行直接的操作,最好还是先在系统内存中建一个和主表面一样大小的后台表面。用BltFast把主表面复制一份到后台表面,然后锁住后台表面,再进行操作,这是因为CPU对显存的操作是什分慢的。

1、初始化DirectDraw
  你可以响应WM_CREATE消息,并在OnCreate中初始化DirectDraw

   void CMainFrame::InitDirectX()
  {
     //创建DirectDraw
     if (FAILED( DirectDrawCreate(NULL,&ddraw1,NULL)))
    {
      TRACE(/"Couldn/'t create DirectDraw object.//n/");
    }
    if (FAILED( ddraw1->QueryInterface(IID_IDirectDraw2,(LPVOID *)&ddraw2)))
    {
      TRACE(/"Couldn/'t query the interface.//n/");
    }
    if (ddraw2)
    {
      ddraw2->SetCooperativeLevel(GetSafeHwnd(),DDSCL_NORMAL);
    }
    if (ddraw1)
    {
      ddraw1->Release();
      ddraw1=NULL;
    }

     //创建主表面
     HRESULT r;

    DDSURFACEDESC desc;
    desc.dwSize = sizeof(desc);
    desc.dwFlags = DDSD_CAPS;
    desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
    r=ddraw2->CreateSurface(&desc, &primsurf, 0);
    if(r!=DD_OK)
    {
      AfxMessageBox(/"Create DirectX Surface failed//n /");
      PostMessage(WM_CLOSE);
    }
    r=ddraw2->CreateClipper(0, &clipper, 0);
    if(r!=DD_OK)
    {
      AfxMessageBox(/"CreateClipper() fialed//n /");
      PostMessage(WM_CLOSE);
    }
    r=clipper->SetHWnd(0,GetSafeHwnd());
    if(r!=DD_OK)
    {
      AfxMessageBox(/"SetHWnd() failed//n /");
      PostMessage(WM_CLOSE);
    }
    r=primsurf->SetClipper(clipper);
    if(r!=DD_OK)
    {
      AfxMessageBox(/"SetClipper() fialed//n /");
      PostMessage(WM_CLOSE);
    }

     //创建后台表面
     ZeroMemory(&desc,sizeof(desc));
    desc.dwSize=sizeof(desc);
    desc.dwFlags=DDSD_WIDTH|DDSD_HEIGHT|DDSD_CAPS;
    desc.dwWidth=::GetSystemMetrics(SM_CXSCREEN);
    desc.dwHeight=::GetSystemMetrics(SM_CYSCREEN);
    desc.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;

    r=ddraw2->CreateSurface(&desc,&backsurf,0);
     //收集图象参数
     DDSURFACEDESC ddsd;

    ZeroMemory(&ddsd, sizeof(ddsd));
    ddsd.dwSize=sizeof(ddsd);
    backsurf->GetSurfaceDesc(&ddsd);
    BitCount = ddsd.ddpfPixelFormat.dwRGBBitCount;
    lPitch = ddsd.lPitch;
    dwWidth = ddsd.dwWidth;
    dwHeight = ddsd.dwHeight;
    dwRBitMask = ddsd.ddpfPixelFormat.dwRBitMask;
    dwGBitMask = ddsd.ddpfPixelFormat.dwGBitMask;
    dwBBitMask = ddsd.ddpfPixelFormat.dwBBitMask;
     //确定16Bit色时的修正(16Bit色时有两种显示格式565和555)
     RMove = 8; //这里先假定为565格式
     GMove = 3;
    BMove = 3;
    if(BitCount==16)
    {
      if(dwGBitMask==992)
      {
         //555格式
         RMove = 7;
        GMove = 2;
      }
      if(dwGBitMask==1984)
         //555格式
         BMove = 2;
    }
  }

2、得到图象数据的起始位置指针

   DDSURFACEDESC ddsd;

  ZeroMemory(&ddsd,sizeof(ddsd));
  ddsd.dwSize=sizeof(ddsd);
  if FAILED(backsurf->Lock(NULL, &ddsd, DDLOCK_WAIT , NULL))
    TRACE(/"backsurf->lock failed//n/");
// ddsd.lpSurface即为图象数据的指针

3、图象数据的转换
  DirectX是不支持图象格式转换的,如果你想得到的是24Bit的图象,而现在的显示模式为16Bit色,你就要自己动手了(看过下面的代码后你可能会发现,经转换后图象是倒置的,这是我为了方便对数进行压缩而进行的,你可以改变其for循环语句把图象换过来)。

   void CMainFrame:: Conversion()
  {
    int x,y;
    DDSURFACEDESC ddsd;

    ZeroMemory(&ddsd,sizeof(ddsd));
    ddsd.dwSize=sizeof(ddsd);

    if FAILED(backsurf->Lock(NULL, &ddsd, DDLOCK_WAIT , NULL))
      TRACE(/"backsurf->lock failed//n/");

    BYTE *scr=new BYTE[3*ddsd.dwHeight*ddsd.dwWidth];
    BYTE *scrt=scr;

     //Surface to RGB switch(BitCount)
     {
      case 8:
      {
         //TRACE(/"8 Bit//n/");
         BYTE *tem=(BYTE *)ddsd.lpSurface;
        PALETTEENTRY entry[256];

        HDC hScrDC=CreateDC(/"DISPLAY/", NULL, NULL, NULL);
        ::GetSystemPaletteEntries(hScrDC,0,256, entry);

        for(y=(int)dwHeight-1;y>=0;y--)
        {
          for(x=0;x<(int)dwWidth;x++)
          {
            *scrt++=entry[tem[x+lPitch*y]].peRed;
            *scrt++=entry[tem[x+lPitch*y]].peGreen;
            *scrt++=entry[tem[x+lPitch*y]].peBlue;
          }
        }
        break;
      }
      case 16:
      {
         //TRACE(/"16 Bit//n/");
         WORD *tem=(WORD *)ddsd.lpSurface;
         WORD color;
        for(y=(int)dwHeight-1;y>=0;y--)
        {
          for(x=0;x<(int)lPitch/2;x++)
          {
            color=tem[x+lPitch/2*y];
            *scrt++=(BYTE)((color&dwRBitMask)>>RMove);
            *scrt++=(BYTE)((color&dwGBitMask)>>GMove);
            *scrt++=(BYTE)(((color&dwBBitMask))<< BMove);
          }
        }
        break;
      }
      case 24:
      {
         //TRACE(/"24 Bit//n/");
         BYTE *tem=(BYTE *)ddsd.lpSurface;
        for(y=(int)dwHeight-1;y>=0;y--)
        {
          for(x=0;x<(int)dwWidth*3;x+=3)
          {
            *scrt++=tem[x+2+y*lPitch];
            *scrt++=tem[x+1+y*lPitch];
            *scrt++=tem[x+y*lPitch];
          }
        }
        break;
      }
      case 32:
      {
        AfxMessageBox(/"目前还不支持32Bit色//n请调整为24Bit色或16Bit色/");
        backsurf->Unlock(NULL);
        delete scr; PostMessage(WM_CLOSE);
        break;
      }
    }
    backsurf->Unlock(NULL);
     //在里进行你的数据处理 delete scr;
    //数据处理完毕释放内存
   }