VC获取窗口图片(截图)

//借鉴网上的文章:

HBITMAP CopyDCToBitmap(HDC hDC, LPRECT lpRect)
{
 if(!hDC || !lpRect || IsRectEmpty(lpRect))
  return NULL;
 
 HDC hMemDC; 
 HBITMAP hBitmap, hOldBitmap; 
 int nX, nY, nX2, nY2; 
 int nWidth, nHeight;
 
 nX = lpRect->left;
 nY = lpRect->top;
 nX2 = lpRect->right;
 nY2 = lpRect->bottom;
 nWidth = nX2 - nX;
 nHeight = nY2 - nY;
 
 hMemDC = CreateCompatibleDC(hDC);
 
 hBitmap = CreateCompatibleBitmap(hDC, nWidth, nHeight);
 
 hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
 
 StretchBlt(hMemDC, 0, 0, nWidth, nHeight, hDC, nX, nY, nWidth, nHeight, SRCCOPY);
 
 hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
 
 DeleteDC(hMemDC);
 DeleteObject(hOldBitmap);
 
 
 return hBitmap;
}

 

 

 

 

 

 

 

 

 

 

 

//抓屏,保存为图片

 

BOOL SaveBmp(HBITMAP bmp,char* path)    //32
{    
//把位图的信息保存到bmpinfo;    
BITMAP bmpinfo;    
GetObject(bmp,sizeof(BITMAP),&bmpinfo);    
DWORD dwBmBitsSize = ((bmpinfo.bmWidth * 32+31)/32) * 4 * bmpinfo.bmHeight;     
//位图文件头 14字节    
BITMAPFILEHEADER bf;    
bf.bfType      = 0x4D42;                  
//BM    
bf.bfSize      = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize;     
bf.bfReserved1 = 0;     
bf.bfReserved2 = 0;     
bf.bfOffBits   = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);     
//位图信息头    
BITMAPINFOHEADER bi;    
bi.biSize          = sizeof(BITMAPINFOHEADER);    
bi.biWidth         = bmpinfo.bmWidth;    
bi.biHeight        = bmpinfo.bmHeight;    
bi.biPlanes        = 1;    
bi.biBitCount      = 32;    
bi.biCompression   = BI_RGB;    
bi.biSizeImage     = 0;    
bi.biXPelsPerMeter = 0;    
bi.biYPelsPerMeter = 0;    
bi.biClrUsed       = 8;    
bi.biClrImportant  = 0;    
//位图数据;    
char* context = new char[dwBmBitsSize];
HDC dc  = ::GetDC(NULL);    
GetDIBits(dc, bmp, 0, bi.biHeight, context, (BITMAPINFO*)&bi, DIB_RGB_COLORS);    
FILE* f = fopen(path,"wb");    
fwrite((char*)&bf,sizeof(BITMAPFILEHEADER),1,f);    
fwrite((char*)&bi,sizeof(BITMAPINFOHEADER),1,f);    
fwrite(context,dwBmBitsSize,1,f);    
fclose(f);    
delete context;    
::ReleaseDC(NULL,dc);    
return 0;    
}  

 

BOOL SaveBmp1(HBITMAP bmp, char* path) //24
{
    BITMAP bmpinfo;
    GetObject(bmp, sizeof(BITMAP), &bmpinfo);
    //DWORD dwBmBitsSize = ((bmpinfo.bmWidth * 32 + 31) / 32) * 4 * bmpinfo.bmHeight;
DWORD dwBmBitsSize = bmpinfo.bmWidth * bmpinfo.bmHeight * 3;

    BITMAPFILEHEADER bf;
    bf.bfType = 0x4D42;

    bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize;
    bf.bfReserved1 = 0;
    bf.bfReserved2 = 0;
    bf.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

    BITMAPINFOHEADER bi;
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bmpinfo.bmWidth;
    bi.biHeight = bmpinfo.bmHeight;
    bi.biPlanes = 1;
    bi.biBitCount = /*32*/24;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 8;
    bi.biClrImportant = 0;

    unsigned char* context = new unsigned char[dwBmBitsSize];
    HDC dc = ::GetDC(NULL);
    GetDIBits(dc, bmp, 0, bi.biHeight, (unsigned char*)context, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
     FILE* f = fopen(path, "wb");
     fwrite((char*)&bf, sizeof(BITMAPFILEHEADER), 1, f);
     fwrite((char*)&bi, sizeof(BITMAPINFOHEADER), 1, f);
     fwrite(context, dwBmBitsSize, 1, f);
     fclose(f);
    delete[] context;
    ::ReleaseDC(NULL, dc);
    return 0;
}




void CaptureScreen()
{
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hDesktopWnd = GetDesktopWindow();
HDC hDesktopDC = GetDC(hDesktopWnd);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,
nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,hCaptureBitmap);
BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,hDesktopDC,0,0,SRCCOPY);
SaveBmp(hCaptureBitmap, "F:\\capture.bmp"); //Place holder - Put your code
//here to save the captured image to disk
ReleaseDC(hDesktopWnd,hDesktopDC);
DeleteDC(hCaptureDC);
DeleteObject(hCaptureBitmap);
}






void CCaptureScreenDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码


CaptureScreen();
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chinabinlang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值