使用TCP在网络上发送位图时遇到问题

时间:2012-09-08 10:49:42

标签: c# networking bitmap

我正在尝试在C#中设置一个简单的桌面监控程序。我一直在寻找一个工作模型差不多一个小时,但是我找到的任何东西都会抛出异常或太复杂,调用模糊的函数。

我能够将屏幕捕获到位图实例,并且我在尝试发送它时遇到了麻烦,但此刻,程序正常运行并关闭。客户端抛出异常,因为连接未正确关闭(懒惰我!),但没有其他事情发生。没有输出文件,没有。

这是服务器的代码

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();
        }
    }
}

这是客户的代码:

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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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();
}

接收方的代码并没有太大改变文件扩展名。

以下答案有助于解释这些变化: https://stackoverflow.com/a/8308142/1698182

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