首页 > 解决方案 > c# 从流中发送/接收图像

问题描述

我目前已经编写了以下代码。客户端服务器端。我想按需将图像从服务器发送到客户端。该图像将是来自服务器的屏幕截图,因此我将始终使用不同的大小。

发送第一张图像是一项完成得很好的任务。但是当我发送下一个图片框时不会刷新。从调试中我可以看到从服务器到客户端的字节成功通过。但是代码似乎只接收字节而不继续其余代码

“while”循环( client )的“commented”行似乎在调试中工作,但我看不到结果,因为程序卡在 while 循环中,我无法在前台获得应用程序的窗口

我目前正在 Windows 中进行测试,但客户端将进行调整以在 android 上工作

服务器图片 服务器 客户图片 在此处输入图像描述

服务器

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    Socket client;
    private void button2_Click(object sender, EventArgs e)
    {
        Bitmap bmp = TakingScreenshotEx1();
        bmp.Save("1.jpeg", ImageFormat.Jpeg);

        byte[] buffer = ReadImageFile("1.jpeg");
        int v = client.Send(buffer, buffer.Length, SocketFlags.None);
        Console.WriteLine("Image SENT!");
    }

    private Bitmap TakingScreenshotEx1()
    {
        //Create a new bitmap.
        var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                       Screen.PrimaryScreen.Bounds.Height,
                                       PixelFormat.Format32bppArgb);

        // Create a graphics object from the bitmap.
        var g = Graphics.FromImage(bmpScreenshot);

        // Take the screenshot from the upper left corner to the right bottom corner.
        g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                         Screen.PrimaryScreen.Bounds.Y,
                         0,
                         0,
                         Screen.PrimaryScreen.Bounds.Size,
                         CopyPixelOperation.SourceCopy);

        return bmpScreenshot;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
        Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        server.Bind(iep);
        server.Listen(100);
        Console.WriteLine("Waiting for client....");
        client = server.Accept();
    }
    private static byte[] ReadImageFile(String img)
    {
        FileInfo fileinfo = new FileInfo(img);
        byte[] buf = new byte[fileinfo.Length];
        FileStream fs = new FileStream(img, FileMode.Open, FileAccess.Read);
        fs.Read(buf, 0, buf.Length);
        fs.Close();

        GC.ReRegisterForFinalize(fileinfo);
        GC.ReRegisterForFinalize(fs);
        return buf;
    }
}

客户

public Form1()
{
    InitializeComponent();
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] buffer = new byte[1000000];
private void button1_Click(object sender, EventArgs e)
{  

   if(client.Connected != true)
    client.Connect(iep);


 //   while (true)
 //   {
        int v = client.Receive(buffer, buffer.Length, SocketFlags.None);

        Console.WriteLine("Data Received!");

        Stream stream = new MemoryStream(buffer);
        var img = Bitmap.FromStream(stream);
        pictureBox1.Image = img;

 //   }

}

标签: c#imagewinformssocketsstream

解决方案


正如所Peter Duniho指出的那样while loop 阻塞了 UI 线程

我在这里找到了一个合适的解决方案 如何等待线程完成而不阻塞 UI

您还可以检查 Invoking the Main Thread inside Timer Method


推荐阅读