mfc 截取桌面 并保存为 bmp 图片

void CPowerDownDlg::CopyScreenToBitmap(void)
{
 
 
 dcc = GetDesktopWindow()->GetWindowDC();
 CBitmap hbitmap;//参数可选
 

 dcMem =  CreateCompatibleDC(*dcc); //guanlian
 BITMAPINFO RGB16BitsBITMAPINFO;
 ZeroMemory(&RGB16BitsBITMAPINFO, sizeof(BITMAPINFO));
 RGB16BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
 RGB16BitsBITMAPINFO.bmiHeader.biWidth = 480;
 RGB16BitsBITMAPINFO.bmiHeader.biHeight = 800;
 RGB16BitsBITMAPINFO.bmiHeader.biPlanes = 1;
 RGB16BitsBITMAPINFO.bmiHeader.biBitCount = 16;

 BYTE *lpBitmapBits = NULL;
 HBITMAP directBmp = CreateDIBSection(dcMem, (BITMAPINFO*)&RGB16BitsBITMAPINFO,
  DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
 HGDIOBJ previousObject = SelectObject(dcMem, directBmp);

 BitBlt(dcMem, 0, 0, 480, 800, *dcc, 0, 0, SRCCOPY);
 
 BITMAPFILEHEADER bmBITMAPFILEHEADER;
 ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
 bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
 bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
 bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((480*800)*2); ///2=(16 / 8)

 FILE *mStream = NULL;
 if((mStream = fopen("deskmap.bmp", "wb")))
 {
  //write bitmap file header
  fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
  //write bitmap info
  fwrite(&(RGB16BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
  //write bitmap pixels data
  fwrite(lpBitmapBits, 2*480*800, 1, mStream);
  //close file
  fclose(mStream);
 }


 //delete 如果只是导出bmp 需要销毁
  DeleteObject(dcMem);
  DeleteObject(dcc);
  DeleteObject(directBmp);
  DeleteObject(previousObject);
}

 

截取的桌面图片保存在 deskmap.map 中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值