public void ReceiveCallback(IAsyncResult ar) { UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u; IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e; if (open) { bytes = u.EndReceive(ar, ref e); if (bytes[0] == 0xFF && bytes[1] == 0xFF) { output.Invoke((MethodInvoker)delegate { output.Text = Encoding.GetEncoding(437).GetString(bytes, 2, bytes.Length - 2); }); } else { int y = ((bytes[1] << 8) | bytes[0]) / 2; interlace = (byte)(bytes[0] & 0x1); for (int x = 0; x < bytes.Length - 2; x++) { if (y < frameHeight && x < frameWidth) { imageData[x + (y * frameWidth)] = (ushort)(((ushort)bytes[x * 2 + 3] << 8) | bytes[x * 2 + 2]); } } if (y == frameHeight / 2 - 1) { updateImage(); } } } if (open) { UdpState s = new UdpState(); s.e = e; s.u = u; u.BeginReceive(new AsyncCallback(ReceiveCallback), s); } }
private void button2_Click(object sender, EventArgs e) { if (!open) { frameWidth = int.Parse(widthTextBox.Text); frameHeight = int.Parse(heightTextBox.Text); pictureBox1.Width = frameWidth; pictureBox1.Height = frameHeight; button2.Text = "Stop Listening"; udpClient = new UdpClient(0xF00D); udpClient.Client.ReceiveBufferSize = frameWidth * frameHeight * 10; open = true; b = new Bitmap(frameWidth, frameHeight, PixelFormat.Format16bppRgb565); imageData = new ushort[frameWidth * frameHeight / 2]; UdpState s = new UdpState(); s.e = RemoteIpEndPoint; s.u = udpClient; udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s); } else { open = false; button2.Text = "Start Listening"; udpClient.Close(); } }