0
我想要C#套接字發送圖像。它有效,但不穩定。發送的圖像非常大,更新速度非常快,從而導致它瞬間閃爍。我正在尋找一種方法來壓縮發送的數據,如果可能的話。我使用此代碼:壓縮通過C#套接字發送的數據?
服務器端:
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;
}
客戶端:
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);
}
用gzip壓縮你的圖片嗎?使用JPEG等有損格式重新編碼圖像是否公平? – sarnold
「閃爍」與通過套接字發送數據無關。你大概只需要利用[雙緩衝](http://en.wikipedia.org/wiki/Multiple_buffering#Double_buffering_in_computer_graphics)。 –
我會看看gzip,謝謝。此外,閃爍是綠色/藍色,並使圖像失真或不合適。當它需要雙緩衝時,它不像正常的閃爍。不過謝謝。 –