将远程屏幕转化成流文件,通过WINSOCKET发送,接收到后在转换成JPG文件,再转化成BMP文件。以下是具体代码:
服务器端:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, jpeg, StdCtrls;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
Memo1: TMemo;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
m1: TMemoryStream;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
try
ServerSocket1.Close;
ServerSocket1.Port := StrToInt(edit1.Text);
ServerSocket1.Active := true;
memo1.Clear;
finally
end;
end;
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode := 0;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
s,s1: String;
bitmap: TBitmap;
jpg: TJpegImage;
desk: TCanvas;
memos: string;
begin
s := socket.ReceiveText;
if s = 'gets' then
begin
memos := s;
memo1.Lines.Add(memos);
bitmap := TBitmap.Create;
jpg := TJpegImage.Create;
desk := TCanvas.Create;
desk.Handle := getdc(hwnd_desktop);
m1 := TmemoryStream.Create;
with bitmap do
begin
width := screen.Width;
height := screen.Height;
bitmap.Canvas.CopyRect(canvas.ClipRect,desk,desk.ClipRect);
end;
jpg.Assign(bitmap);
jpg.SaveToStream(m1);
jpg.Free;
m1.Position := 0;
s1 := intTostr(m1.Size);
socket.SendText(s1);
end;
if s = 'okok' then
begin
memo1.Lines.Add(s);
m1.Position := 0;
socket.SendStream(m1);
end;
end;
end.
客户端:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, ExtCtrls, jpeg;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
Image1: TImage;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Edit2: TEdit;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
c: LongInt;
m: TMemoryStream;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if ClientSocket1.Socket.Connected then
ClientSocket1.Socket.SendText('gets');
image1.Refresh;
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode := 0;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
buffer: array[0..10000] of byte;
len: Integer;
l1: String;
b: TBitmap;
j: TJpegImage;
begin
if c = 0 then
begin
l1 := socket.ReceiveText;
c := strToInt(l1);
socket.SendText('okok');
end else
begin
Len := socket.ReceiveLength;
socket.ReceiveBuf(buffer,len);
m.WriteBuffer(buffer,len);
if m.Size >= c then
begin
m.Position := 0;
b := TBitmap.Create;
j := TJpegImage.Create;
try
j.LoadFromStream(m);
b.Assign(j);
image1.Picture.Bitmap.Assign(b);
finally
b.Free;
j.Free;
// ClientSocket1.Active := false;
m.Clear;
c := 0;
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
m := TmemoryStream.Create;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
m.Free;
ClientSocket1.Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
clientsocket1.Close;
ClientSocket1.Address := edit1.Text;
ClientSocket1.Port := strToInt(edit2.Text);
ClientSOCket1.Active := true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if ClientSocket1.Socket.Connected then
form1.Caption := 'ok'
else
form1.Caption := 'false';
end;
end.
轻松搞定!