vbwinsock传输图片
⑴ 怎样通过WinSock传送图片
VB用 winsock 发送图片和接受图片(转载)2009-10-30 12:57窗体,需要控件:Picture1 要载入图片;Winsock1 [ Index = 0 ]
Option Explicit
Private Sub Form_Load()
Winsock1(0).Protocol = sckTCPProtocol '使用TCP协议
Winsock1(0).LocalPort = 9999 '设置本地端口
Winsock1(0).RemotePort = 0 '设置要连接的远程端口号
Winsock1(0).Listen '设置为侦听模式
End Sub
'接受连接
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim PBag As New PropertyBag '得到图片信息
Dim PictureByt() As Byte '保存图像
Load Winsock1(Winsock1.Count)
Winsock1(Winsock1.Count - 1).Accept requestID '接受新连接
'---------------------------------------------------------------
PBag.WriteProperty "Picture", Picture1.Picture
PictureByt = PBag.Contents '获取容器Byte到PictureByt()数组中
'---------------------------------------------------------------
Winsock1(Winsock1.Count - 1).SendData PictureByt '发送数据
End Sub
'==========================================================
窗体Client,需要控件:Command1,Command2,Picture1,Winsock1 。全部是默认属性
Option Explicit
Private Sub Command1_Click()
Winsock1.Close '关闭连接
Winsock1.Protocol = sckTCPProtocol '使用TCP协议
Winsock1.LocalPort = 0 '动态地建立连接
Winsock1.RemotePort = 9999 '设置要连接的远程端口号
Winsock1.Connect "192.168.1.124" '设置要连接的计算机名称或IP地址
'IP改成要连接的IP
Command1.Enabled = False
End Sub
Private Sub Command2_Click()
Dim bytData() As Byte '接收图片
Dim PBag As New PropertyBag '图片信息
ReDim bytData(1 To Winsock1.BytesReceived) '接收图片大小
Winsock1.GetData bytData '读取缓冲区数据
PBag.Contents = bytData
Set Picture1.Picture = PBag.ReadProperty("Picture") '设置图片
Command2.Enabled = False
End Sub
Private Sub Form_Load()
Command1.Caption = "连接"
Command2.Caption = "显示图片"
End Sub