[英]Compress the data sent via a C# socket?
I'm trying C# sockets to send images. 我正在尝试使用C#套接字发送图像。 It works, but it's unstable. 它可以工作,但是不稳定。 The images sent through are quite large and are updated very quickly which causes it to flicker every now and then. 发送的图像很大,并且更新非常快,这使其不时闪烁。 I'm looking for a way to compress the data sent if possible. 我正在寻找一种方法来压缩发送的数据(如果可能)。 I'm using this code: 我正在使用此代码:
Server side: 服务器端:
System.IO.MemoryStream stream = new System.IO.MemoryStream();
// !! Code here that captures the screen !!
bitmap.Save(stream, myImageCodecInfo, myEncoderParameters);
byte[] imageBytes = stream.ToArray();
stream.Dispose();
// Send the image
clientSocket.Send(imageBytes);
// Empty the byte array?
for (int i = 0; i < imageBytes.Length; i++)
{
imageBytes[i] = 0;
}
Client side: 客户端:
private void OnConnect(IAsyncResult ar)
{
try
{
MessageBox.Show("Connected");
//Start listening to the data asynchronously
clientSocket.BeginReceive(byteData,
0,
byteData.Length,
SocketFlags.None,
new AsyncCallback(OnReceive),
null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Stream Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OnReceive(IAsyncResult ar)
{
try
{
int byteCount = clientSocket.EndReceive(ar);
// Display the image on the pictureBox
MemoryStream ms = new MemoryStream(byteData);
pictureBox1.Image = Image.FromStream(ms);
}
catch (ArgumentException e)
{
//MessageBox.Show(e.Message);
}
clientSocket.BeginReceive(byteData,0,byteData.Length,SocketFlags.None,new AsyncCallback(OnReceive),null);
}
I ended up using gzip. 我最终使用了gzip。
Turns out the flicker wasn't due to it being updated very quickly, but was because of the way I had sockets set up. 原来,闪烁不是因为更新非常快,而是因为我设置了套接字。 It wasn't sending the whole image. 它没有发送整个图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.