SendBuffer发送一个流时出错误 ( 积分: 20 )

  • 主题发起人 主题发起人 siaosa
  • 开始时间 开始时间
S

siaosa

Unregistered / Unconfirmed
GUEST, unregistred user!
发送端
var
mStream:TStringStream;
begin
mStream:=TStringStream.Create('');
Image1.Picture.Bitmap.SaveToStream(mStream);
Idudpclient1.SendBuffer(mStream,mStream.Size);
mStream.Free;
end;

接收端
var
mStream:TFileStream;
begin
mStream:=TFileStream.Create('c:/001.jpg',fmCreate);
adata.ReadBuffer(mStream,adata.Size );
mStream.Free;
Image2.Picture.LoadFromFile('c:/001.jpg');
end;
接收到的001.jpg大小为0
 
发送端
var
mStream:TStringStream;
begin
mStream:=TStringStream.Create('');
Image1.Picture.Bitmap.SaveToStream(mStream);
Idudpclient1.SendBuffer(mStream,mStream.Size);
mStream.Free;
end;

接收端
var
mStream:TFileStream;
begin
mStream:=TFileStream.Create('c:/001.jpg',fmCreate);
adata.ReadBuffer(mStream,adata.Size );
mStream.Free;
Image2.Picture.LoadFromFile('c:/001.jpg');
end;
接收到的001.jpg大小为0
 
Stream读入内容后要把指针指向头
图片是不是可以用TStringStream
 
发送端
var
mStream:TStringStream;
begin
mStream:=TStringStream.Create('');
Image1.Picture.Bitmap.SaveToStream(mStream);
mStream.Position:=0;
Idudpclient1.SendBuffer(mStream,mStream.Size);
mStream.Free;
end;

接收端
var
mStream:TFileStream;
begin
mStream:=TFileStream.Create('c:/001.jpg',fmCreate);
adata.ReadBuffer(mStream,adata.Size );
mStream.Position:=0;
mStream.Free;
Image2.Picture.LoadFromFile('c:/001.jpg');
end;
客户端和服务端都加入mStream.Position:=0结果还是一样,把TStringStream换成TMemoryStream,TFileStream结果都是 接收到的001.jpg大小为0
 
用byte数组吧,保险没错
 
用byte应该怎么改代码?能帮着改一下吗?
 
把Stream存入bytes,再从bytes中取出

var
Form1: TForm1;
SendBytes:array of byte;//一个byte数组

implementation

{$R *.dfm}

//把图片保存到byte数组
procedure TForm1.Button1Click(Sender: TObject);
var
mStream:TMemoryStream;
begin
mStream:=TMemoryStream.Create;
Image1.Picture.LoadFromFile('a.bmp');
Image1.Picture.Bitmap.SaveToStream(mStream);
mStream.Position:=0;
setlength(SendBytes,mStream.Size);//设置数组长度
mStream.ReadBuffer(SendBytes[0],mStream.Size);//把图片内容写入SendBytes数组
mStream.Free;
end;

//把byte数组中内容取出,保存为新图片
procedure TForm1.Button2Click(Sender: TObject);
var
mStream:TMemoryStream;
begin
mStream:=TMemoryStream.Create;
mStream.WriteBuffer(SendBytes[0],length(SendBytes));//把SendBytes中内容保存入mStream
mStream.Position:=0;
mStream.SaveToFile('b.bmp');//存为新图片
mStream.Free;
end;
 
接受答案了.
 
后退
顶部