- 浏览: 2577 次
- 性别:
- 来自: 上海
文章分类
最新评论
最近因为工作需要,认真研究了一下屏幕截图的方法。
最主要的方法有两种,一、调用windows GDI32 API函数。二、使用DirectX9.0来实现。
另外,光注了一下Microsoft Expression Encoder 4 Screen Capture这个微软新出的功能,Expression Encoder 4 可实现屏幕录制,录制 文件格式为WMV ,为免费使用版本,Expression Encoder 4 Pro为 收费版本。
还 看了一下基于windows图形驱动技术的屏幕截图方法 ,文章链接地址:http://blog.csdn.net/jia162/article/details/2509974。实现起来可能比较困难与复杂。没找到实例参考及技术实现的算法,因此也就没有深入研究。
下面两种方法
一、GDI32 API截图,600*480大小生成Bitmap位图大概需要45ms左右,生成位图并save成bmp文件需要大概110ms左右,图片越大,耗费的时间越长,效率比较低。
二、DirectX截图,是把整个屏幕的拷贝到内存里,再进行截取,执行拷贝屏幕的方法g_pd3dDevice->GetFrontBufferData(0, g_pSurface)需要80ms-100ms,效率也比较低。
若那位技术牛人有好滴方法,请推荐一下哦!!!
方法一实现:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace DataFrameFetch
{
public class WinGdi32Api
{
[DllImport("GDI32.dll")]
public static extern bool BitBlt(int hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, int hdcSrc, int nXSrc, int nYSrc, int dwRop);
[DllImport("GDI32.dll")]
public static extern int CreateCompatibleBitmap(int hdc, int nWidth, int nHeight);
[DllImport("GDI32.dll")]
public static extern int CreateCompatibleDC(int hdc);
[DllImport("GDI32.dll")]
public static extern bool DeleteDC(int hdc);
[DllImport("GDI32.dll")]
public static extern bool DeleteObject(int hObject);
[DllImport("GDI32.dll")]
public static extern int GetDeviceCaps(int hdc, int nIndex);
[DllImport("GDI32.dll")]
public static extern int SelectObject(int hdc, int hgdiobj);
[DllImport("User32.dll")]
public static extern int GetDesktopWindow();
[DllImport("User32.dll")]
public static extern int GetWindowDC(int hWnd);
[DllImport("User32.dll")]
public static extern int GetDC(int hWnd);
[DllImport("User32.dll")]
public static extern int ReleaseDC(int hWnd, int hDC);
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace DataFrameFetch
{
public class DataFetch
{
public DataFetch()
{ }
/// <summary>
/// 全屏截图
/// </summary>
/// <returns></returns>
public Bitmap CaptureScreen()
{
DateTime dt_start = DateTime.Now;
int source = WinGdi32Api.GetWindowDC(WinGdi32Api.GetDesktopWindow());
int bitmap = WinGdi32Api.CreateCompatibleBitmap(source, WinGdi32Api.GetDeviceCaps(source,, WinGdi32Api.GetDeviceCaps(source, 10));
int destination = WinGdi32Api.CreateCompatibleDC(source);
WinGdi32Api.SelectObject(destination, bitmap);
WinGdi32Api.BitBlt(destination, 0, 0, WinGdi32Api.GetDeviceCaps(source,, WinGdi32Api.GetDeviceCaps(source, 10), source, 0, 0, 0x00CC0020);
Bitmap img = this.GetBitmap(bitmap);
this.Cleanup(bitmap, source, destination);
DateTime dt_end = DateTime.Now;
TimeSpan ts = dt_end - dt_start;
System.Diagnostics.Debug.WriteLine(ts.Milliseconds.ToString());
return img;
}
public Bitmap CaptureScreen(Control control)
{
DateTime dt_start = DateTime.Now;
int source = WinGdi32Api.GetDC(control.Handle.ToInt32());
int bitmap = WinGdi32Api.CreateCompatibleBitmap(source, control.Width,control.Height);
int destination = WinGdi32Api.CreateCompatibleDC(source);
WinGdi32Api.SelectObject(destination, bitmap);
WinGdi32Api.BitBlt(destination, 0, 0, control.Width, control.Height, source, 0, 0, 0x00CC0020);
Bitmap img = this.GetBitmap(bitmap);
this.Cleanup(bitmap, source, destination);
byte[] buffer = this.ConvertBitmapToRGBByteArray(img);
string filename = "F:\\img\\" + dt_start.ToString("yyyyMMddHHmmss") + dt_start.Millisecond.ToString();
FileStream fs = new FileStream(filename + ".olc", FileMode.Create);
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
fs.Close();
//Bitmap bmp = this.FromRGB(buffer, img.Width, img.Height);
//bmp.Save(filename + "_1.bmp");
DateTime dt_end = DateTime.Now;
TimeSpan ts = dt_end - dt_start;
System.Diagnostics.Debug.WriteLine(ts.Milliseconds.ToString());
return img;
}
private void Cleanup(int bitmap, int source, int destination)
{
WinGdi32Api.ReleaseDC(WinGdi32Api.GetDesktopWindow(), source);
WinGdi32Api.DeleteDC(destination);
WinGdi32Api.DeleteObject(bitmap);
}
private Bitmap GetBitmap(int hbitmap)
{
Bitmap bmp = new Bitmap(Image.FromHbitmap(new IntPtr(hbitmap)), Image.FromHbitmap(new IntPtr(hbitmap)).Width, Image.FromHbitmap(new IntPtr(hbitmap)).Height);
return bmp;
}
/// <summary>
/// 将位图转换成数组
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
private byte[] ConvertBitmapToRGBByteArray(Bitmap bmp)
{
Rectangle rect = new Rectangle(new Point(0,0),bmp.Size);
BitmapData bmpdata = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int len = bmp.Width * bmp.Height * 3;
byte[] buffer = new byte[len];
int index = 0;
unsafe
{
byte* color = (byte*)bmpdata.Scan0;
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
buffer[index++] = *color;
buffer[index++] = *(color + 1);
buffer[index++] = *(color + 2);
color += 3;
}
//color += bmpdata.Stride - bmpdata.Width * 3;
}
}
bmp.UnlockBits(bmpdata);
return buffer;
}
/// <summary>
/// 图像象素数组转成位图
/// </summary>
/// <param name="buffer"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public Bitmap ConvertRGBByteArrayToBitmap(byte[] buffer, int width, int height)
{
// 申请目标位图的变量,并将其内存区域锁定
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);//创建新图像
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bmpData = bmp.LockBits(rect,ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb);
//// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
Marshal.Copy(buffer, 0, bmpData.Scan0, buffer.Length);
bmp.UnlockBits(bmpData); // 解锁内存区域
return bmp;
}
}
}
方法二以VC++实现
BOOL ScreenShot(HWND hWnd, TCHAR* fileName)
{
HRESULT hr;
IDirect3D9* gpD3D=NULL;
IDirect3DDevice9* gpd3dDevice=NULL;
IDirect3DSurface9* gpSurface=NULL;
D3DDISPLAYMODE ddm;
D3DPRESENT_PARAMETERS d3dpp;
if((gpD3D=Direct3DCreate9(D3D_SDK_VERSION))==NULL)
{
ErrorMessage("Unable to Create Direct3D ");
return E_FAIL;
}
if(FAILED(gpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&ddm)))
{
ErrorMessage("Unable to Get Adapter Display Mode");
return E_FAIL;
}
ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed=WINDOW_MODE;
d3dpp.Flags=D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
d3dpp.BackBufferFormat=ddm.Format;
d3dpp.BackBufferHeight=nDisplayHeight=gScreenRect.bottom =ddm.Height;
d3dpp.BackBufferWidth=nDisplayWidth=gScreenRect.right =ddm.Width;
d3dpp.MultiSampleType=D3DMULTISAMPLE_NONE;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow=hWnd;
d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT;
if(FAILED(gpD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING ,&d3dpp,&gpd3dDevice)))
{
ErrorMessage("Unable to Create Device");
return E_FAIL;
}
if(FAILED(gpd3dDevice->CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &gpSurface, NULL)))
{
ErrorMessage("Unable to Create Surface");
return E_FAIL;
}
if (FAILED(hr = gpd3dDevice->GetFrontBufferData(0, gpSurface)))
{
gpSurface->Release() ;
return hr ;
}
hr = D3DXSaveSurfaceToFile(fileName, D3DXIFF_BMP, gpSurface, NULL, NULL);
gpSurface->Release() ;
return hr ;
}
最主要的方法有两种,一、调用windows GDI32 API函数。二、使用DirectX9.0来实现。
另外,光注了一下Microsoft Expression Encoder 4 Screen Capture这个微软新出的功能,Expression Encoder 4 可实现屏幕录制,录制 文件格式为WMV ,为免费使用版本,Expression Encoder 4 Pro为 收费版本。
还 看了一下基于windows图形驱动技术的屏幕截图方法 ,文章链接地址:http://blog.csdn.net/jia162/article/details/2509974。实现起来可能比较困难与复杂。没找到实例参考及技术实现的算法,因此也就没有深入研究。
下面两种方法
一、GDI32 API截图,600*480大小生成Bitmap位图大概需要45ms左右,生成位图并save成bmp文件需要大概110ms左右,图片越大,耗费的时间越长,效率比较低。
二、DirectX截图,是把整个屏幕的拷贝到内存里,再进行截取,执行拷贝屏幕的方法g_pd3dDevice->GetFrontBufferData(0, g_pSurface)需要80ms-100ms,效率也比较低。
若那位技术牛人有好滴方法,请推荐一下哦!!!
方法一实现:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace DataFrameFetch
{
public class WinGdi32Api
{
[DllImport("GDI32.dll")]
public static extern bool BitBlt(int hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, int hdcSrc, int nXSrc, int nYSrc, int dwRop);
[DllImport("GDI32.dll")]
public static extern int CreateCompatibleBitmap(int hdc, int nWidth, int nHeight);
[DllImport("GDI32.dll")]
public static extern int CreateCompatibleDC(int hdc);
[DllImport("GDI32.dll")]
public static extern bool DeleteDC(int hdc);
[DllImport("GDI32.dll")]
public static extern bool DeleteObject(int hObject);
[DllImport("GDI32.dll")]
public static extern int GetDeviceCaps(int hdc, int nIndex);
[DllImport("GDI32.dll")]
public static extern int SelectObject(int hdc, int hgdiobj);
[DllImport("User32.dll")]
public static extern int GetDesktopWindow();
[DllImport("User32.dll")]
public static extern int GetWindowDC(int hWnd);
[DllImport("User32.dll")]
public static extern int GetDC(int hWnd);
[DllImport("User32.dll")]
public static extern int ReleaseDC(int hWnd, int hDC);
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace DataFrameFetch
{
public class DataFetch
{
public DataFetch()
{ }
/// <summary>
/// 全屏截图
/// </summary>
/// <returns></returns>
public Bitmap CaptureScreen()
{
DateTime dt_start = DateTime.Now;
int source = WinGdi32Api.GetWindowDC(WinGdi32Api.GetDesktopWindow());
int bitmap = WinGdi32Api.CreateCompatibleBitmap(source, WinGdi32Api.GetDeviceCaps(source,, WinGdi32Api.GetDeviceCaps(source, 10));
int destination = WinGdi32Api.CreateCompatibleDC(source);
WinGdi32Api.SelectObject(destination, bitmap);
WinGdi32Api.BitBlt(destination, 0, 0, WinGdi32Api.GetDeviceCaps(source,, WinGdi32Api.GetDeviceCaps(source, 10), source, 0, 0, 0x00CC0020);
Bitmap img = this.GetBitmap(bitmap);
this.Cleanup(bitmap, source, destination);
DateTime dt_end = DateTime.Now;
TimeSpan ts = dt_end - dt_start;
System.Diagnostics.Debug.WriteLine(ts.Milliseconds.ToString());
return img;
}
public Bitmap CaptureScreen(Control control)
{
DateTime dt_start = DateTime.Now;
int source = WinGdi32Api.GetDC(control.Handle.ToInt32());
int bitmap = WinGdi32Api.CreateCompatibleBitmap(source, control.Width,control.Height);
int destination = WinGdi32Api.CreateCompatibleDC(source);
WinGdi32Api.SelectObject(destination, bitmap);
WinGdi32Api.BitBlt(destination, 0, 0, control.Width, control.Height, source, 0, 0, 0x00CC0020);
Bitmap img = this.GetBitmap(bitmap);
this.Cleanup(bitmap, source, destination);
byte[] buffer = this.ConvertBitmapToRGBByteArray(img);
string filename = "F:\\img\\" + dt_start.ToString("yyyyMMddHHmmss") + dt_start.Millisecond.ToString();
FileStream fs = new FileStream(filename + ".olc", FileMode.Create);
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
fs.Close();
//Bitmap bmp = this.FromRGB(buffer, img.Width, img.Height);
//bmp.Save(filename + "_1.bmp");
DateTime dt_end = DateTime.Now;
TimeSpan ts = dt_end - dt_start;
System.Diagnostics.Debug.WriteLine(ts.Milliseconds.ToString());
return img;
}
private void Cleanup(int bitmap, int source, int destination)
{
WinGdi32Api.ReleaseDC(WinGdi32Api.GetDesktopWindow(), source);
WinGdi32Api.DeleteDC(destination);
WinGdi32Api.DeleteObject(bitmap);
}
private Bitmap GetBitmap(int hbitmap)
{
Bitmap bmp = new Bitmap(Image.FromHbitmap(new IntPtr(hbitmap)), Image.FromHbitmap(new IntPtr(hbitmap)).Width, Image.FromHbitmap(new IntPtr(hbitmap)).Height);
return bmp;
}
/// <summary>
/// 将位图转换成数组
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
private byte[] ConvertBitmapToRGBByteArray(Bitmap bmp)
{
Rectangle rect = new Rectangle(new Point(0,0),bmp.Size);
BitmapData bmpdata = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int len = bmp.Width * bmp.Height * 3;
byte[] buffer = new byte[len];
int index = 0;
unsafe
{
byte* color = (byte*)bmpdata.Scan0;
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
buffer[index++] = *color;
buffer[index++] = *(color + 1);
buffer[index++] = *(color + 2);
color += 3;
}
//color += bmpdata.Stride - bmpdata.Width * 3;
}
}
bmp.UnlockBits(bmpdata);
return buffer;
}
/// <summary>
/// 图像象素数组转成位图
/// </summary>
/// <param name="buffer"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public Bitmap ConvertRGBByteArrayToBitmap(byte[] buffer, int width, int height)
{
// 申请目标位图的变量,并将其内存区域锁定
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);//创建新图像
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bmpData = bmp.LockBits(rect,ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb);
//// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
Marshal.Copy(buffer, 0, bmpData.Scan0, buffer.Length);
bmp.UnlockBits(bmpData); // 解锁内存区域
return bmp;
}
}
}
方法二以VC++实现
BOOL ScreenShot(HWND hWnd, TCHAR* fileName)
{
HRESULT hr;
IDirect3D9* gpD3D=NULL;
IDirect3DDevice9* gpd3dDevice=NULL;
IDirect3DSurface9* gpSurface=NULL;
D3DDISPLAYMODE ddm;
D3DPRESENT_PARAMETERS d3dpp;
if((gpD3D=Direct3DCreate9(D3D_SDK_VERSION))==NULL)
{
ErrorMessage("Unable to Create Direct3D ");
return E_FAIL;
}
if(FAILED(gpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&ddm)))
{
ErrorMessage("Unable to Get Adapter Display Mode");
return E_FAIL;
}
ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed=WINDOW_MODE;
d3dpp.Flags=D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
d3dpp.BackBufferFormat=ddm.Format;
d3dpp.BackBufferHeight=nDisplayHeight=gScreenRect.bottom =ddm.Height;
d3dpp.BackBufferWidth=nDisplayWidth=gScreenRect.right =ddm.Width;
d3dpp.MultiSampleType=D3DMULTISAMPLE_NONE;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow=hWnd;
d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT;
if(FAILED(gpD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING ,&d3dpp,&gpd3dDevice)))
{
ErrorMessage("Unable to Create Device");
return E_FAIL;
}
if(FAILED(gpd3dDevice->CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &gpSurface, NULL)))
{
ErrorMessage("Unable to Create Surface");
return E_FAIL;
}
if (FAILED(hr = gpd3dDevice->GetFrontBufferData(0, gpSurface)))
{
gpSurface->Release() ;
return hr ;
}
hr = D3DXSaveSurfaceToFile(fileName, D3DXIFF_BMP, gpSurface, NULL, NULL);
gpSurface->Release() ;
return hr ;
}
相关推荐
学习GDI+ 3D文字实例,可以直接运行
纯 GDI+ 实现3D照相机视角变换 材质贴图等等,绝对物有所值
GDI+简单屏幕监控客户端
GDI截屏和DX截屏,保存到BMP文件
使用GDI实现的3D模型数据的解析并渲染显示的演示程序。
论GDI与DirectDraw的关系,DirectDraw开发者或学习者必顺清楚了解的内容。
GDI+简单屏幕监控服务端
VC2015实现简单的整个屏幕截图保存为jpg, 然后再该图片上根据需要裁剪特定大小的图片在另存jpg, 以及其他范例。
gdi+实现截屏保存png文件,鼠标钩子,直接截图 可选定保存特定矩形区域或整个屏幕或活动窗口; 注意全局鼠标钩子的使用 gdi画线在mousemove事件里的擦除 代码不完善仅供参考,欢迎修改!
使用GDI+,完美实现符合商业需要的3D效果的饼状图
MFC中GDI与Direct2D交互_D2D1_例子
C# GDI+ 水晶3D饼图源码,C#2005做的,下了运行一下看看效果再看看源码,很不错的。用来学习C# GDI+编程是很不错的实例
完美3D饼状图,可以设置大小,输出位置等多想功能。
我参考ffmepg截屏部分的源码,在Windows上用的是gdi bitblt截屏,这完全可以自己用gdi实现一个截屏模块。最终实现如下,整个功能模块只依赖于Windows头文件,效果与ffmpeg基本一致,如果不是追求极致性能(mirro ...
这个是GDI的多屏幕扩展抓屏,下面这篇文章讲的是DXGI,欢迎下载https://blog.csdn.net/xjb2006/article/details/107079312
本工程包含了使用各种API(Direct3D,OpenGL,GDI,DirectSound,SDL2)播放多媒体例子。 其中音频输入为PCM采样数据。输出至系统的声卡播放出来。 视频输入为YUV/RGB像素数据。输出至显示器上的一个窗口播放出来。 ...
《Windows Graphics Programming Win32 GDI and DirectDraw》配套源码
袁峰的书 Feng Yuan,这里重新上传一次。原先chm格式的文件下载后打不开,这实在和你我的人品都没有关系;CSDN太烂的缘故,又不能更新原文件。
C# GDI+写的5个小练习,各种GDI基本操作,仿QQ屏幕截图,有详细的代码说明,非常适合初学C#GDI的人练习参考。
易语言GDI调节屏幕亮度源码,GDI调节屏幕亮度,窗口不可关闭,子程序1,API_置窗口位置,SetWindowLongA,CallWindowProcA,API_隐藏窗口