Drectx 3D窗口后台截图
//GDI与DX截屏API操作
LPDIRECTDRAW lpDD = NULL;
LPDIRECTDRAWSURFACE lpDDSPrime = NULL;
LPDIRECTDRAWSURFACE lpDDSBack = NULL;
LPDIRECTDRAWSURFACE lpDDSGdi = NULL;
LPDIRECTDRAWSURFACE lpSurf = NULL; DDSURFACEDESC DDSdesc;
BOOL m_b24=TRUE;
//rfbServerInitMsg m_scrinfo;
RECT m_bmrect; struct _BMInfo {
BITMAPINFO bmi ;
BOOL truecolour;
RGBQUAD cmap[] ;
} m_bminfo; // 用来保存位图信息的结构 int DX_Init() {// DirectX初始化。返回当前表面获取一张屏幕位图的存储空间大小
HRESULT hr; // 初始化directX
hr = DirectDrawCreate(, &lpDD, );
if (FAILED(hr)) return FALSE; hr = lpDD->SetCooperativeLevel(NULL, DDSCL_NORMAL);
if (FAILED(hr)) return FALSE; ZeroMemory(&DDSdesc, sizeof(DDSdesc));
DDSdesc.dwSize = sizeof(DDSdesc);
DDSdesc.dwFlags = DDSD_CAPS;
DDSdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hr = lpDD->CreateSurface(&DDSdesc, &lpDDSPrime, );
if (FAILED(hr)) return FALSE; hr = lpDD->GetGDISurface(&lpDDSGdi);
if (FAILED(hr)) return FALSE; ZeroMemory(&DDSdesc, sizeof(DDSdesc));
DDSdesc.dwSize = sizeof(DDSdesc);
DDSdesc.dwFlags = DDSD_ALL;
hr = lpDDSPrime->GetSurfaceDesc(&DDSdesc);
if (FAILED(hr)) return FALSE; // 初始化位图信息
if ((DDSdesc.dwFlags & DDSD_WIDTH) && (DDSdesc.dwFlags & DDSD_HEIGHT)) {
m_bmrect.left = m_bmrect.top = ;
m_bmrect.right = DDSdesc.dwWidth;
m_bmrect.bottom = DDSdesc.dwHeight;
} else return FALSE; m_bminfo.bmi.bmiHeader.biCompression = BI_RGB;//BI_BITFIELDS;
m_bminfo.bmi.bmiHeader.biBitCount = DDSdesc.ddpfPixelFormat.dwRGBBitCount; // m_bminfo.truecolour = DDSdesc.ddpfPixelFormat.dwFlags & DDPF_RGB;
if (m_bminfo.bmi.bmiHeader.biBitCount > )
m_bminfo.truecolour = TRUE;
else
m_bminfo.truecolour = FALSE; ZeroMemory(&DDSdesc, sizeof(DDSdesc));
DDSdesc.dwSize = sizeof(DDSdesc);
DDSdesc.dwFlags = DDSD_CAPS | DDSD_HEIGHT |DDSD_WIDTH;
DDSdesc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
DDSdesc.dwHeight = m_bmrect.bottom - m_bmrect.top;
DDSdesc.dwWidth = m_bmrect.right - m_bmrect.left;
hr = lpDD->CreateSurface(&DDSdesc, &lpDDSBack, );
if (FAILED(hr)) return FALSE;
// hr = lpDDSPrime->QueryInterface( IID_IDirectDrawSurface3, (LPVOID *)&lpSurf);
// if (FAILED(hr)) return FALSE; switch (m_bminfo.bmi.bmiHeader.biBitCount) {
case :
case :
// Update the bitmapinfo header
m_b24 = TRUE;
m_bminfo.bmi.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
m_bminfo.bmi.bmiHeader.biWidth = ;
m_bminfo.bmi.bmiHeader.biHeight = ;
m_bminfo.bmi.bmiHeader.biPlanes = ;
// m_bminfo.bmi.bmiHeader.biBitCount = 24;
m_bminfo.bmi.bmiHeader.biCompression = BI_RGB;
m_bminfo.bmi.bmiHeader.biSizeImage = abs((m_bminfo.bmi.bmiHeader.biWidth * m_bminfo.bmi.bmiHeader.biHeight * m_bminfo.bmi.bmiHeader.biBitCount)/);
m_bminfo.bmi.bmiHeader.biXPelsPerMeter = (*)/;
m_bminfo.bmi.bmiHeader.biYPelsPerMeter = (*)/;
m_bminfo.bmi.bmiHeader.biClrUsed = ;
m_bminfo.bmi.bmiHeader.biClrImportant = ;
break;
} return m_bminfo.bmi.bmiHeader.biSizeImage;
} BOOL CaptureScreen(RECT &rect, BYTE *scrBuff, UINT scrBuffSize) {// 捕捉屏幕。rect: 区域。scrBuff: 输出缓冲。scrBuffSize: 缓冲区大小
HRESULT hr=; hr = lpDDSBack->BltFast(rect.left,rect.top,lpDDSPrime,&rect,DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
if (FAILED(hr)) return FALSE; DDSURFACEDESC surfdesc;
ZeroMemory(&surfdesc, sizeof(surfdesc));
surfdesc.dwSize = sizeof(surfdesc); hr = lpDDSBack->Lock(&rect, &surfdesc, DDLOCK_READONLY | DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR /*|DDLOCK_NOSYSLOCK*/, NULL);
// hr = lpDDSPrime->Lock(&rect, &surfdesc, DDLOCK_READONLY | DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR /*|DDLOCK_NOSYSLOCK*/, NULL);
if (FAILED(hr)) return FALSE; // copy the data into our buffer
BYTE * destbuffpos, * srcbuffpos;
// m_scrinfo.format.bitsPerPixel = 24;
srcbuffpos = (BYTE *) surfdesc.lpSurface;
destbuffpos = scrBuff; memcpy( destbuffpos, srcbuffpos,m_bminfo.bmi.bmiHeader.biSizeImage); // unlock the primary surface
// lpDDSPrime->Unlock(surfdesc.lpSurface);
lpDDSBack->Unlock(surfdesc.lpSurface);
return TRUE;
} int SaveBitmapToFile(BITMAP *bitmap, LPSTR lpFileName,char *lpBuf) {
DWORD dwWritten;
BITMAPFILEHEADER bmfHdr;
BITMAPINFOHEADER bi;
HANDLE fh=NULL;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth= bitmap->bmWidth;
bi.biHeight = bitmap->bmHeight;
bi.biPlanes = ;
bi.biBitCount = bitmap->bmBitsPixel*;
bi.biCompression = BI_RGB;
bi.biSizeImage = ;
bi.biXPelsPerMeter = ;
bi.biYPelsPerMeter = ;
bi.biClrUsed = ;
bi.biClrImportant = ;
fh = CreateFile(lpFileName, GENERIC_WRITE, , NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh == INVALID_HANDLE_VALUE) return FALSE;
bmfHdr.bfType = 0x4D42; // "BM"
bmfHdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+bitmap->bmWidth*bitmap->bmHeight*bitmap->bmBitsPixel;
bmfHdr.bfReserved1 = ;
bmfHdr.bfReserved2 = ;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
WriteFile(fh, (char *)&bi,sizeof(BITMAPINFOHEADER), &dwWritten, NULL);
WriteFile(fh, (char *)lpBuf,bitmap->bmWidth*bitmap->bmHeight*bitmap->bmBitsPixel, &dwWritten, NULL);
FlushFileBuffers(fh);
CloseHandle(fh);
return true;
} //(1)获取屏幕绘图设备
//(2)创建一个与屏幕绘图设备相兼容的内存绘图设备
//(2)在内存中创建一个与屏幕绘图设备相兼容的图像对象
//(3)将屏幕设备中的图像复制到内存绘图设备中
//(4)将内存图像保存到文件中
//相关函数:
//GetDIBits:按位的方式返回指定的BITMAP,并按指定的格式存储到内存中
int GetBitmapFromScreen(char *lpFileName) {
char *lpBuf;
HBITMAP hBitmap,hOld ;
HDC hDC,hcDC;
BITMAP bb;
BITMAPINFO b;
HANDLE hp,fh=NULL;
DWORD dwX,dwY; dwX=GetSystemMetrics(SM_CXSCREEN);
dwY=GetSystemMetrics(SM_CYSCREEN);
hDC=GetDC(NULL);
hcDC=CreateCompatibleDC(hDC);
hBitmap=CreateCompatibleBitmap(hDC,dwX,dwY);
hOld=(HBITMAP)SelectObject(hcDC,hBitmap);
BitBlt(hcDC,, ,dwX,dwY, hDC, , , SRCCOPY);
bb.bmWidth=dwX;
bb.bmHeight =dwY;
bb.bmPlanes = ;
bb.bmWidthBytes=bb.bmWidth*;
bb.bmBitsPixel=;
bb.bmType=;
b.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
b.bmiHeader.biWidth =dwX;
b.bmiHeader.biHeight =dwY;
b.bmiHeader.biPlanes =;
b.bmiHeader.biBitCount =*;
b.bmiHeader.biCompression =BI_RGB;
b.bmiHeader.biSizeImage =;
b.bmiHeader.biXPelsPerMeter=;
b.bmiHeader.biYPelsPerMeter=;
b.bmiHeader.biClrUsed =;
b.bmiHeader.biClrImportant =;
b.bmiColors[].rgbBlue =;
b.bmiColors[].rgbGreen =;
b.bmiColors[].rgbRed =;
b.bmiColors[].rgbReserved =;
hp=GetProcessHeap();
lpBuf=(char *)HeapAlloc(hp,HEAP_ZERO_MEMORY,bb.bmHeight*bb.bmWidth*);
GetDIBits(hcDC,hBitmap,,dwY,lpBuf,&b,DIB_RGB_COLORS);
SaveBitmapToFile(&bb,lpFileName,lpBuf);
ReleaseDC(NULL,hDC);
DeleteDC(hcDC);
DeleteObject(hBitmap);
DeleteObject(hOld);
HeapFree(hp,,lpBuf);
return true;
}
Drectx 3D窗口后台截图的更多相关文章
- c++ 屏幕截图指定窗口句柄后台截图返回位图句柄
/根据窗口句柄后台截图保存成BMP位图文件并且显示到picture 控件上 void GetScreenBmp(HWND hwnd, int left, int top, int width, int ...
- 基于java的后台截图功能的实现
Java后台截图功能的实现 背景介绍: 在近期开发的可视化二期项目中的邮件项目中,邮件中的正文中含有图片.该图片的产生是将一些html网页转为图片格式,刚开始考虑使用第三方组件库html2image和 ...
- C# 非顶端窗口截图 - 用于查找指定窗口并截图
原文地址:http://blog.csdn.net/u013096568/article/details/53400389 panel上可以通过DrawToBitmap截图,不管是否在屏幕外是否有遮挡 ...
- 按键精灵 句柄 获得句柄 控制windows窗口 后台
新建一个文本文档,打开,Windows就会给这个文本文档的窗口临时分配唯一的一串数字来标识这个窗体,以区别于其他窗口,这串数字就叫句柄. 因为句柄是临时随机分配的,所以每次虽然是打开同一个文件,但 ...
- EasyUI + EF + MVC4 后台截图
到目前完成的页面截图,完成了增删改查几项功能的技术测试,在解决几个小问题,就重新设计结构开始一个完整的后台开发,坚持用博客和云笔记记录开发过程.
- swift - 3D 视图,截图,关键字搜索
1.xib 上的 3D效果 按钮 2. import UIKit //1.导入框架 import MapKit class ViewController: UIViewController { @IB ...
- c# 根据窗口截图,合并图片
c# 根据窗口截图,合并图片 public class CaptureWindows { #region 类 /// <summary> /// Helper class containi ...
- selenium常用操作,查找元素,操作Cookie,获取截图,获取窗口信息,切换,执行js代码
目录: 1. 常用操作 2. 查找元素 3. 操作Cookie 4. 获取截图 5. 获取窗口信息 6. 切换 7. 执行JS代码 简介 selenium.webdriver.remote.webdr ...
- selenium基础(窗口截图)
窗口截图 目的:当脚本执行出错时对当前窗口进行截图 方法:get_screenshot_as_file() #打开百度首页,搜索“selenium",完成后进行截图,并将结果保存至D:/te ...
随机推荐
- MTD应用学习札记【转】
转自:https://blog.csdn.net/lh2016rocky/article/details/70885421 今天做升级方案用到了mtd-utils中的flash_eraseall和fl ...
- android调节声音大小
android调节声音大小 1.背景音乐的一些知识 网上好多关于背景音乐添加用到的类: MediaPlayer,SoundPool,AudioManager的资料,可是有时候解决不了我们在开发中遇到的 ...
- Singleton单例对象的使用
namespace www{ public abstract class SingletonManager<T> : ISingletonManager where T : class, ...
- conda管理python包
参考:http://blog.sina.com.cn/s/blog_c3c116470102wlv5.html 查看python,numpy,scipy,matplotlib的版本及安装位置: htt ...
- react + redux 完整的项目,同时写一下个人感悟
先附上项目源码地址和原文章地址:https://github.com/bailicangd... 做React需要会什么? react的功能其实很单一,主要负责渲染的功能,现有的框架,比如angula ...
- ubuntu下root和安装mysql
sudo password创建新的root密码: 1.用当前登录用户打开终端,在终端输入命令 sudo passwd,输入当前用户的密码然后回车 2.会提示输入新密码,输入完成后回车(http://w ...
- springBoot事物
1.事物 只是需要一个注解即可 2.事物程序 package com.caojun.springboot; import org.springframework.beans.factory.annot ...
- 使用matplotlib绘制散点图
在matplotlib中使用函数 matplotlib.pyplot.scatter 绘制散点图,matplotlib.pyplot.scatter的函数签名如下: matplotlib.pyplot ...
- 【LeetCode刷题】SQL-Second Highest Salary 及扩展以及Oracle中的用法
转载于:https://www.cnblogs.com/contixue/p/7057025.html Write a SQL query to get the second highest sala ...
- jenkins 整合maven,svn(配置钩子程序实现提交代码自动构建),tomcat实现热部署(windows+linux分别实现)
springboot : https://blog.csdn.net/zjh_746140129/article/details/80904876 1 准备工作: (1)运行jenkins的tomca ...