I am developing the GUI on C# as windows form Application. I am receiving the data stream from Client (as a console App) on GUI Server (as windows form App) using TCP/IP protocol. The stream pattern receives like:
The trouble I am facing in the graphics plotting. I plot two graphics objects on an image in a picture box. While plotting the graphics the second shows flickering while plotting. I really don't understand why it produces flicker in second (Red) graphic plot. I am sharing my Server code here, Please tell me where I am mistaken.
Server Code (GUI):
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Threading;
namespace GUI_Server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread listener;
Int32 port = 3000;
IPAddress ip = IPAddress.Parse("127.0.0.1");
ArrayList nSockets;
String[] PART = null;
String data = null;
private System.Drawing.Graphics g, path;
Double w, x, y, z;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = new Bitmap("Image");
g = pictureBox1.CreateGraphics();
path = Graphics.FromImage(pictureBox1.Image);
nSockets = new ArrayList();
label1.Text = "IP Address:" + ip;
listener = new Thread(listen);
listener.Start();
}
public void listen()//thread
{
TcpListener tcpListener = new TcpListener(ip, port);
tcpListener.Start();
while (true)
{
Socket handlerSocket = tcpListener.AcceptSocket();
if (handlerSocket.Connected)
{
Control.CheckForIllegalCrossThreadCalls = false;
label2.Text = "Connected";
lock (this)
{
nSockets.Add(handlerSocket);
}
ThreadStart thdstHandler1 = new ThreadStart(handlerThread1);
Thread thdHandler1 = new Thread(thdstHandler1);
thdHandler1.Start();
}
}
}
public void handlerThread1() // thread
{
Socket handlerSocket = (Socket)nSockets[nSockets.Count - 1];
NetworkStream networkStream = new NetworkStream(handlerSocket);
Byte[] bytes = new Byte[1024];
int k;
lock (this)
{
while ((k = networkStream.Read(bytes, 0, bytes.Length)) != 0)
{
data = Encoding.ASCII.GetString(bytes, 0, k);
PART = data.Split('\t');
if (Convert.ToDouble(part[0]) == 1)
{
w = (Convert.ToDouble(part[1]) / 0.0347) + 60;
x = (Convert.ToDouble(part[2]) / 0.0335) + 656;
textBox1.Text = ("Tag ID_Blue: " + part[0] + "\t" + "X: " + part[2] + "\t" + "Y: " + part[1]);
}
g.DrawRectangle(new Pen(Color.Blue, 3), Convert.ToInt32(x),
Convert.ToInt32(w), 6, 6); // first graphic
if (Convert.ToDouble(part[0]) == 2)
{
y = (Convert.ToDouble(part[1]) / 0.0347) + 60;
z = (Convert.ToDouble(part[2]) / 0.0335) + 656;
textBox2.Text = ("Tag ID_Red: " + part[0] + "\t" + "X: " + part[2] + "\t" + "Y: " + part[1]);
}
g.DrawRectangle(new Pen(Color.Red, 3), Convert.ToInt32(z), Convert.ToInt32(y), 6, 6); // second graphic
Thread.Sleep (50);
pictureBox1.Refresh();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
networkStream.Write(msg, 0, msg.Length);
}
}
handlerSocket = null;
}
is there any way to refresh just graphics, not the whole picture box. Does the flickering occur due to refresh the picture box?