C # LAN desktop sharing software (I)
If you run this software to view traffic monitoring, you will find 1 ~ Upload and download at around 2 Mb/s, and sometimes the error "invalid parameter" is reported. If you save the screen locally, each image may be 4 Mbit/s (bmp), 120KB (jpg ), 10 images are transmitted in one second. Therefore, the network traffic usage is high. We will solve these problems one by one.
1. Compress client Images
Compression function (using System. Drawing. Imaging; required ;)
= = = = = .getImageCoderInfo(== EncoderParameters(= EncoderParameter(ecd, ] = ImageCodecInfo getImageCoderInfo(== (ImageCodecInfo ici =
The client thread execution body is changed
(= [] b =
After compression, the image size is only about 38 kb.
2. Server Error Handling
The error "invalid parameter" is caused
[] B = [*]; = Image. FromStream (ms); // ms data error cannot be converted to Image
Why is ms invalid? The reason is that the image size exceeds the size of byte array B, or the returned data is lost or empty content.
Solution: Since the byte array capacity is small, we will increase its capacity. For example, byte [] B = new byte [1024*10000]; increased by 10 times
Because we have already compressed images on the client, we don't have to worry about this.
Another point is to determine whether the data is an image after receiving the data, and discard it if not.
Implementation Code
getImage(MemoryStream ms,= =
The server thread execution body is changed
([] b = [ * = (getImage(ms, =
No one will ask why not.
Int len = 0;
Byte [] B = new byte [1024];
While (len = hostSocket. Receive (B)> 0)
{Ms. write (B, 0, B. lenth)} receives data cyclically to save memory allocation.
It should be noted that, if you still have a better solution, please share it with us. Of course, this program is not complete yet. The above is just a solution to the problem.