Q
qiuyan81
Unregistered / Unconfirmed
GUEST, unregistred user!
我把INDYUDP客户端放在一个线程里发送图片
但是速度很慢 发一张图要2~3秒才能发送完成
用jpeg把图片压缩到1%时也要2秒这样才能发完
抓图速度很快 就是被发送的时间拉慢了 很郁闷
理论上UDP是不需要对方返回收到包的应答
下面给出线程的代码 希望大家能帮我分析下 怎么才能达到QQ上的远程协助的效果
unit GetSrcThread;
interface
uses
windows,Classes,Messages, SysUtils,IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient,jpeg,Graphics,
Forms, Dialogs;
type
Tcom=Record
Ip:string[15];
prot:integer;
buf:array[0..32000] of char;
cmd,msg:string[255];
PackEnd:boolean;
end ;
TGetScreen = class(TThread)
private
Com:Tcom;
MyCom:Tcom;
IDC:TIdUDPClient;
MyStream:TMemoryStream;
{ Private declarations }
protected
procedure Execute; override;
public
constructor create(IP:string;Port:integer);
function GetSrc(Cur: Boolean;Full:boolean):boolean;
end;
implementation
uses main;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure GetSrceen.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ GetSrceen }
procedure TGetScreen.Execute;
begin
{ Place thread code here }
mainfrm.caption:='0';
MyStream:=TMemoryStream.Create;
while not mainfrm.Stop do
begin
try
mycom.cmd:='scr';
mycom.msg:='';
if getsrc(true,true) then
repeat
if mystream.Size-mystream.Position>32000 then
begin
mycom.PackEnd:=false;
mystream.ReadBuffer(com.buf,32000);
end
else
begin
mystream.ReadBuffer(mycom.buf,mystream.Size-mystream.Position);
mycom.PackEnd:=true;
end;
idc.SendBuffer(mycom,sizeof(mycom));
mainfrm.Caption:=inttostr(strtoint(mainfrm.Caption)+sizeof(mycom));
Application.ProcessMessages;
until myStream.Position = myStream.Size;
finally
mystream.Clear;
// sleep(30);
end;
end;
freeandnil(mystream);
end;
constructor TGetScreen.create(IP:string;Port:integer);
begin
IDC:=TIdUDPClient.Create(nil);
idc.Active:=true;
idc.Host:=ip;
idc.Port:=port;
idc.BufferSize:=32768;
inherited Create(false);
end;
function TGetScreen.GetSrc(Cur: Boolean;Full:boolean):boolean;
var
MyBmp:Tbitmap;
MyJpeG:TjpegImage;
MyHwnd:hwnd;
xy:Trect;
p:Tpoint;
begin
MyBmp:=Tbitmap.Create;
MyJpeG:=TjpegImage.Create;
if full=true then
begin
mybmp.Height:=screen.Height;
mybmp.Width:=screen.Width;
Myhwnd:=0;
end
else
begin
myhwnd:=GetForegroundWindow;
getwindowrect(myhwnd,xy);
mybmp.Height:=xy.Bottom;
mybmp.Width:=xy.Right;
end;
StretchBlt(mybmp.Canvas.Handle,0,0,mybmp.Width,mybmp.Height,getdc(myhwnd),0,0,mybmp.Width,mybmp.Height,srccopy);
if cur=true then
begin
GetCursorPos(p);
drawicon(mybmp.Canvas.Handle,p.X,p.Y,GetCursor());
end;
try
myjpeg.Assign(mybmp);
myjpeg.CompressionQuality:=90;
Mystream.Position:=0;
myjpeg.SaveToStream(Mystream);
Mystream.Position:=0;
if Mystream.Size>0 then
getsrc:=true
else
getsrc:=false;
myjpeg.LoadFromStream(mystream);
mybmp.Assign(myjpeg);
mainfrm.Img.Picture.Bitmap.Assign(mybmp);
mainfrm.Img.Refresh;
except
//mybmp.FreeImage;
//myjpeg.Free;
getsrc:=false;
//freeandnil(mybmp);
//freeandnil(myjpeg);
end;
freeandnil(mybmp);
freeandnil(myjpeg);
// getsrc:=false;
end;
end.
但是速度很慢 发一张图要2~3秒才能发送完成
用jpeg把图片压缩到1%时也要2秒这样才能发完
抓图速度很快 就是被发送的时间拉慢了 很郁闷
理论上UDP是不需要对方返回收到包的应答
下面给出线程的代码 希望大家能帮我分析下 怎么才能达到QQ上的远程协助的效果
unit GetSrcThread;
interface
uses
windows,Classes,Messages, SysUtils,IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient,jpeg,Graphics,
Forms, Dialogs;
type
Tcom=Record
Ip:string[15];
prot:integer;
buf:array[0..32000] of char;
cmd,msg:string[255];
PackEnd:boolean;
end ;
TGetScreen = class(TThread)
private
Com:Tcom;
MyCom:Tcom;
IDC:TIdUDPClient;
MyStream:TMemoryStream;
{ Private declarations }
protected
procedure Execute; override;
public
constructor create(IP:string;Port:integer);
function GetSrc(Cur: Boolean;Full:boolean):boolean;
end;
implementation
uses main;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure GetSrceen.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ GetSrceen }
procedure TGetScreen.Execute;
begin
{ Place thread code here }
mainfrm.caption:='0';
MyStream:=TMemoryStream.Create;
while not mainfrm.Stop do
begin
try
mycom.cmd:='scr';
mycom.msg:='';
if getsrc(true,true) then
repeat
if mystream.Size-mystream.Position>32000 then
begin
mycom.PackEnd:=false;
mystream.ReadBuffer(com.buf,32000);
end
else
begin
mystream.ReadBuffer(mycom.buf,mystream.Size-mystream.Position);
mycom.PackEnd:=true;
end;
idc.SendBuffer(mycom,sizeof(mycom));
mainfrm.Caption:=inttostr(strtoint(mainfrm.Caption)+sizeof(mycom));
Application.ProcessMessages;
until myStream.Position = myStream.Size;
finally
mystream.Clear;
// sleep(30);
end;
end;
freeandnil(mystream);
end;
constructor TGetScreen.create(IP:string;Port:integer);
begin
IDC:=TIdUDPClient.Create(nil);
idc.Active:=true;
idc.Host:=ip;
idc.Port:=port;
idc.BufferSize:=32768;
inherited Create(false);
end;
function TGetScreen.GetSrc(Cur: Boolean;Full:boolean):boolean;
var
MyBmp:Tbitmap;
MyJpeG:TjpegImage;
MyHwnd:hwnd;
xy:Trect;
p:Tpoint;
begin
MyBmp:=Tbitmap.Create;
MyJpeG:=TjpegImage.Create;
if full=true then
begin
mybmp.Height:=screen.Height;
mybmp.Width:=screen.Width;
Myhwnd:=0;
end
else
begin
myhwnd:=GetForegroundWindow;
getwindowrect(myhwnd,xy);
mybmp.Height:=xy.Bottom;
mybmp.Width:=xy.Right;
end;
StretchBlt(mybmp.Canvas.Handle,0,0,mybmp.Width,mybmp.Height,getdc(myhwnd),0,0,mybmp.Width,mybmp.Height,srccopy);
if cur=true then
begin
GetCursorPos(p);
drawicon(mybmp.Canvas.Handle,p.X,p.Y,GetCursor());
end;
try
myjpeg.Assign(mybmp);
myjpeg.CompressionQuality:=90;
Mystream.Position:=0;
myjpeg.SaveToStream(Mystream);
Mystream.Position:=0;
if Mystream.Size>0 then
getsrc:=true
else
getsrc:=false;
myjpeg.LoadFromStream(mystream);
mybmp.Assign(myjpeg);
mainfrm.Img.Picture.Bitmap.Assign(mybmp);
mainfrm.Img.Refresh;
except
//mybmp.FreeImage;
//myjpeg.Free;
getsrc:=false;
//freeandnil(mybmp);
//freeandnil(myjpeg);
end;
freeandnil(mybmp);
freeandnil(myjpeg);
// getsrc:=false;
end;
end.