下面是陈经韬的文章,可能对你有用,里面有<br><br>利用流实现网络传输屏幕图像<br><br> 大家应该见过很多网管程序,这类程序其中有一个功能就是监控远程电脑的屏幕。实际上,这也是利用流操作来实现的。下面我们给出一个例子,这个例子分两个程序,一个服务端,一个是客户端。程序编译后可以直接在单机、局部网或者互联网上使用。程序中已经给出相应注释。后面我们再来作具体分析。<br> 新建一个工程,在Internet面版上拖一个ServerSocket控件到窗口,该控件主要用于监听客户端,用来与客户端建立连接和通讯。设置好监听端口后调用方法Open或者Active:=True即开始工作。注意:跟前面的NMUDP不同,当Socket开始监听后就不能再改变它的端口,要改变的话必须先调用Close或设置Active为False,否则将会产生异常。另外,如果该端口已经打开的话,就不能再用这个端口了。所以程序运行尚未退出就不能再运行这个程序,否则也会产生异常,即弹出出错窗口。实际应用中可以通过判断程序是否已经运行,如果已经运行就退出的方法来避免出错。<br> 当客户端有数据传入,将触发ServerSocket1ClientRead事件,我们可以在这里对接收的数据进行处理。在本程序中,主要是接收客户端发送过来的字符信息并根据事先的约定来进行相应操作。<br>程序全部代码如下:<br><br>unit Unit1;{服务端程序}<br>interface<br>uses<br>Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, JPEG,ExtCtrls, ScktComp;<br>type<br>TForm1 = class(TForm)<br>ServerSocket1: TServerSocket;<br>procedure ServerSocket1ClientRead(Sender: TObject;Socket: TCustomWinSocket);<br>procedure FormCreate(Sender: TObject);<br>procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>private<br>procedure Cjt_GetScreen(var Mybmp: TBitmap; DrawCur: Boolean);<br>{自定义抓屏函数,DrawCur表示抓鼠标图像与否}<br>{ Private declarations }<br>public<br>{ Public declarations }<br>end;<br>var<br>Form1: TForm1;<br>MyStream: TMemorystream;{内存流对象} <br>implementation<br>{$R *.DFM}<br>procedure TForm1.Cjt_GetScreen(var Mybmp: TBitmap; DrawCur: Boolean);<br>var<br>Cursorx, Cursory: integer;<br>dc: hdc;<br>Mycan: Tcanvas;<br>R: TRect;<br>DrawPos: TPoint;<br>MyCursor: TIcon;<br>hld: hwnd;<br>Threadld: dword;<br>mp: tpoint;<br>pIconInfo: TIconInfo;<br>begin<br>Mybmp := Tbitmap.Create; {建立BMPMAP }<br>Mycan := TCanvas.Create; {屏幕截取}<br>dc := GetWindowDC(0);<br>try<br>Mycan.Handle := dc;<br>R := Rect(0, 0, screen.Width, screen.Height);<br>Mybmp.Width := R.Right;<br>Mybmp.Height := R.Bottom;<br>Mybmp.Canvas.CopyRect(R, Mycan, R);<br>finally<br>releaseDC(0, DC);<br>end;<br>Mycan.Handle := 0;<br>Mycan.Free;<br>if DrawCur then {画上鼠标图象}<br>begin<br>GetCursorPos(DrawPos);<br>MyCursor := TIcon.Create;<br>getcursorpos(mp);<br>hld := WindowFromPoint(mp);<br>Threadld := GetWindowThreadProcessId(hld, nil);<br>AttachThreadInput(GetCurrentThreadId, Threadld, True);<br>MyCursor.Handle := Getcursor();<br>AttachThreadInput(GetCurrentThreadId, threadld, False);<br>GetIconInfo(Mycursor.Handle, pIconInfo);<br>cursorx := DrawPos.x - round(pIconInfo.xHotspot);<br>cursory := DrawPos.y - round(pIconInfo.yHotspot);<br>Mybmp.Canvas.Draw(cursorx, cursory, MyCursor); {画上鼠标}<br>Mycursor.ReleaseHandle; {释放数组内存}<br>MyCursor.Free; {释放鼠标指针}<br>end; <br>end;<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>ServerSocket1.Port := 3000; {端口}<br>ServerSocket1.Open; {Socket开始侦听}<br>end;<br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>if ServerSocket1.Active then ServerSocket1.Close; {关闭Socket}<br>end;<br>procedure TForm1.ServerSocket1ClientRead(Sender: TObject;<br>Socket: TCustomWinSocket);<br>var<br>S, S1: string;<br>MyBmp: TBitmap;<br>Myjpg: TJpegimage;<br>begin<br>S := Socket.ReceiveText;<br>if S = 'cap' then {客户端发出抓屏幕指令}<br>begin<br>try<br>MyStream := TMemorystream.Create;{建立内存流}<br>MyBmp := TBitmap.Create;<br>Myjpg := TJpegimage.Create;<br>Cjt_GetScreen(MyBmp, True); {True表示抓鼠标图像}<br>Myjpg.Assign(MyBmp); {将BMP图象转成JPG格式,便于在互联网上传输}<br>Myjpg.CompressionQuality := 10; {JPG文件压缩百分比设置,数字越大图像月清晰,但数据也越大}<br>Myjpg.SaveToStream(MyStream); {将JPG图象写入流中}<br>Myjpg.free;<br>MyStream.Position := 0;{注意:必须添加此句}<br>s1 := inttostr(MyStream.size);{流的大小}<br>Socket.sendtext(s1); {发送流大小}<br>finally<br>MyBmp.free;<br>end;<br>end;<br>if s = 'ready' then {客户端已准备好接收图象}<br>begin<br>MyStream.Position := 0;<br>Socket.SendStream(MyStream); {将流发送出去}<br>end;<br>end;<br>end.<br><br> 上面是服务端,下面我们来写客户端程序。新建一个工程,添加Socket控件ClientSocket、图像显示控件Image、一个 Panel 、一个Edit、两个 Button和一个状态栏控件StatusBar1。注意:把Edit1和两个 Button放在Panel1上面。ClientSocket的属性跟ServerSocket差不多,不过多了一个Address属性,表示要连接的服务端IP地址。填上IP地址后点“连接”将与服务端程序建立连接,如果成功就可以进行通讯了。点击“抓屏”将发送字符给服务端。因为程序用到了JPEG图像单元,所以要在Uses中添加Jpeg.<br>全部代码如下:<br>unit Unit2{客户端};<br>interface<br>uses<br>Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,ScktComp,ExtCtrls,Jpeg, ComCtrls;<br>type<br>TForm1 = class(TForm)<br>ClientSocket1: TClientSocket;<br>Image1: TImage;<br>StatusBar1: TStatusBar;<br>Panel1: TPanel;<br>Edit1: TEdit;<br>Button1: TButton;<br>Button2: TButton;<br>procedure Button1Click(Sender: TObject);<br>procedure ClientSocket1Connect(Sender: TObject;<br>Socket: TCustomWinSocket);<br>procedure Button2Click(Sender: TObject);<br>procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;<br>ErrorEvent: TErrorEvent; var ErrorCode: Integer);<br>procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);<br>procedure FormCreate(Sender: TObject);<br>procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>procedure ClientSocket1Disconnect(Sender: TObject;<br>Socket: TCustomWinSocket);<br>private<br>{ Private declarations }<br>public<br>{ Public declarations }<br>end;<br>var<br>Form1: TForm1;<br>MySize: Longint;<br>MyStream: TMemorystream;{内存流对象}<br>implementation<br>{$R *.DFM}<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>{-------- 下面为设置窗口控件的外观属性 ------------- }<br>{注意:把Button1、Button2和Edit1放在Panel1上面}<br>Edit1.Text := '127.0.0.1';<br>Button1.Caption := '连接主机';<br>Button2.Caption := '抓屏幕';<br>Button2.Enabled := false;<br>Panel1.Align := alTop;<br>Image1.Align := alClient;<br>Image1.Stretch := True;<br>StatusBar1.Align:=alBottom;<br>StatusBar1.SimplePanel := True;<br>{----------------------------------------------- }<br>MyStream := TMemorystream.Create; {建立内存流对象}<br>MySize := 0; {初始化}<br>end;<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>if not ClientSocket1.Active then<br>begin<br>ClientSocket1.Address := Edit1.Text; {远程IP地址}<br>ClientSocket1.Port := 3000; {Socket端口}<br>ClientSocket1.Open; {建立连接}<br>end;<br>end;<br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>Clientsocket1.Socket.SendText('cap'); {发送指令通知服务端抓取屏幕图象}<br>Button2.Enabled := False;<br>end;<br>procedure TForm1.ClientSocket1Connect(Sender: TObject;<br>Socket: TCustomWinSocket);<br>begin<br>StatusBar1.SimpleText := '与主机' + ClientSocket1.Address + '成功建立连接!';<br>Button2.Enabled := True;<br>end;<br>procedure TForm1.ClientSocket1Error(Sender: TObject;<br>Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;<br>var ErrorCode: Integer);<br>begin<br>Errorcode := 0; {不弹出出错窗口}<br>StatusBar1.SimpleText := '无法与主机' + ClientSocket1.Address + '建立连接!';<br>end;<br>procedure TForm1.ClientSocket1Disconnect(Sender: TObject;<br>Socket: TCustomWinSocket);<br>begin<br>StatusBar1.SimpleText := '与主机' + ClientSocket1.Address + '断开连接!';<br>Button2.Enabled := False;<br>end;<br>procedure TForm1.ClientSocket1Read(Sender: TObject;<br>Socket: TCustomWinSocket);<br>var<br>MyBuffer: array[0..10000] of byte; {设置接收缓冲区}<br>MyReceviceLength: integer;<br>S: string;<br>MyBmp: TBitmap;<br>MyJpg: TJpegimage;<br>begin<br>StatusBar1.SimpleText := '正在接收数据......';<br>if MySize = 0 then {MySize为服务端发送的字节数,如果为0表示为尚未开始图象接收}<br>begin<br>S := Socket.ReceiveText;<br>MySize := Strtoint(S); {设置需接收的字节数}<br>Clientsocket1.Socket.SendText('ready'); {发指令通知服务端开始发送图象}<br>end<br>else<br>begin {以下为图象数据接收部分}<br>MyReceviceLength := socket.ReceiveLength; {读出包长度}<br>StatusBar1.SimpleText := '正在接收数据,数据大小为:' + inttostr(MySize);<br>Socket.ReceiveBuf(MyBuffer, MyReceviceLength); {接收数据包并读入缓冲区内}<br>MyStream.Write(MyBuffer, MyReceviceLength); {将数据写入流中}<br>if MyStream.Size >= MySize then {如果流长度大于需接收的字节数,则接收完毕}<br>begin<br>MyStream.Position := 0;<br>MyBmp := tbitmap.Create;<br>MyJpg := tjpegimage.Create;<br>try<br>MyJpg.LoadFromStream(MyStream); {将流中的数据读至JPG图像对象中}<br>MyBmp.Assign(MyJpg); {将JPG转为BMP}<br>StatusBar1.SimpleText := '正在显示图像';<br>Image1.Picture.Bitmap.Assign(MyBmp); {分配给image1元件 }<br>finally {以下为清除工作 }<br>MyBmp.free;<br>MyJpg.free;<br>Button2.Enabled := true;<br>{ Socket.SendText('cap');添加此句即可连续抓屏 }<br>MyStream.Clear;<br>MySize := 0;<br>end;<br>end;<br>end;<br>end;<br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>MyStream.Free; {释放内存流对象}<br>if ClientSocket1.Active then ClientSocket1.Close; {关闭Socket连接}<br>end;<br>end.<br><br> 程序原理:运行服务端开始侦听,再运行客户端,输入服务端IP地址建立连接,然后发一个字符通知服务端抓屏幕。服务端调用自定义函数Cjt_GetScreen抓取屏幕存为BMP,把BMP转换成JPG,把JPG写入内存流中,然后把流发送给客户端。客户端接收到流后做相反操作,将流转换为JPG再转换为BMP然后显示出来。<br> 注意:因为Socket的限制,不能一次发送过大的数据,只能分几次发。所以程序中服务端抓屏转换为流后先发送流的大小,通知客户端这个流共有多大,客户端根据这个数字大小来判断是否已经接收完流,如果接收完才转换并显示。<br> 这个程序跟前面的自制OICQ都是利用了内存流对象TMemoryStream。其实,这个流对象是程序设计中用得最普遍的,它可以提高I/O的读写能力,而且如果你要同时操作几个不同类型的流,互相交换数据的话,用它作“中间人”是最好不过的了。比如说你把一个流压缩或者解压缩,就先建立一个TMemoryStream对象,然后把别的数据拷贝进去,再执行相应操作就可以了。因为它是直接在内存中工作,所以效率是非常高的。有时侯甚至你感觉不到有任何的延迟。<br> 程序有待改进的地方:当然可以加一个压缩单元,发送前先压缩再发送。注意:这里也是有技巧的,就是直接把BMP压缩而不要转换成JPG再压。实验证明:上面程序一幅图像大小大概为40-50KB,如果用LAH压缩算法处理一下便只有8-12KB,这样传输起来就比较快。如果想更快的话,可以采用这样的方法:先抓第一幅图像发送,然后从第二幅开始只发跟前一幅不同区域的图像。外国有一个程序叫Remote Administrator,就是采用这样的方法。他们测试的数据如下:局部网一秒钟100-500幅,互联网上,在网速极低的情况下,一秒钟传输5-10幅。说这些题外话只想说明一个道理:想问题,特别是写程序,特别是看起来很复杂的程序,千万不要钻牛角尖,有时侯不妨换个角度来想。程序是死的,人才是活的。当然,这些只能靠经验的积累。但是一开始就养成好习惯是终身受益的!<br>