赛
赛特
Unregistered / Unconfirmed
GUEST, unregistred user!
服务器:
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
jpg:TJpegImage;
Str:String;
begin
AData.Seek(0,0);
SetLength(Str,AData.Size);
AData.Read(Str[1],AData.Size);
if Pos('Send File:File Length:',Str)>0 then
begin
Delete(Str,1,Length('Send File:File Length:'));
FileSize:=StrToIntDef(Str,0);
if Stream<>nil then
begin
Stream.Free;
Stream:=nil;
end;
Stream:=TMemoryStream.Create;
Str:='Recived File Length OK!';
Statictext1.Caption:=inttostr(filesize);
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end
else if Pos('Send File:File End!',Str)>0 then
begin
//显示图片 [red]这里有问题啊[/red]
try
jpg := TJpegImage.Create;
Stream.Position:=0;
//jpg.LoadFromStream(Stream);
Statictext2.Caption:=inttostr(Stream.Size);
Image1.Picture.Bitmap.LoadFromStream(Stream);
Statictext3.Caption:='yes';
jpg.Free;
except
end;
if Stream<>nil then
begin
Stream.Free;
Stream:=nil;
Str:='Recived File OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end;
FileSize:=0;
end
else
begin
if Stream<>nil then
begin
Stream.Seek(0,2);
AData.Seek(0,0);
Stream.CopyFrom(AData,AData.Size);
Str:='Recived File Package OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
Application.ProcessMessages;
end;
end;
end;
客户端:
procedure TForm1.Timer1Timer(Sender: TObject);
var
MyBmp: TBitmap;
jpg:TJpegImage;
p:Array[0..1023] of byte;
Posi,Len:Integer;
ReceivedString:string;
begin
Timer1.Enabled:=false;
ScreenCap(MyBmp, true); //本地截屏,保存在MyBmp中
memStream := TMemoryStream.Create;
jpg := TJpegImage.Create;
jpg.Assign(MyBmp);
jpg.CompressionQuality := 50;
jpg.JPEGNeeded;
jpg.Compress;
jpg.SaveToStream(memStream);
jpg.Free;
IdUDPClient1.Host:='127.0.0.1';
IdUDPClient1.Port:=9001;
IdUDPClient1.Active:=True;
try
Posi:=0;
IdUDPClient1.Send('Send File:File Length:'+IntToStr(memStream.Size));
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)='RECIVED FILE LENGTH OK!' then
begin
While Posi<memStream.Size do
begin
Len:=1024;
if memStream.Size-Posi<1024 then Len:=memStream.Size-Posi;
memStream.Read(p,Len);
IdUDPClient1.SendBuffer(P,Len);
Inc(Posi,Len);
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)<>'RECIVED FILE PACKAGE OK!' then break;
Application.ProcessMessages;
end;
IdUDPClient1.Send('Send File:File End!');
end
else
ShowMessage('Send file cancel!');
finally
memStream.Free;
end;
MyBmp.Free;
end;
显示图片那里出现问题,而且Stream.size也能准确的等于客户端这边发送的长度,但就是显示图片要报错?
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
jpg:TJpegImage;
Str:String;
begin
AData.Seek(0,0);
SetLength(Str,AData.Size);
AData.Read(Str[1],AData.Size);
if Pos('Send File:File Length:',Str)>0 then
begin
Delete(Str,1,Length('Send File:File Length:'));
FileSize:=StrToIntDef(Str,0);
if Stream<>nil then
begin
Stream.Free;
Stream:=nil;
end;
Stream:=TMemoryStream.Create;
Str:='Recived File Length OK!';
Statictext1.Caption:=inttostr(filesize);
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end
else if Pos('Send File:File End!',Str)>0 then
begin
//显示图片 [red]这里有问题啊[/red]
try
jpg := TJpegImage.Create;
Stream.Position:=0;
//jpg.LoadFromStream(Stream);
Statictext2.Caption:=inttostr(Stream.Size);
Image1.Picture.Bitmap.LoadFromStream(Stream);
Statictext3.Caption:='yes';
jpg.Free;
except
end;
if Stream<>nil then
begin
Stream.Free;
Stream:=nil;
Str:='Recived File OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end;
FileSize:=0;
end
else
begin
if Stream<>nil then
begin
Stream.Seek(0,2);
AData.Seek(0,0);
Stream.CopyFrom(AData,AData.Size);
Str:='Recived File Package OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
Application.ProcessMessages;
end;
end;
end;
客户端:
procedure TForm1.Timer1Timer(Sender: TObject);
var
MyBmp: TBitmap;
jpg:TJpegImage;
p:Array[0..1023] of byte;
Posi,Len:Integer;
ReceivedString:string;
begin
Timer1.Enabled:=false;
ScreenCap(MyBmp, true); //本地截屏,保存在MyBmp中
memStream := TMemoryStream.Create;
jpg := TJpegImage.Create;
jpg.Assign(MyBmp);
jpg.CompressionQuality := 50;
jpg.JPEGNeeded;
jpg.Compress;
jpg.SaveToStream(memStream);
jpg.Free;
IdUDPClient1.Host:='127.0.0.1';
IdUDPClient1.Port:=9001;
IdUDPClient1.Active:=True;
try
Posi:=0;
IdUDPClient1.Send('Send File:File Length:'+IntToStr(memStream.Size));
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)='RECIVED FILE LENGTH OK!' then
begin
While Posi<memStream.Size do
begin
Len:=1024;
if memStream.Size-Posi<1024 then Len:=memStream.Size-Posi;
memStream.Read(p,Len);
IdUDPClient1.SendBuffer(P,Len);
Inc(Posi,Len);
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)<>'RECIVED FILE PACKAGE OK!' then break;
Application.ProcessMessages;
end;
IdUDPClient1.Send('Send File:File End!');
end
else
ShowMessage('Send file cancel!');
finally
memStream.Free;
end;
MyBmp.Free;
end;
显示图片那里出现问题,而且Stream.size也能准确的等于客户端这边发送的长度,但就是显示图片要报错?