winapi 截屏函数(转)

void screen(wchar_t* fileName)
  {
   HWND window = ::GetDesktopWindow();
   HDC _dc = ::GetWindowDC(window);//屏幕DC
   HDC dc = CreateCompatibleDC(0);//内存DC
   
   RECT re;
   ::GetWindowRect(window, &re);
   int w = re.right,
   h = re.bottom;
   void* buf = new char[w*h*4];
   
   HBITMAP bm = CreateCompatibleBitmap(_dc, w, h);//建立和屏幕兼容的bitmap
   SelectObject(dc, bm);//将memBitmap选入内存DC 
   StretchBlt(dc, 0, 0, w, h, _dc, 0, 0, w, h, SRCCOPY);//复制屏幕图像到内存DC
   
   void* f =CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
   
   GetObject(bm, 84, buf);
   
   tagBITMAPINFO bi;
   bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
   bi.bmiHeader.biWidth = w;
   bi.bmiHeader.biHeight = h;
   bi.bmiHeader.biPlanes = 1;
   bi.bmiHeader.biBitCount = 32;
   bi.bmiHeader.biCompression = 0;
   bi.bmiHeader.biSizeImage = 0;
   
   CreateDIBSection(dc, &bi, DIB_RGB_COLORS, &buf, 0, 0);
   GetDIBits(dc, bm, 0, h, buf, &bi, DIB_RGB_COLORS);
   
   BITMAPFILEHEADER bif;
   bif.bfType = MAKEWORD('B','M');
   bif.bfSize = w*h*4+54;
   bif.bfOffBits = 54;
  
   BITMAPINFOHEADER bii;
   bii.biSize = 40;
   bii.biWidth = w;
   bii.biHeight = h;
   bii.biPlanes = 1;
   bii.biBitCount = 32;
   bii.biCompression = 0;
   bii.biSizeImage = w*h*4;
   
   DWORD r;
   WriteFile(f, &bif, sizeof(bif), &r, NULL);
   WriteFile(f, &bii, sizeof(bii), &r, NULL);
   WriteFile(f, buf, w*h*4, &r, NULL);
  
   CloseHandle(f);
   DeleteDC(_dc);
   DeleteDC(dc);
  }
阅读更多
文章标签: winapi colors null bi
个人分类: 学习笔记
想对作者说点什么? 我来说一句

C#实时<em>窗口</em>截图

2018年05月09日 0B 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭