当我将位图从服务器发送到客户端时OutOfMemoryException

时间:2012-08-03 21:06:55

标签: sockets bitmap out-of-memory

我在服务器端将面板屏幕作为位图,并尝试将其发送到客户端并放置在客户端的另一个面板上。但是我得到了OutOfMemoryException。服务器代码:

{
    TcpClient tcpClient = (TcpClient) client;
    NetworkStream clientStream = tcpClient.GetStream();
    MemoryStream ms = new MemoryStream();
    Bitmap bmp = new Bitmap(Pnl_Board.Width, Pnl_Board.Height);

    while (tcpClient.Connected) {

        Pnl_Board.DrawToBitmap(bmp, Pnl_Board.Bounds);

        bmp.Save(ms, ImageFormat.Bmp);

        byte[] imageBuffer = ms.GetBuffer();
        clientStream.Write(imageBuffer, 0, (int) ms.Length);
        clientStream.Flush();

    }

    tcpClient.Close();
}

和客户端代码:

this.client = new TcpClient();
this.client.Connect(IPAddress.Parse("127.0.0.1"), 3000);
this.stream = client.GetStream();
while (this.client.Connected) {
    Bitmap bmp = (Bitmap) Image.FromStream(stream); // I get error here
    PnlBoard.BackgroundImage = bmp;

}
this.stream.Close();
this.client.Close();

0 个答案:

没有答案