屏幕截图保存文件

    CDC *pDC;//屏幕DC
    pDC = CDC::FromHandle(::GetDC(NULL));//获取当前整个屏幕DC
    int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
    int Width = pDC->GetDeviceCaps(HORZRES);
    int Height = pDC->GetDeviceCaps(VERTRES);

    CDC memDC;//内存DC
    memDC.CreateCompatibleDC(pDC);

    CBitmap memBitmap, *oldmemBitmap;//建立和屏幕兼容的bitmap
    memBitmap.CreateCompatibleBitmap(pDC, Width, Height);

    oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
    memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC
    Bitmap bm(memBitmap, NULL/*(HPALETTE)cdc.GetCurrentPalette()->GetSafeHandle()*/);

    wchar_t buff[MAX_PATH] = {0};
    wsprintf(buff, L"%s\\%04d.jpg", m_savePath.c_str(), nIndex);
    CLSID pngClsid;
    GetEncoderClsid(L"image/png", &pngClsid);
    bm.Save(buff, &pngClsid, NULL);

  

posted on 2013-10-17 22:40  快乐的大猪  阅读(198)  评论(0编辑  收藏  举报

导航