JJC
JJC

Reputation: 25

Having problems sending a bitmap over the network using TCP

I'm trying to set up a simple desktop monitoring program in C#. I've been searching around for almost an hour trying to find a working model, however anything I found either threw exceptions or was too darn complicated, making calls to obscure functions.

I am able to capture the screen to a bitmap instance and I have had a crack at trying to send it, but at the moment, the programs run normally and close. The client throws an exception because the connection wasn't closed properly (lazy me!), but nothing else happens. No output file is written to, nothing.

Here is the server's code

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using System.Net.Sockets;
using System;

class ScreenCapture
{
    public static void Main()
    {
        try
        {
            TcpListener srv = new TcpListener(IPAddress.Any, 51530);
            srv.Start(1);
            TcpClient client = srv.AcceptTcpClient();
            NetworkStream ns = client.GetStream();
            StreamWriter send = new StreamWriter(ns);
            StreamReader read = new StreamReader(ns);

            Rectangle screenshot;
            Bitmap bitmap;
            screenshot = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
            bitmap = new Bitmap(screenshot.Width, screenshot.Height, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(bitmap);
            g.CopyFromScreen(screenshot.Left, screenshot.Top, 0, 0, screenshot.Size);
            g.Dispose();
            MemoryStream m = new MemoryStream();
            bitmap.Save(m, ImageFormat.Jpeg);

            byte[] data = m.ToArray();
            Console.WriteLine(data.ToString());
            send.Write(m);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Console.ReadKey();
        }
    }
}

Here's the client's code:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Drawing;
using System.Drawing.Imaging;

class RetrieveScreenShot
{
    public static void Main()
    {
        try
        {
            TcpClient client = new TcpClient("127.0.0.1", 51530);
            NetworkStream ns = client.GetStream();
            Image receivedImage = Image.FromStream(ns);
            receivedImage.Save("output.bmp");
            ns.Close();
            client.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Console.ReadLine();
        }
    }
}

Upvotes: 0

Views: 1760

Answers (1)

Michael Shaffer
Michael Shaffer

Reputation: 374

try
{
   TcpListener srv = new TcpListener(IPAddress.Any, 51530);
   srv.Start(1);
   TcpClient client = srv.AcceptTcpClient();
   NetworkStream ns = client.GetStream();

   Rectangle screenshot;
   Bitmap bitmap;
   screenshot = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
   bitmap = new Bitmap(screenshot.Width, screenshot.Height, PixelFormat.Format32bppArgb);
   Graphics g = Graphics.FromImage(bitmap);
   g.CopyFromScreen(screenshot.Left, screenshot.Top, 0, 0, screenshot.Size);
   g.Dispose();

   MemoryStream m = new MemoryStream();

   //you can also just save to network stream and skip the copy but i kept it for demo
   bitmap.Save(m, ImageFormat.Jpeg);

   //reset the memory stream to start of stream
   m.Position = 0;
   //copy memory stream to network stream
   m.CopyTo(ns);
   //make sure copy is completed
   m.Flush();
   m.Close();

   //Makes sure everything is sent before closing it
   ns.Flush();

   //The Image.FromStream() seems to wait for the stream to be finished/closed.
   client.Close();

}
catch (Exception ex)
{
   Console.WriteLine(ex.ToString());
   Console.ReadKey();
}

The code on the Receiver side did not change much just the file extension.

The following answers help to explain the changes: https://stackoverflow.com/a/8308142/1698182

https://stackoverflow.com/a/22906136/1698182

Upvotes: 1

Related Questions