S
sunsiz
Unregistered / Unconfirmed
GUEST, unregistred user!
我做的程序,可以抓取屏幕发送给接手端。不过在接手端接受不了图像信息,存到文件看,是一片空白的文件。请大家帮忙!!!
发送端代码:
procedure TForm1.FormCreate(Sender: TObject);
begin
m1:=TMemoryStream.create;
TcpServer1.LocalPort:='1111';
TcpServer1.Active:=true;
TcpClient1.RemoteHost:='192.168.1.2';
TcpClient1.RemotePort:='2222';
TcpClient1.Active:=true;
end;
procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
s1:string;
desk:tcanvas;
bitmap:tbitmap;
jpg:tjpegimage;
begin
if ClientSocket.Receiveln()='start' then
begin
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;
PixelFormat :=pf16bit;
canvas.CopyRect(canvas.cliprect,desk,desk.cliprect);
end;
jpg.CompressionQuality :=5;
jpg.Assign(bitmap);
jpg.SaveToStream(m1);
// jpg.free;
bitmap.Free;
m1.Position:=0;
s1:=inttostr(m1.size);
try
if TcpClient1.Connect then
begin
TCPClient1.sendln(s1);
end;
finally
TcpClient1.Disconnect;
end;
end
else
begin
m1.Position:=0;
try
if TcpClient1.Connect then
begin
m1.SaveToFile('C:/temp.jpg');
TCPClient1.SendStream(M1);
end;
finally
TcpClient1.Disconnect;
end;
end;
end;
*****************************************************************
m1.SaveToFile('C:/temp.jpg');在存到的文件是正常的。
接受端代码:
procedure TForm1.FormCreate(Sender: TObject);
begin
memoryStream:=TMemoryStream.create;
TcpServer1.LocalPort:='2222';
TcpServer1.Active:=true;
TcpClient1.RemoteHost:='192.168.1.2';
TcpClient1.RemotePort:='1111';
TcpClient1.Active:=true;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
try
TcpClient1.RemoteHost:='192.168.1.2';
TcpClient1.RemotePort:='1111';
//TcpClient1.Active:=true;
if TcpClient1.Connect then
begin
TcpClient1.Sendln('start');
end;
finally
TcpClient1.Disconnect;
end;
end;
procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
buffer:array [0..100000] of byte; //设置接收缓冲区
ll:string;
b:tbitmap;
j:tjpegimage;
begin
if c=0 then //C为服务端发送的字节数,如果为0表示为尚未开始图象接收
begin
ll:=ClientSocket.Receiveln;
c:=strtoint(ll); //设置需接收的字节数
try
TcpClient1.RemoteHost:='192.168.1.2';
TcpClient1.RemotePort:='1111';
//TcpClient1.Active:=true;
if TcpClient1.Connect then
begin
TcpClient1.Sendln('okok'); //通知服务端开始发送图象
end;
finally
TcpClient1.Disconnect;
end;
end else
begin //以下为图象数据接收部分
ClientSocket.ReceiveBuf(buffer,c); //接收数据包并读入缓冲区内
memorystream.Write(buffer,c); //追加入流M中
// showmessage(inttostr(memorystream.Size));
if memorystream.Size>=c then //如果流长度大于需接收的字节数,则接收完毕
begin
memorystream.Position:=0;
b:=tbitmap.Create;
j:=tjpegimage.Create;
try
memorystream.SaveToFile('C:/tmp.jpg');
j.LoadFromStream(memorystream); //将流M中的数据读至JPG图像对象J中
b.Assign(j); //将JPG转为BMP
Image1.Picture.Bitmap.Assign(b); //分配给image1元件
finally //以下为清除工作
b.free;
j.free;
memorystream.Clear;
c:=0;
end;
end;
// end;
end;
end;
************************************************************
memorystream.SaveToFile('C:/tmp.jpg');接受不了图像。
请各位帮个忙!!!
发送端代码:
procedure TForm1.FormCreate(Sender: TObject);
begin
m1:=TMemoryStream.create;
TcpServer1.LocalPort:='1111';
TcpServer1.Active:=true;
TcpClient1.RemoteHost:='192.168.1.2';
TcpClient1.RemotePort:='2222';
TcpClient1.Active:=true;
end;
procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
s1:string;
desk:tcanvas;
bitmap:tbitmap;
jpg:tjpegimage;
begin
if ClientSocket.Receiveln()='start' then
begin
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;
PixelFormat :=pf16bit;
canvas.CopyRect(canvas.cliprect,desk,desk.cliprect);
end;
jpg.CompressionQuality :=5;
jpg.Assign(bitmap);
jpg.SaveToStream(m1);
// jpg.free;
bitmap.Free;
m1.Position:=0;
s1:=inttostr(m1.size);
try
if TcpClient1.Connect then
begin
TCPClient1.sendln(s1);
end;
finally
TcpClient1.Disconnect;
end;
end
else
begin
m1.Position:=0;
try
if TcpClient1.Connect then
begin
m1.SaveToFile('C:/temp.jpg');
TCPClient1.SendStream(M1);
end;
finally
TcpClient1.Disconnect;
end;
end;
end;
*****************************************************************
m1.SaveToFile('C:/temp.jpg');在存到的文件是正常的。
接受端代码:
procedure TForm1.FormCreate(Sender: TObject);
begin
memoryStream:=TMemoryStream.create;
TcpServer1.LocalPort:='2222';
TcpServer1.Active:=true;
TcpClient1.RemoteHost:='192.168.1.2';
TcpClient1.RemotePort:='1111';
TcpClient1.Active:=true;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
try
TcpClient1.RemoteHost:='192.168.1.2';
TcpClient1.RemotePort:='1111';
//TcpClient1.Active:=true;
if TcpClient1.Connect then
begin
TcpClient1.Sendln('start');
end;
finally
TcpClient1.Disconnect;
end;
end;
procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
buffer:array [0..100000] of byte; //设置接收缓冲区
ll:string;
b:tbitmap;
j:tjpegimage;
begin
if c=0 then //C为服务端发送的字节数,如果为0表示为尚未开始图象接收
begin
ll:=ClientSocket.Receiveln;
c:=strtoint(ll); //设置需接收的字节数
try
TcpClient1.RemoteHost:='192.168.1.2';
TcpClient1.RemotePort:='1111';
//TcpClient1.Active:=true;
if TcpClient1.Connect then
begin
TcpClient1.Sendln('okok'); //通知服务端开始发送图象
end;
finally
TcpClient1.Disconnect;
end;
end else
begin //以下为图象数据接收部分
ClientSocket.ReceiveBuf(buffer,c); //接收数据包并读入缓冲区内
memorystream.Write(buffer,c); //追加入流M中
// showmessage(inttostr(memorystream.Size));
if memorystream.Size>=c then //如果流长度大于需接收的字节数,则接收完毕
begin
memorystream.Position:=0;
b:=tbitmap.Create;
j:=tjpegimage.Create;
try
memorystream.SaveToFile('C:/tmp.jpg');
j.LoadFromStream(memorystream); //将流M中的数据读至JPG图像对象J中
b.Assign(j); //将JPG转为BMP
Image1.Picture.Bitmap.Assign(b); //分配给image1元件
finally //以下为清除工作
b.free;
j.free;
memorystream.Clear;
c:=0;
end;
end;
// end;
end;
end;
************************************************************
memorystream.SaveToFile('C:/tmp.jpg');接受不了图像。
请各位帮个忙!!!