type TForm1 = class(TForm) ServerSocket1: TServerSocket; procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1; m1:tmemorystream;
implementation
{$R *.DFM}
procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); var s,s1:string; desk:tcanvas; bitmap:tbitmap; jpg:tjpegimage; begin s:=socket.ReceiveText; if s=‘gets‘ then //客户端发出申请 begin bitmap:=tbitmap.Create; jpg:=tjpegimage.Create; desk:=tcanvas.Create; //以下代码为取得当前屏幕图象 desk.Handle:=getdc(hwnd_desktop); m1:=tmemorystream.Create; //初始化流m1,在用sendstream(m1)发送流后, //它将保留到socket对话结束, //不能用手工free掉,否则会触发异常 with bitmap do begin width:=screen.Width; height:=screen.Height; canvas.CopyRect(canvas.cliprect,desk,desk.cliprect); end; jpg.Assign(bitmap); //将图象转成JPG格式 jpg.SaveToStream(m1); //将JPG图象写入流中 jpg.free; m1.Position:=0; s1:=inttostr(m1.size); Socket.sendtext(s1); //发送图象大小 end; if s=‘okok‘ then //客户端已准备好接收图象 begin m1.Position:=0; Socket.SendStream(m1); //发送JPG图象 end;
end;
procedure TForm1.FormCreate(Sender: TObject); begin ServerSocket1.open; end;