#ifndef _CAPTURESCREEN_H
#define _CAPTURESCREEN_H #include <windows.h>
class CaptureScreen
{
public:
CaptureScreen()
{};
~CaptureScreen(void)
{}; void ScreenInit();
void ScreenFree();
bool ScreenSetWindow(LPRECT lpRect);
bool ScreenCopy(BYTE *dstBuf); int rHeight; //要获得的图像尺寸
int rWidth;
private:
int nScreenWidth;
int nScreenHeight;
PRGBTRIPLE scan0;
BITMAPINFO pbi;
int stride;
int top_x;
int top_y;
int bot_x;
int bot_y;
}; #endif
 #include "CaptureScreen.h"

 void CaptureScreen::ScreenInit()
{
nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
nScreenHeight = GetSystemMetrics(SM_CYSCREEN); pbi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbi.bmiHeader.biWidth = nScreenWidth;
pbi.bmiHeader.biHeight = nScreenHeight;
pbi.bmiHeader.biPlanes = ;
pbi.bmiHeader.biBitCount = ;
pbi.bmiHeader.biCompression = BI_RGB;
stride = ((nScreenWidth * + ) & 0xffffffe0) / ; // 24位图像扫描线宽度
scan0 = (PRGBTRIPLE)malloc(stride * nScreenHeight); // 图像数据缓冲区,应释放
} void CaptureScreen::ScreenFree()
{
free(scan0);
} bool CaptureScreen::ScreenSetWindow(LPRECT lpRect)
{
top_x = lpRect->left;
top_y = lpRect->top;
bot_x = lpRect->right;
bot_y = lpRect->bottom;
if(top_x >= bot_x)
return false;
if(top_y >= bot_y)
return false;
if(top_x< || bot_x>nScreenWidth)
return false;
if(top_y< || bot_y>nScreenHeight)
return false; rHeight = bot_y-top_y;
rWidth = bot_x-top_x;
return true;
} bool CaptureScreen::ScreenCopy(BYTE *dstBuf)
{
HWND hDesktopWnd = GetDesktopWindow();
HDC hDesktopDC = GetDC(hDesktopWnd);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,hCaptureBitmap);
BitBlt(hCaptureDC,,,nScreenWidth,nScreenHeight,hDesktopDC,,,SRCCOPY|NOMIRRORBITMAP);
DeleteDC(hCaptureDC); GetDIBits(hDesktopDC, hCaptureBitmap, , nScreenHeight, scan0, &pbi, DIB_RGB_COLORS);
ReleaseDC(hDesktopWnd,hDesktopDC);
DeleteObject(hCaptureBitmap); BYTE *srcBuf = (BYTE*) scan0; int j;
srcBuf += (nScreenHeight--top_y)*stride+top_x*;// scan0倒序
for(j=;j<rHeight;j++)
{
memcpy(dstBuf,srcBuf,rWidth*);
dstBuf += rWidth*;
srcBuf -= stride;
}
return true;
}
 #include "CaptureScreen.h"
#include <stdio.h>
#include <Mmsystem.h>
#pragma comment( lib,"winmm.lib" )
void SaveBmp2(BYTE *bgr32,int w,int h,int mode)
{ /////////////////////////////////////////////
DWORD size=w*h*;
LPSTR lpData=(LPSTR)GlobalAlloc(GPTR,size);
/////////////////////////////////////////////
BITMAPINFOHEADER bih;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biWidth=w;
bih.biHeight=h;
bih.biPlanes=;
bih.biBitCount=;
bih.biCompression=;
bih.biSizeImage=w*h*;//size;
bih.biXPelsPerMeter=;
bih.biYPelsPerMeter=;
bih.biClrUsed=;
bih.biClrImportant=; BITMAPFILEHEADER bfh;
bfh.bfType=0x4D42; // "BM" // 设置位图文件头
bfh.bfReserved1=bfh.bfReserved2=;
bfh.bfSize=+size;
bfh.bfOffBits=; if(mode == )
{
for(int i=;i<h;i++)
{
memcpy(lpData+(h--i)*w*,bgr32+i*w*,w*);
}
}
else
memcpy(lpData,bgr32,w*h*); static int filecount=;
char filename[];
sprintf(filename,"C:\\%d.bmp",filecount);
FILE *fp=fopen(filename,"wb"); if(fp)
{ fwrite(&bfh,,sizeof(BITMAPFILEHEADER),fp);
fwrite(&bih,,sizeof(BITMAPINFOHEADER),fp);
fwrite(lpData,,size,fp);
fclose(fp);
filecount++;
}
GlobalFree(lpData); } int main()
{ CaptureScreen myCapture;
myCapture.ScreenInit();
RECT dstRect={,,,};
myCapture.ScreenSetWindow(&dstRect);
BYTE *dstBuff = (BYTE*)malloc(myCapture.rWidth*myCapture.rHeight*);
int lastTime = timeGetTime();
myCapture.ScreenCopy(dstBuff);
printf("time:%dms",timeGetTime()-lastTime);
SaveBmp2((BYTE*)dstBuff,myCapture.rWidth,myCapture.rHeight,);
myCapture.ScreenFree(); return ;
}

windows截屏的更多相关文章

  1. C# 编写一个简单易用的 Windows 截屏增强工具

    半年前我开源了 DreamScene2 一个小而快并且功能强大的 Windows 动态桌面软件.有很多的人喜欢,这使我有了继续做开源的信心.这是我的第二个开源作品 ScreenshotEx 一个简单易 ...

  2. 团队软件开发_基于windows下截屏软件关于NABC框架的特点

    经过我们小组数次的激烈讨论,就自己的能力和时间而言,我们小组的初步的计划是开发一款基于windows下的截图软件. 关于这个软件的功能,我们初步的想法如下: 1.能在windows下后台运行,有相应的 ...

  3. Windows之系统自带截屏快捷键

    Windows之系统自带截屏快捷键 现在我们都习惯了使用QQ截屏,但是有时候电脑没有网络,也就意味着无法登陆QQ,在这个时候再有截屏的需求时,我们就束手无策了. 截取全屏 现在我说以个Windows系 ...

  4. 【Windows 10 应用开发】如何防止应用程序被截屏

    今天老周只想跟大伙们分享一个小技巧,是的,小小的技巧,很简单,保证你能学会的,要是学不会,可以考虑跳泰山. 有些时候,我们可能会想到不要让应用程序界面上显示的内容被截屏,要阻止应用界面呈现在截图上,可 ...

  5. 截屏快捷键(windows)自带截屏,不需要安装任何插件

    方案一:电脑自带截屏,不需要QQ Shift+win+s即可截图(推荐使用) 方案二:搜狗输入法下载自带:Ctrl+Shift+x

  6. MonoGame 3.2 下,截屏与 Texture2D 的保存

    10月20日注:后来发现了这篇博文(英文),XNA 中的 Color 实际上是与 Alpha 值自左乘(premultiplied)的,这也解释了直接用 0xARGB 转译而颜色异常的原因. 注意,由 ...

  7. C#截屏

    本实例代码实现了WinForm截屏保存为图片,亲测可行. 界面截图: 下载:http://hovertree.com/h/bjaf/scjyuanma.htm 以下代码可以实际运行,在项目HoverT ...

  8. 一个仿windows泡泡屏保的实现

    一个仿windows泡泡屏保的实现 有天看到有人在百度知道上问windows 泡泡屏保该怎么用C#做,一时有趣,就做了一个出来,对于其中几个要点总结如下: 一,屏保程序的制作要求 屏保程序的扩展名是. ...

  9. OpenGL 多视图与截屏

    最近看红宝书学习 OpenGL 一段时间了,写了简单的 demo 程序温习一下知识. 主要是 使用 glScissor 多视图显示画面和使用 glReadPixels 给画面截屏,使用显示列表(dis ...

随机推荐

  1. 如何使用不同参数组合生成独立的TestCase函数(Python)

    在使用selenium2 Python做自动化测试的时候遇到个问题,写一个testcase 生成报告后,会有一个case的执行状态记录.这样我们写一个登录功能的自动化用例,只写一个case显然是不行的 ...

  2. 加载页面(Loading)

    /* 文件说明:页面加载时Loading JS 文件描述:解决IE或FF下,初始化加载时,页面布局乱掉的问题,参考:*/var width = $(window).width();var height ...

  3. 2017年1月8日 星期日 --出埃及记 Exodus 21:34

    2017年1月8日 星期日 --出埃及记 Exodus 21:34 the owner of the pit must pay for the loss; he must pay its owner, ...

  4. 2017年1月6日 星期五 --出埃及记 Exodus 21:32

    2017年1月6日 星期五 --出埃及记 Exodus 21:32 If the bull gores a male or female slave, the owner must pay thirt ...

  5. 2016年6月27日 星期一 --出埃及记 Exodus 14:24

    2016年6月27日 星期一 --出埃及记 Exodus 14:24 During the last watch of the night the LORD looked down from the ...

  6. 基于kickstart定制自动化安装的linux镜像系统

    使用软件和平台 1.基于平台:                  Vmware workstation 8.0 2.基于系统镜像:               rhel-server-5.8-i386 ...

  7. web打印

    实现方法 引用jquery和,jqprint到您的页面 <script language="javascript" src="jquery-1.4.4.min.js ...

  8. 一SERVLET (1)

    转载自http://www.cnblogs.com/xdp-gacl/p/3760336.html 一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公 ...

  9. HTML在IE中的条件注释

    HTML在IE中的条件注释 HTML的条件注释在IE5中被首次引入,直到IE9.一直都是简单地判定用户浏览器(IE,非IE,IE版本)的一种手段,而在IE10的标准模式下,条件注释功能被停止支持(兼容 ...

  10. mysql中整数类型后面的数字,是不是指定这个字段的长度?比如int(11),11代表11个字节吗?

    原文地址:    http://www.cnblogs.com/stringzero/p/5707467.html 原先对mysql不太理解,但也没有报错.但理解的不够深入.这次补上. 原来以为int ...