不管是socket还是udp发送图片都是把图片转换成字节流,收到字节流的电脑把字节流写入磁盘便生成了新的图片。
首先是把图片转换成字节流的方法:
public static byte[] GetPictureBytes(string filename) //filename填写图片路径
{
FileInfo fileInfo = new FileInfo(filename);
byte[] buffer = new byte[fileInfo.Length];
using (FileStream stream = fileInfo.OpenRead())
{
stream.Read(buffer, 0, buffer.Length);
}
return buffer;
}
发送端:
地址: IPEndPoint iep = new IPEndPoint(IPAddress.Parse(strIp), int.Parse(strPort));
bytes = GetPictureBytes(@strP); //图片路径,转成字节流
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//实例化Socket对象
client.Connect(iep);//与该ip地址进行连接
byte[] datasiz