Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why I get a black screen while capturing screen?

Why I get a black screen while capturing screen?

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Guoguor
    wrote on last edited by
    #1

    I want to use the follow codes to capture screen ,but I get a black screen. Please help me to find the errors! thanks! HWND hDesktopWnd=::GetDesktopWindow(); HDC hDesktopDC=::GetDC(hDesktopWnd); HDC hDesktopCompatibleDC=CreateCompatibleDC(hDesktopDC); HBITMAP hDesktopCompatibleBitmap=CreateCompatibleBitmap(hDesktopDC,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN)); SelectObject(hDesktopCompatibleDC,hDesktopCompatibleBitmap); BitBlt(hDesktopCompatibleDC,0,0,GetSystemMetrics(SM_CXSCREEN) ,GetSystemMetrics(SM_CYSCREEN) ,hDesktopDC,0,0,SRCCOPY); // InvalidateRect(NULL,false); HDC hBmpFileDC=CreateCompatibleDC(hDesktopCompatibleDC); HBITMAP hBmpFileBitmap=CreateDIBSection(hDesktopCompatibleDC,&bi,DIB_RGB_COLORS,&pBits,NULL,0); SelectObject(hBmpFileDC,hBmpFileBitmap); BitBlt(hBmpFileDC,0,0,width,height,hDesktopCompatibleDC,0,0,SRCCOPY); // the fellow codes is save the bitmap HANDLE hFile=CreateFile((unsigned short *)szFileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); bi.bmiHeader.biBitCount=16; // bi.bmiHeader.biClrUsed=0; bi.bmiHeader.biHeight=GetSystemMetrics(SM_CYSCREEN); //GetHeight(); bi.bmiHeader.biWidth=GetSystemMetrics(SM_CXSCREEN); //GetWidth(); bi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); bi.bmiHeader.biPlanes=1; bi.bmiHeader.biCompression=BI_RGB; // bi.bmiHeader.biXPelsPerMeter=0; // bi.bmiHeader.biYPelsPerMeter=0; // bi.bmiHeader.biClrImportant=0; DWORD bitSize=((bi.bmiHeader.biWidth*16)/8)*bi.bmiHeader.biHeight; bi.bmiHeader.biSizeImage=bitSize; bitHeader.bfType=((WORD)('M'<<8)|'B'); bitHeader.bfReserved1=0; bitHeader.bfReserved2=0; bitHeader.bfOffBits=(DWORD)(sizeof(BITMAPFILEHEADER)+bi.bmiHeader.biSize); bitHeader.bfSize=bi.bmiHeader.biSizeImage+bitHeader.bfOffBits; if(hFile!=INVALID_HANDLE_VALUE) { DWORD dwRet=0; WriteFile(hFile,&bitHeader,sizeof(bitHeader),&dwRet,NULL); WriteFile(hFile,&bi.bmiHeader,sizeof(bi.bmiHeader),&dwRet,NULL); WriteFile(hFile,pBits,bi.bmiHeader.biSizeImage,&dwRet,NULL); CloseHandle(hFile); } DeleteDC(hBmpFileDC); DeleteDC(hDesktopCompatibleDC); ::ReleaseDC(hDesktopWnd,hDesktopDC); DeleteObject(hBmpFileBitmap); DeleteObject(hDesktopCompatibleBitmap);

    Y 1 Reply Last reply
    0
    • G Guoguor

      I want to use the follow codes to capture screen ,but I get a black screen. Please help me to find the errors! thanks! HWND hDesktopWnd=::GetDesktopWindow(); HDC hDesktopDC=::GetDC(hDesktopWnd); HDC hDesktopCompatibleDC=CreateCompatibleDC(hDesktopDC); HBITMAP hDesktopCompatibleBitmap=CreateCompatibleBitmap(hDesktopDC,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN)); SelectObject(hDesktopCompatibleDC,hDesktopCompatibleBitmap); BitBlt(hDesktopCompatibleDC,0,0,GetSystemMetrics(SM_CXSCREEN) ,GetSystemMetrics(SM_CYSCREEN) ,hDesktopDC,0,0,SRCCOPY); // InvalidateRect(NULL,false); HDC hBmpFileDC=CreateCompatibleDC(hDesktopCompatibleDC); HBITMAP hBmpFileBitmap=CreateDIBSection(hDesktopCompatibleDC,&bi,DIB_RGB_COLORS,&pBits,NULL,0); SelectObject(hBmpFileDC,hBmpFileBitmap); BitBlt(hBmpFileDC,0,0,width,height,hDesktopCompatibleDC,0,0,SRCCOPY); // the fellow codes is save the bitmap HANDLE hFile=CreateFile((unsigned short *)szFileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); bi.bmiHeader.biBitCount=16; // bi.bmiHeader.biClrUsed=0; bi.bmiHeader.biHeight=GetSystemMetrics(SM_CYSCREEN); //GetHeight(); bi.bmiHeader.biWidth=GetSystemMetrics(SM_CXSCREEN); //GetWidth(); bi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); bi.bmiHeader.biPlanes=1; bi.bmiHeader.biCompression=BI_RGB; // bi.bmiHeader.biXPelsPerMeter=0; // bi.bmiHeader.biYPelsPerMeter=0; // bi.bmiHeader.biClrImportant=0; DWORD bitSize=((bi.bmiHeader.biWidth*16)/8)*bi.bmiHeader.biHeight; bi.bmiHeader.biSizeImage=bitSize; bitHeader.bfType=((WORD)('M'<<8)|'B'); bitHeader.bfReserved1=0; bitHeader.bfReserved2=0; bitHeader.bfOffBits=(DWORD)(sizeof(BITMAPFILEHEADER)+bi.bmiHeader.biSize); bitHeader.bfSize=bi.bmiHeader.biSizeImage+bitHeader.bfOffBits; if(hFile!=INVALID_HANDLE_VALUE) { DWORD dwRet=0; WriteFile(hFile,&bitHeader,sizeof(bitHeader),&dwRet,NULL); WriteFile(hFile,&bi.bmiHeader,sizeof(bi.bmiHeader),&dwRet,NULL); WriteFile(hFile,pBits,bi.bmiHeader.biSizeImage,&dwRet,NULL); CloseHandle(hFile); } DeleteDC(hBmpFileDC); DeleteDC(hDesktopCompatibleDC); ::ReleaseDC(hDesktopWnd,hDesktopDC); DeleteObject(hBmpFileBitmap); DeleteObject(hDesktopCompatibleBitmap);

      Y Offline
      Y Offline
      YoSilver
      wrote on last edited by
      #2

      Do not get the desktop window DC. It causes serious problems. Instead of this

      HWND hDesktopWnd=::GetDesktopWindow();
      HDC hDesktopDC=::GetDC(hDesktopWnd);

      write this

      HDC hDesktopDC = ::CreateDC("DISPLAY",0,0,0);

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups