C# UDP发送图片
如题我把picturebox中的图片转换为二进制数组后用UDP发送出去,接收的时候吧这个数组转换为图片显示在picturebox中但是为什么显示的是一张全黑的图片?
发送:
UdpClient picUDP = new UdpClient();
try
{
//让其自动提供子网中的IP广播地址
IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 8001);
Image TempPic = pictureBox1.Image;
MemoryStream ms = new MemoryStream();
TempPic.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = new byte[ms.Length];
bytes = ms.ToArray();
//向子网发送信息
picUDP.Send(bytes, bytes.Length, iep);
}
catch (Exception err)
{
MessageBox.Show(err.Message, "发送失败");
}
finally
{
picUDP.Close();
}
接收
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
//在本机指定的端口接收
udpClient = new UdpClient(port);
IPEndPoint remote = null;
//接收从远程主机发送过来的信息;
while (true)
{
try
{
//关闭udpClient时此句会产生异常
byte[] bytes = udpClient.Receive(ref