static void work_thread() { int [] temp = new int[4]; int [] buffer = null; int size; int i, j, k; Graphics g; do { try { g = Graphics.FromImage(bitmap); g.Clear(Color.Black); g.Dispose(); g = null; pBox.Invalidate(); if (bDemoMode) { if (demo_list != null) { if (demo_list.Count > demo_iter) { xmin = ((ExamplePoint)demo_list[demo_iter]).xmin; ymin = ((ExamplePoint)demo_list[demo_iter]).ymin; xmax = ((ExamplePoint)demo_list[demo_iter]).xmax; ymax = ((ExamplePoint)demo_list[demo_iter]).ymax; nMax = ((ExamplePoint)demo_list[demo_iter]).max_iter; if (((ExamplePoint)demo_list[demo_iter]).name != null) { form.Text = "Mandelbrot Viewer - " + ((ExamplePoint)demo_list[demo_iter]).name; } else { form.Text = String.Format("Mandelbrot Viewer - {0}", demo_iter); } colors = new Color[nMax + 1]; ColorRainbow.Make_color_array(nNumColors, colors); colors[nMax] = Color.FromKnownColor(KnownColor.Black); // add one on the top to avoid edge errors } demo_iter++; if (demo_iter >= demo_list.Count) { demo_iter = 0; } } } if (colors.Length != nMax + 1) { colors = new Color[nMax + 1]; ColorRainbow.Make_color_array(nNumColors, colors); colors[nMax] = Color.FromKnownColor(KnownColor.Black); // add one on the top to avoid edge errors } sock_writer.Write(xmin); sock_writer.Write(ymin); sock_writer.Write(xmax); sock_writer.Write(ymax); sock_writer.Write(nMax); for (;;) { temp[0] = sock_reader.ReadInt32(); temp[1] = sock_reader.ReadInt32(); temp[2] = sock_reader.ReadInt32(); temp[3] = sock_reader.ReadInt32(); if (temp[0] == 0 && temp[1] == 0 && temp[2] == 0 && temp[3] == 0) { if (bDemoMode) { Thread.Sleep(5000); break; } bDrawing = false; return; } size = (temp[1] + 1 - temp[0]) * (temp[3] + 1 - temp[2]); buffer = new int[size]; for (i = 0; i < size; i++) { buffer[i] = sock_reader.ReadInt32(); } int max_color = colors.Length; Random rand = new Random(); try { lock (bitmap) { k = 0; for (j = temp[2]; j <= temp[3]; j++) { for (i = temp[0]; i <= temp[1]; i++) { //bitmap.SetPixel(i, j, colors[buffer[k]]); if (buffer[k] >= 0 && buffer[k] < max_color) { bitmap.SetPixel(i, j, colors[buffer[k]]); } else { bitmap.SetPixel(i, j, Color.FromArgb(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255))); } k++; } } } } catch (Exception e) { MessageBox.Show("exception thrown while accessing bitmap: " + e.Message, "Error"); } pBox.Invalidate(); } } catch (Exception e) { // do something with the exception MessageBox.Show("Exception thrown in worker thread: " + e.Message, "Error"); break; } } while (bDemoMode); bDrawing = false; }
private void connect_button_Click(object sender, System.EventArgs e) { if (bConnected) { MessageBox.Show("You may only connect once", "Note"); return; } try { sock = new TcpClient(host.Text, Convert.ToInt32(port.Text)); if (sock == null) { MessageBox.Show("Unable to connect to " + host.Text + " on port " + port.Text, "Error"); return; } } catch (SocketException exception) { MessageBox.Show("Unable to connect to " + host.Text + " on port " + port.Text + ", " + exception.Message, "Error"); return; } bConnected = true; sock_writer = new BinaryWriter(sock.GetStream()); sock_reader = new BinaryReader(sock.GetStream()); nWidth = sock_reader.ReadInt32(); nHeight = sock_reader.ReadInt32(); nNumColors = sock_reader.ReadInt32(); nMax = sock_reader.ReadInt32(); //MessageBox.Show(String.Format("Width: {0}, Height: {1}, num_colors: {2}", nWidth, nHeight, nNumColors), "Note"); // validate input values if (nWidth > 2048) { nWidth = 2048; } if (nWidth < 1) { nWidth = 768; } if (nHeight > 2048) { nHeight = 2048; } if (nHeight < 1) { nHeight = 768; } if (nNumColors > 1024) { nNumColors = 1024; } if (nNumColors < 1) { nNumColors = 128; } if (nMax < 1) { nMax = 100; } if (nMax > 5000) { nMax = 5000; } colors = new Color[nMax + 1]; ColorRainbow.Make_color_array(nNumColors, colors); colors[nMax] = Color.FromKnownColor(KnownColor.Black); // add one on the top to avoid edge errors bitmap = new Bitmap(nWidth, nHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Graphics g; g = Graphics.FromImage(bitmap); g.Clear(Color.FromKnownColor(KnownColor.Black)); g.Dispose(); /* * Rectangle rButton = connect_button.Bounds; * Rectangle rBox = outputBox.Bounds; * outputBox.SetBounds(rButton.Left, rButton.Top, rBox.Width, rBox.Height + (rBox.Top - rButton.Top)); * connect_button.Hide(); * host.Hide(); * port.Hide(); * outputBox.Invalidate(); */ connect_button.Enabled = false; host.ReadOnly = true; port.ReadOnly = true; if (demo_list != null && demo_list.Count > 0) { go_stop_button.Enabled = true; } bDrawing = true; pBox = outputBox; ThreadStart threadProc = new ThreadStart(work_thread); thread = new Thread(threadProc); thread.Start(); }