Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

GZIP Compress/Decompress [TCP Transfer]

$
0
0

Hello,

I've been mixing with the GZIP for a while and can't figure out how to compress an Image -> Send it over TCP -> and decompress. 

I can send a bitmap but as fast it's compressed I can't decompress it on the "other side" of the connection.

I send it using:

 MemoryStream ms = new MemoryStream();

            // Take Image
            int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
            int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;

            Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
            bmpScreenShot = bmp;
            bmpScreenShot.Save(ms, ImageFormat.Jpeg);
            bmpScreenShot.Dispose();

            byte[] fileBuffer = new byte[ms.Length];
            TcpClient clientSocket = new TcpClient(IP, Port);
            NetworkStream networkStream = clientSocket.GetStream();
            networkStream.Write(ms.ToArray(), 0, fileBuffer.GetLength(0));
            networkStream.Close();
            ms.Close();

and then receive it using multiple threads, though this one gets the picture to the forms background:

 public void HandlerThread()
        {
            Socket handlerSocket = (Socket)alSockets[alSockets.Count - 1];
            NetworkStream networkStream = new NetworkStream(handlerSocket);
            int thisRead = 0;
            int blockSize = 1024;
            Byte[] dataByte = new Byte[blockSize];
            lock (this)
            {                             
                    MemoryStream ms = new MemoryStream();
                    while (true)
                    {
                        thisRead = networkStream.Read(dataByte, 0, blockSize);
                        ms.Write(dataByte, 0, thisRead);    
                        if (thisRead == 0)                       
                        break;                        
                    }                   
                    BackgroundImage = byteArrayToImage(ToByteArray(ms));          
                    dataByte = null;                             
            }
        }      

How could I possible compress this and decompress again?

Many thanks in advice //

Johan


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>