如果用tcpsocket发送和接收图片(最好有代码)。(50分)

  • 主题发起人 主题发起人 wzwcn
  • 开始时间 开始时间
W

wzwcn

Unregistered / Unconfirmed
GUEST, unregistred user!
问题如题。
 
用流Stream,Delphi应该有例子。
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, IdTCPConnection, IdTCPClient,
IdBaseComponent, IdComponent, IdTCPServer;

type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
IdTCPClient1: TIdTCPClient;
Image1: TImage;
Image2: TImage;
BitBtn1: TBitBtn;
Bevel1: TBevel;
Bevel3: TBevel;
Bevel4: TBevel;
procedure BitBtn1Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
var
Temp:TMemoryStream;
begin
Temp:=TMemoryStream.Create;
//将图像文件保存到内存流
Image1.Picture.Graphic.SaveToStream(Temp);
//发送图像信息
IdTCPClient1.Connect();
IdTCPClient1.writestream(Temp);
IdTCPClient1.Disconnect();
Temp.Free ;
end;

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
const
TempImageFile='c:/test.bmp';
//设置的临时图像文件
var
t:TMemoryStream;
begin
t:=TMemoryStream.Create;
AThread.Connection.ReadStream(t,-1,True);
//从连接中取得流式数据
t.SaveToFile(TempImageFile);
Image2.Picture.LoadFromFile(TempImageFile);
t.Free ;
end;

end.
 
多人接受答案了。
 
后退
顶部