C#socket服务端与客户端互相选择和发送图片,收到的图片可以显示在picturebox控件中
时间: 2024-03-23 07:40:30 浏览: 69
CShaper的Picturebox显示图片放大缩小和与Halcon控件hwindowcontrol鼠标事件放大缩小.rar
5星 · 资源好评率100%
下面是一个简单的 C# Socket 图片传输示例,服务端和客户端可以互相选择和发送图片,收到的图片可以显示在 PictureBox 控件中。
服务端:
```csharp
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace SocketServer
{
public partial class FormServer : Form
{
private TcpListener listener;
private TcpClient client;
private NetworkStream stream;
private byte[] buffer;
public FormServer()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
listener = new TcpListener(IPAddress.Any, 8000);
listener.Start();
listBoxLog.Items.Add("Waiting for client...");
buttonStart.Enabled = false;
buttonStop.Enabled = true;
AcceptClient();
}
private async void AcceptClient()
{
client = await listener.AcceptTcpClientAsync();
listBoxLog.Items.Add("Client connected.");
stream = client.GetStream();
buffer = new byte[client.ReceiveBufferSize];
ReceiveImage();
}
private async void ReceiveImage()
{
int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
MemoryStream ms = new MemoryStream(buffer, 0, bytesRead);
pictureBoxServer.Image = System.Drawing.Image.FromStream(ms);
listBoxLog.Items.Add(string.Format("Image received. Size: {0} bytes.", bytesRead));
}
ReceiveImage();
}
private void buttonStop_Click(object sender, EventArgs e)
{
if (stream != null)
stream.Close();
if (client != null)
client.Close();
if (listener != null)
listener.Stop();
listBoxLog.Items.Add("Server stopped.");
buttonStart.Enabled = true;
buttonStop.Enabled = false;
}
}
}
```
客户端:
```csharp
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace SocketClient
{
public partial class FormClient : Form
{
private TcpClient client;
private NetworkStream stream;
private byte[] buffer;
public FormClient()
{
InitializeComponent();
}
private void buttonConnect_Click(object sender, EventArgs e)
{
client = new TcpClient();
client.Connect(textBoxIP.Text, 8000);
stream = client.GetStream();
buffer = new byte[client.ReceiveBufferSize];
buttonConnect.Enabled = false;
buttonDisconnect.Enabled = true;
}
private async void buttonSend_Click(object sender, EventArgs e)
{
if (pictureBoxClient.Image == null)
{
MessageBox.Show("Please choose an image to send.");
return;
}
MemoryStream ms = new MemoryStream();
pictureBoxClient.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = ms.ToArray();
await stream.WriteAsync(imageBytes, 0, imageBytes.Length);
listBoxLog.Items.Add(string.Format("Image sent. Size: {0} bytes.", imageBytes.Length));
}
private void buttonDisconnect_Click(object sender, EventArgs e)
{
if (stream != null)
stream.Close();
if (client != null)
client.Close();
listBoxLog.Items.Add("Client disconnected.");
buttonConnect.Enabled = true;
buttonDisconnect.Enabled = false;
}
private void buttonChoose_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Image Files (*.jpg, *.jpeg, *.png, *.bmp)|*.jpg;*.jpeg;*.png;*.bmp";
if (dialog.ShowDialog() == DialogResult.OK)
{
pictureBoxClient.Image = System.Drawing.Image.FromFile(dialog.FileName);
}
}
}
}
```
注意事项:
1. 在实际应用中,需要考虑网络延迟和传输的数据量大小等因素,可能需要对数据进行分割和压缩等处理。
2. 在传输二进制数据时,需要注意编码方式,通常使用 UTF8 或 Unicode 编码。
3. 在客户端选择图片时,需要过滤掉非图片文件。
阅读全文