标题: 【汉化资料】屏幕监控编程 [打印本页] 作者: shane007 时间: 2009-5-28 00:02 标题: 【汉化资料】屏幕监控编程 原文: z+ U5 l" Y1 Z http://www.dklkt.cn/article.asp?id=114 / b) P9 A# k+ f! f$ N2 B0 y . ]2 W- q X: h% n最近花了点时间研究这个。写写收获吧。+ Q4 a; v8 ]# k7 g
# N, @' Y# j, B& G( _" S e首先要说的是,要实现屏幕的捕捉,目前有以下几种方法。; v1 U# S s+ q! t! t
9 E- S4 k2 I! ?" u
1。使用GDI函数或者Windows Media API函数5 N* ~2 n& l) [. x+ a
2。使用DirectX技术9 N. I l& C6 D7 T% M0 f
3。使用api hook技术5 N- G K a. J% Y# Y5 e5 B
4。使用图形驱动技术 , \' E' R6 |# F( W' u0 ]4 |* O6 C5 F$ y3 y+ [
关于实现,这里有一篇老外的文章,来自codeproject,可以看一下。 : }0 v3 ~& s$ }+ E1 e! |引用 6 p; A$ X" \8 k- @Various methods for capturing the screen$ A B. g' `& S8 _; E( [
By Gopalakrishna Palem& X$ w+ O; n7 I+ K6 R# u
# n, e5 h7 k2 K
Contents 1 H: H2 S8 Q& g3 w' g3 L+ f( \Introduction , f, k6 h/ J1 c) o" d
Capture it the GDI way ' p4 `* _- q( H, r: L2 D; dAnd the DirectX way of doing it / F# p- ~# k# ?2 O1 i6 u
Capturing the screen with Windows Media API Y+ `! z+ O, ^! F* `3 ]
Introduction % Q( S* ]2 M: A/ Z0 Z- Z; B) p - g. K9 v, A* W$ GSome times, we want to capture the contents of the entire screen programmatically. The following explains how it can be done. Typically, the immediate options we have, among others, are using GDI and/or DirectX. Another option that is worth considering is Windows Media API. Here, we would consider each of them and see how they can be used for our purpose. In each of these approaches, once we get the screenshot into our application defined memory or bitmap, we can use it in generating a movie. Refer to the article Create Movie From HBitmap for more details about creating movies from bitmap sequences programmatically. 8 _- T, D$ O( c0 B# F* PCapture it the GDI way , f4 O& d9 ?& P. T8 a0 }, u& @2 j! K/ K+ S, o3 |: D
When performance is not an issue and when all that we want is just a snapshot of the desktop, we can consider the GDI option. This mechanism is based on the simple principle that the desktop is also a window - that is it has a window Handle (HWND) and a device context (DC). If we can get the device context of the desktop to be captured, we can just blit those contents to our application defined device context in the normal way. And getting the device context of the desktop is pretty straightforward if we know its window handle - which can be achieved through the function GetDesktopWindow(). Thus, the steps involved are: 7 F; {4 P" `4 n( t1 N4 BAcquire the Desktop window handle using the function GetDesktopWindow(); % S$ p1 ]0 ]* |4 x- u
Get the DC of the desktop window using the function GetDC(); 9 Q* R$ A9 _8 ]8 C
Create a compatible DC for the Desktop DC and a compatible bitmap to select into that compatible DC. These can be done using CreateCompatibleDC() and CreateCompatibleBitmap(); selecting the bitmap into our DC can be done with SelectObject(); 7 Z1 T5 O2 t: n2 b+ |9 z; SWhenever you are ready to capture the screen, just blit the contents of the Desktop DC into the created compatible DC - that's all - you are done. The compatible bitmap we created now contains the contents of the screen at the moment of the capture. 8 m6 J1 S/ L. f9 N9 c j2 p7 b, s! VDo not forget to release the objects when you are done. Memory is precious (for the other applications). & t( O( ?1 h$ V$ |& E8 z3 O t5 H
Example5 |! s1 w; j. d1 d0 j; I: \& T
Void CaptureScreen()% k$ I" P, I5 D: G) S' D
{# M& Q" e& a; N5 T( v0 H, \* H
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN); , j' L% t3 {& j! J8 V# W( e int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);% f* r+ A2 F4 B( V2 P
HWND hDesktopWnd = GetDesktopWindow(); & A3 Z9 B. r/ f9 c! L) I HDC hDesktopDC = GetDC(hDesktopWnd);3 \" \* h" v' y7 Q# W
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);! M, U6 y1 r0 Z% V# |6 _! }$ A
HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC, j- p; }0 q: z/ A p" q% [ nScreenWidth, nScreenHeight); * }/ Z3 |, M* \5 G% S SelectObject(hCaptureDC,hCaptureBitmap); 1 `7 D! e1 a. J% r BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight," [) }! x8 f' j1 Y
hDesktopDC,0,0,SRCCOPY|CAPTUREBLT); $ }2 f- `3 i: P$ `" y
SaveCapturedBitmap(hCaptureBitmap); //Place holder - Put your code( `3 g% n3 Y7 o* S3 r) W9 ^
/ |# ~ n$ ^/ k( w. J/ h# I' I3 n //here to save the captured image to disk 8 r: S$ E& `' e& `1 m; v5 d! c3 {6 z/ R& x* U' V3 [
ReleaseDC(hDesktopWnd,hDesktopDC);' @9 U, i, w1 J
DeleteDC(hCaptureDC); 5 m* `+ z+ k& e/ S DeleteObject(hCaptureBitmap); : l) t) p4 r Z} 9 z- R; s' \0 }4 ?% D C% u ; p: ^0 _( |+ |% T' SIn the above code snippet, the function GetSystemMetrics() returns the screen width when used with SM_CXSCREEN, and returns the screen height when called with SM_CYSCREEN. Refer to the accompanying source code for details of how to save the captured bitmap to the disk and how to send it to the clipboard. Its pretty straightforward. The source code implements the above technique for capturing the screen contents at regular intervals, and creates a movie out of the captured image sequences. ! {# y X e+ \3 A+ e' AAnd the DirectX way of doing it- @, J' B1 `1 i' U, j
2 S& t/ k+ R7 O& J' [0 y: R" ZCapturing the screenshot with DirectX is a pretty easy task. DirectX offers a neat way of doing this. ' s# ]2 l: C) F9 ~7 w + y+ B7 P8 `% }Every DirectX application contains what we call a buffer, or a surface to hold the contents of the video memory related to that application. This is called the back buffer of the application. Some applications might have more than one back buffer. And there is another buffer that every application can access by default - the front buffer. This one, the front buffer, holds the video memory related to the desktop contents, and so essentially is the screen image.6 Z) ?5 X& K# J( ]% B: S) H" }
0 z' ~* X% v5 R' e+ r$ @
By accessing the front buffer from our DirectX application, we can capture the contents of the screen at that moment. 9 y$ K, f8 Y$ p: S. U& e9 ^5 h$ n$ c k9 D1 D; g. o, A+ t) E8 ]8 U% `
Accessing the front buffer from the DirectX application is pretty easy and straightforward. The interface IDirect3DDevice9 provides the GetFrontBufferData() method that takes a IDirect3DSurface9 object pointer and copies the contents of the front buffer onto that surface. The IDirect3DSurfce9 object can be generated by using the method IDirect3DDevice8::CreateOffscreenPlainSurface(). Once the screen is captured onto the surface, we can use the function D3DXSaveSurfaceToFile() to save the surface directly to the disk in bitmap format. Thus, the code to capture the screen looks as follows:0 D5 x1 x& l- Z6 B9 Q* s* F) ]
extern IDirect3DDevice9* g_pd3dDevice; / [& Z! `5 l. |& {& `* y3 E: iVoid CaptureScreen() - S1 j {8 a/ |# f/ W/ f2 {{2 E$ ^ L: k1 {0 X# W+ U p
IDirect3DSurface9* pSurface;5 ?8 a6 T8 }$ M# S( h( k2 e' K) i- ]2 \
g_pd3dDevice->CreateOffscreenPlainSurface(ScreenWidth, ScreenHeight, 1 Y& `0 ]+ D. U# V D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL);6 |3 h- {7 `' L
g_pd3dDevice->GetFrontBufferData(0, pSurface); # \* g) n- z+ N2 C5 L2 A8 z3 b D3DXSaveSurfaceToFile("Desktop.bmp",D3DXIFF_BMP,pSurface,NULL,NULL);+ `9 ?/ x7 ~' Z) z, a) O1 p
pSurface->Release(); 2 \2 I# d- |8 W& c) Y
} & \9 w9 V6 ?& ^- f6 N3 l2 r) Z; @4 _7 b% I5 A3 |, R
In the above, g_pd3dDevice is an IDirect3DDevice9 object, and has been assumed to be properly initialized. This code snippet saves the captured image onto the disk directly. However, instead of saving to disk, if we just want to operate on the image bits directly - we can do so by using the method IDirect3DSurface9::LockRect(). This gives a pointer to the surface memory - which is essentially a pointer to the bits of the captured image. We can copy the bits to our application defined memory and can operate on them. The following code snippet presents how the surface contents can be copied into our application defined memory: t9 W+ Z6 ~! Pextern void* pBits; , ^% u4 L2 ?0 k+ P5 D2 O" s% L$ |extern IDirect3DDevice9* g_pd3dDevice;3 }. m3 t# S- f( Q0 _1 J0 c
IDirect3DSurface9* pSurface;7 D% b; \- p% z- l
g_pd3dDevice->CreateOffscreenPlainSurface(ScreenWidth, ScreenHeight, 9 }+ m" K: i& i0 z) [4 M D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, : f% K% s3 J. I- ~' u
&pSurface, NULL);% s: G& J1 A0 z/ Q
g_pd3dDevice->GetFrontBufferData(0, pSurface); : [% o( D9 C1 k. i+ Q# yD3DLOCKED_RECT lockedRect;! B# l L" f- E1 s- h1 o
pSurface->LockRect(&lockedRect,NULL, " {3 j3 n+ @& m* I M D3DLOCK_NO_DIRTY_UPDATE| 2 A6 c8 _0 L- F9 y D3DLOCK_NOSYSLOCK|D3DLOCK_READONLY))); / _/ d2 V& E( Cfor( int i=0 ; i < ScreenHeight ; i++) # ]/ {( u. U m% T{2 E2 g0 _; z+ ^6 ~5 }" ^4 h4 P* K
memcpy( (BYTE*) pBits + i * ScreenWidth * BITSPERPIXEL / 8 , " y9 A6 k5 Q' O$ T. | ]
(BYTE*) lockedRect.pBits + i* lockedRect.Pitch , % a, ]$ o4 `$ |+ X& q5 [7 d
ScreenWidth * BITSPERPIXEL / 8);# R% G+ v* s* @4 `6 \
} f5 G9 ~- f& i- j4 B9 {6 I4 @, e
g_pSurface->UnlockRect();4 \1 M. d! o( S& e. x
pSurface->Release(); ( C: T% }5 p" h' |, @' ] : l G [* D5 ^. {% Z- uIn the above, pBits is a void*. Make sure that we have allocated enough memory before copying into pBits. A typical value for BITSPERPIXEL is 32 bits per pixel. However, it may vary depending on your current monitor settings. The important point to note here is that the width of the surface is not same as the captured screen image width. Because of the issues involved in the memory alignment (memory aligned to word boundaries are assumed to be accessed faster compared to non aligned memory), the surface might have added additional stuff at the end of each row to make them perfectly aligned to the word boundaries. The lockedRect.Pitch gives us the number of bytes between the starting points of two successive rows. That is, to advance to the correct point on the next row, we should advance by Pitch, not by Width. You can copy the surface bits in reverse, using the following:) m3 a' X: F+ M; [5 E# X
for( int i=0 ; i < ScreenHeight ; i++)9 A$ @- `: p" s R/ q7 T* o# L
{ + w, C( K% P5 `+ M memcpy((BYTE*) pBits +( ScreenHeight - i - 1) * . v B7 [* F0 `9 J& X- L( S1 ~& d
ScreenWidth * BITSPERPIXEL/8 , / m( B' C. s9 z% ]. l
(BYTE*) lockedRect.pBits + i* lockedRect.Pitch , 3 }8 ?: Y, u7 c" c' R ScreenWidth* BITSPERPIXEL/8);5 |- d) z! ~8 C
} 0 b& Q. v$ e0 B& O7 C5 I7 j7 w, d) z! Q5 ?5 {
This may come handy when you are converting between top-down and bottom-up bitmaps.6 v2 \+ n5 y! f7 G r% D2 U
9 q3 h* F9 A) Z1 F& i
While the above technique of LockRect() is one way of accessing the captured image content on IDirect3DSurface9, we have another more sophisticated method defined for IDirect3DSurface9, the GetDC() method. We can use the IDirect3DSurface9::GetDC() method to get a GDI compatible device context for the DirectX image surface, which makes it possible to directly blit the surface contents to our application defined DC. Interested readers can explore this alternative.7 m3 r5 @- a m& m' ]
. [2 q0 n+ g8 R7 p# }6 A
The sample source code provided with this article implements the technique of copying the contents of an off-screen plain surface onto a user created bitmap for capturing the screen contents at regular intervals, and creates a movie out of the captured image sequences. 0 h9 g5 p- B! z+ k9 X/ C# Y8 d5 y M+ b8 i8 n, |& `7 S
However, a point worth noting when using this technique for screen capture is the caution mentioned in the documentation: The GetFrontBufferData() is a slow operation by design, and should not be considered for use in performance-critical applications. Thus, the GDI approach is preferable over the DirectX approach in such cases. # B0 H& J Q: `/ ~1 ?. vWindows Media API for capturing the screen/ J& n6 w! h0 k! p a" P
6 Z1 F0 ~- ^& y
Windows Media 9.0 supports screen captures using the Windows Media Encoder 9 API. It includes a codec named Windows Media Video 9 Screen codec that has been specially optimized to operate on the content produced through screen captures. The Windows Media Encoder API provides the interface IWMEncoder2 which can be used to capture the screen content efficiently. 0 s8 z9 z: r2 I# i. y& l$ Q5 P+ P1 O' j8 C5 K( p' {
Working with the Windows Media Encoder API for screen captures is pretty straightforward. First, we need to start with the creation of an IWMEncoder2 object by using the CoCreateInstance() function. This can be done as:6 j$ D2 H4 @& X' X
IWMEncoder2* g_pEncoder=NULL; ' _) K" R3 \; c& e
CoCreateInstance(CLSID_WMEncoder,NULL,CLSCTX_INPROC_SERVER,: \& i [: \! @/ r+ ^$ W# l
IID_IWMEncoder2,(void**)&g_pEncoder);. y- \7 u- ~! a3 k- _( o7 J9 x" g8 t
/ m" e* u4 j9 `7 ]: S$ q3 T4 G
The Encoder object thus created contains all the operations for working with the captured screen data. However, in order to perform its operations properly, the encoder object depends on the settings defined in what is called a profile. A profile is nothing but a file containing all the settings that control the encoding operations. We can also create custom profiles at runtime with various customized options, such as codec options etc., depending on the nature of the captured data. To use a profile with our screen capture application, we create a custom profile based on the Windows Media Video 9 Screen codec. Custom profile objects have been supported with the interface IWMEncProfile2. We can create a custom profile object by using the CoCreateInstance() function as: 8 z/ e H* @$ x/ R4 EIWMEncProfile2* g_pProfile=NULL; ' ]% k0 g9 P) m& Q. hCoCreateInstance(CLSID_WMEncProfile2,NULL,CLSCTX_INPROC_SERVER," o7 a/ u5 h$ k5 m8 [3 U
IID_IWMEncProfile2,(void**)&g_pProfile); J8 a, u6 L! ~, p, |$ U, V+ n- M2 I8 p0 ^3 Z1 K
We need to specify the target audience for the encoder in the profile. Each profile can hold multiple number of audience configurations, which are objects of the interface IWMEncAudienceObj. Here, we use one audience object for our profile. We create the audience object for our profile by using the method IWMEncProfile::AddAudience(), which would return a pointer to IWMEncAudienceObj which can then be used for configurations such as video codec settings (IWMEncAudienceObj::put_VideoCodec()), video frame size settings (IWMEncAudienceObj::put_VideoHeight() and IWMEncAudienceObj::put_VideoWidth()) etc. For example, we set the video codec to be Windows Media Video 9 Screen codec as:+ \% K7 s' j" G0 {4 L! h7 y& j
extern IWMEncAudienceObj* pAudience; 8 R- Z) V7 H+ T: L% z8 q5 O6 }#define VIDEOCODEC MAKEFOURCC('M','S','S','2') 0 W- x# ` {& L ~- Q/ q
//MSS2 is the fourcc for the screen codec+ O! n' j! A, l I
& h" n/ g- L; X# j2 x( I
long lCodecIndex=-1; H$ Z I; n8 _9 T" sg_pProfile->GetCodecIndexFromFourCC(WMENC_VIDEO,VIDEOCODEC,3 N0 O+ u( d* f% N" I* `
&lCodecIndex); //Get the Index of the Codec % B2 i B! V0 W; Q7 N / }& N9 z1 v* d& M! q( R6 _8 b XpAudience->put_VideoCodec(0,lCodecIndex);# J. |7 Y( [' t9 H J
; m$ j4 o% H) D! b/ g0 }+ U: N0 i
The fourcc is a kind of unique identifier for each codec in the world. The fourcc for the Windows Media Video 9 Screen codec is MSS2. The IWMEncAudienceObj::put_VideoCodec() accepts the profile index as the input to recognize a particular profile - which can be obtained by using the method IWMEncProfile::GetCodecIndexFromFourCC().: L- x- {2 e% K8 \% D* _9 D! _) r! W& J
! m& ]* V7 J( E3 yOnce we have completed configuring the profile object, we can choose that profile into our encoder by using the method IWMEncSourceGroup :: put_Profile() which is defined on the source group objects of the encoder. A source group is a collection of sources where each source might be a video stream or audio stream or HTML stream etc. Each encoder object can work with many source groups from which it get the input data. Since our screen capture application uses only a video stream, our encoder object need to have one source group with a single source, the video source, in it. This single video source needs to configured to use the Screen Device as the input source, which can be done by using the method IWMEncVideoSource2::SetInput(BSTR) as: 9 v5 u9 \2 T$ \, h& X5 Y: Gextern IWMEncVideoSource2* pSrcVid;; U4 }4 \- g, n8 V( z' P
pSrcVid->SetInput(CComBSTR("ScreenCap://ScreenCapture1"); 9 l- Q) Z6 `/ p# l& b0 {% w% u & Q- n! Z2 E1 F6 u- \The destination output can be configured to save into a video file (wmv movie) by using the method IWMEncFile::put_LocalFileName() which requires an IWMEncFile object. This IWMEncFile object can be obtained by using the method IWMEncoder::get_File() as:! O% R) P+ ^7 n' J
IWMEncFile* pOutFile=NULL;. A/ E8 P" \+ \/ J
g_pEncoder->get_File(&pOutFile); % G0 I W- L* NpOutFile->put_LocalFileName(CComBSTR(szOutputFileName);% W+ t+ u. t& R0 {8 z! X; ]8 ^. B+ M
6 T3 v: k8 g/ C8 H) ^$ U/ K3 c" ~
Now, once all the necessary configurations have been done on the encoder object, we can use the method IWMEncoder::Start() to start capturing the screen. The methods IWMEncoder::Stop() and IWMEncoder::Pause might be used for stopping and pausing the capture. - Z5 o; a+ \7 V8 | w F) d+ Q9 r. \; _1 M( B
While this deals with full screen capture, we can alternately select the regions of capture by adjusting the properties of input video source stream. For this, we need to use the IPropertyBag interface of the IWmEnVideoSource2 object as: $ S2 W+ o5 P0 Q4 I#define WMSCRNCAP_WINDOWLEFT CComBSTR("Left") # r: n! _& }8 r- \0 }% {% l+ v#define WMSCRNCAP_WINDOWTOP CComBSTR("Top") ; M8 ~1 @' k7 s; [, d#define WMSCRNCAP_WINDOWRIGHT CComBSTR("Right")2 D4 n/ @! W; d; D2 p5 {* b- | }% p
#define WMSCRNCAP_WINDOWBOTTOM CComBSTR("Bottom") & [! g Y( Q& I& r#define WMSCRNCAP_FLASHRECT CComBSTR("FlashRect")$ b* ~; a' M4 U' J0 z
#define WMSCRNCAP_ENTIRESCREEN CComBSTR("Screen") : S# t. j- J4 J6 y3 ?#define WMSCRNCAP_WINDOWTITLE CComBSTR("WindowTitle") ( N+ V. ^$ U- [- w. mextern IWMEncVideoSource2* pSrcVid; / E, i& j0 s8 {" X0 Z7 t& e0 H$ n: `int nLeft, nRight, nTop, nBottom; . ^- R1 g, \: N8 ypSrcVid->QueryInterface(IID_IPropertyBag,(void**)&pPropertyBag);' W+ X. w; `1 P; M9 `: v
CComVariant varValue = false;$ I3 R3 M( q+ F/ M
pPropertyBag->Write(WMSCRNCAP_ENTIRESCREEN,&varValue);1 v" r! [6 M0 B" j7 U
varValue = nLeft;! e" b6 V* K) M& |, m
pPropertyBag->Write( WMSCRNCAP_WINDOWLEFT, &varValue ); T X' U" }3 Y. _9 C7 Q; jvarValue = nRight;) l; G1 D2 I/ }& Y+ {! A
pPropertyBag->Write( WMSCRNCAP_WINDOWRIGHT, &varValue ); ; @; e) x: X7 K7 NvarValue = nTop; # G- S6 N9 J( R0 o' G9 [ WpPropertyBag->Write( WMSCRNCAP_WINDOWTOP, &varValue );" `, x9 C0 v2 b
varValue = nBottom; ' J7 M( w8 A, ]4 O5 ]( wpPropertyBag->Write( WMSCRNCAP_WINDOWBOTTOM, &varValue ); 8 Y% ]$ m. f/ ]4 N# z# zhttp://www.codeproject.com/KB/dialog/screencap.aspx! J+ i8 U" i i3 O2 V5 D
4 B5 J+ ^& E. J _6 J7 nThe accompanied source code implements this technique for capturing the screen. One point that might be interesting, apart from the nice quality of the produced output movie, is that in this, the mouse cursor is also captured. (By default, GDI and DirectX are unlikely to capture the mouse cursor).8 O0 y, d2 \2 x5 B; |% W, ]
9 t6 v& o& ]2 t' X- M
Note that your system needs to be installed with Windows Media 9.0 SDK components to create applications using the Window Media 9.0 API. 6 s3 @; c3 w/ c! i, c" J' l ! H0 R) u" U# m# y0 D& OTo run your applications, end users must install the Windows Media Encoder 9 Series. When you distribute applications based on the Windows Media Encoder SDK, you must also include the Windows Media Encoder software, either by redistributing Windows Media Encoder in your setup, or by requiring your users to install Windows Media Encoder themselves. 1 a& }4 {5 e3 A& s/ Q3 B ! Y+ S4 f8 {* K8 @, [8 Q! M/ \The Windows Media Encoder 9.0 can be downloaded from: + H% o8 s. H0 x6 n/ Y AWindows Media Encoder 2 G) u4 M9 B+ F& QConclusion/ W+ C, C' r% e! v# P
% T3 r- |! ?8 }1 j1 M5 A. bAll the variety of techniques discussed above are aimed at a single goal - capturing the contents of the screen. However, as can be guessed easily, the results vary depending upon the particular technique that is being employed in the program. If all that we want is just a random snapshot occasionally, the GDI approach is a good choice, given its simplicity. However, using Windows Media would be a better option if we want more professional results. One point worth noting is, the quality of the content captured through these mechanisms might depend on the settings of the system. For example, disabling hardware acceleration (Desktop properties | Settings | Advanced | Troubleshoot) might drastically improve the overall quality and performance of the capture application.$ q* N8 z( Q) V) y2 G
关于API HOOK和图形驱动Mirror Driver,可以看看这篇文章: / x$ f: u& x# ^' F7 q, @5 @4 h) D引用 3 n# g) G+ C% @/ n; x3 z. @http://www.cnblogs.com/niukun/archive/2008/03/08/1096601.html3 M1 }) F9 B M
' x: s+ b8 n; _6 S0 t( Z- X2 ]( S; `
屏幕录制,远程桌面传输,基于Windows图形驱动的屏幕截图技术 + k" g4 T, B4 R
9 i _5 ~+ m+ W+ X$ Z) t/ J
: U9 m' P% t* h' m. n6 @8 l' q/ F1截屏技术3 K: u5 D2 z- X3 `6 d
& n6 l+ q! c4 P1 u
1.1使用api hook技术 2 w6 Q; }8 e; ?5 g& o $ t% X0 ~4 o6 H2 @# B0 a 使用api hook技术截屏基于一下的原理;多数屏幕图形的绘制都是通过调用用户态gdi32.dll中的绘图函数实现的,如果利用api hook技术拦截系统中所有对这些函数的调用,就可以得到屏幕图形刷新或变化的区域坐标;然后使用api函数bitblt将刷新或者变化后的屏幕区域的ddb格式的位图拷贝到内存中,接着使用函数getbits将ddb位图转换为dbi位图,最后压缩、存储或者传输。+ ?5 E2 d6 D( h6 u) S
$ d$ x4 V+ [" `* `/ G- Q, z# X 这种方案只有捕捉到变化,才进行截屏。这样每次截屏都是有效的操作。每次(第一次除外)仅截取了栓新或变化部分,从根本上解决了数据量大的问题。但是这种技术仍然有一下缺点:1实际截屏采用的api函数,截取的是ddb位图,要经过一次格式转换,耗时较大。2如果屏幕变化区域矩形的坐标r1、r2、……rn相继到达,为了不是屏幕变化的信息丢失,通常不是逐个截取,而是将矩形坐标合并,这样就可以截取并未变化的区域,不经增加截屏的时间消耗,而且产生冗余数据。3该技术不支持directdraw技术,由于应用程序可能使用directdraw驱动程序进行直接操纵显示内存、硬件位块转移,硬件重叠和交换表面等图形操作,而不必进行gdi调用,所以此时api hook技术将失去效用,不能捕捉屏幕变化。4api hook技术在屏幕取词,远程控制和多媒体教学中都有实际的应用,但是这种技术是一种非标准的技术,微软公司并不推荐使用。+ |1 b' \* o& u6 A
" B/ U$ N+ l$ i* p) X1.2 使用图形驱动技术 % }2 V6 M0 |' a j+ x/ R
( G, \" L' N( I7 L! O% W该技术的原理:应用程序调用win32 gdi 函数进行图形输出请求,这个请求通过核心模式gdi发送。核心模式gdi把这些请求发送到相应的图形驱动程序。如,显示器驱动程序,通信流如图。现将该技术详细解释如下: j; j9 _4 g+ S& g: t$ q+ ?, \, A; P M, D3 ^7 v0 _, a
(1)显示器驱动输出一系列设备驱动程序接口DDI(Device Driver Interface)函数供GDI调用。信息通过这些入口点的输入/输出参数在GDI和驱动程序之间传递。 / Z) H0 A: u1 O, P/ e; A% |2 U: w! c6 z
(2) 在显示器驱动加载时,GDI调用显示器驱动程序函数DrvEnableDriver,在这里我们向GDI提供显示器驱动支持的,可供GDI调用的DDI函数入口点,其中部分时将要Hook的图形输出函数。 / h% \$ Q$ a% h2 Q3 k n b : a5 |1 y- P3 o0 o. s* n# A(3) 在GDI调用函数DrvEnableDriver成功返回后,GDI调用显示器驱动的DrvEnablePDEV函数,在这里可以设置显示器的显示模式,然后创建一个PDEV结构,PDEV结构是物理显示器的逻辑表示。 * E5 o: m" t. S* e5 @0 [8 B$ {; ?
(4) 在成功创建PDEV结构之后,显示驱动为视频硬件创建一个表面,该表面可以是标准的DIB位图管理表面,然后驱动程序使该表面与一个PDEV结构相关联,这样显示驱动支持的所有绘画操作都将在该DIB位图表面上进行。4 ]# T. H D3 `- P4 p
1 x* l3 {" Y# Y. V% _0 T(2) API Hook技术在实际截屏时,采用API函数实现,截取DDB位图,必须经过一次DDB到DIB的转换;而驱动技术直接从其管理的DIB位图(表面)中将截取区域的图形数据拷贝到应用程序,显著的降低了一次截屏的时间消耗。; y5 r! M, W4 {# p S2 \
# X+ e9 `! O$ y# C' P这几天仔细看了下Gh0st屏幕传输的代码,其实隔行扫描很像是在不停的给屏幕打补丁。用一个接一个小补丁来逐渐更新屏幕的变化。其实Gh0st的隔行扫描也存在一些问题。尽管刷新的频率很高,但是屏幕在运动的时候仍然不是很流畅。可以看到明显的碎片。细看它的代码可以发现,它并不是扫描完整个屏幕再来分析的,而是扫到一个改变,就适当的扩大区域,发送数据。这样一是造成区域重复发送,二是造成大量碎片。 # U" Q# B3 n. p; p D+ v$ b/ @3 D' W. D3 r W5 M
针对这个问题,我试图改进算法,我先是采用将屏幕扫描完后,得到一个大的改变区域发送,结果发现虽然碎片的问题解决了,但cpu占用却上去了。另外,我尝试对屏幕进行分块,然后来推测改变的区域,结果发现碎片问题没有根本解决,cpu占用也增加了。( p: s' y6 E, M6 {
" V" |7 N/ B: S) s8 m# u
也想不到其它的好方法。先写到这里吧。