none
Draw coordinates in the pictureBox RRS feed

  • Question

  • hi 

    i have Lots of coordinates of x and y that they come from a serial port .now i want to paint this x and y coordinates in a pictureBox in real time, how can i do this?

    • Moved by CoolDadTx Wednesday, May 30, 2018 2:32 PM Winforms related
    Wednesday, May 30, 2018 11:14 AM

All replies

  • You can't. .NET is not real-time capable.

    Wednesday, May 30, 2018 11:16 AM
  • So what is your offer?

    Wednesday, May 30, 2018 11:22 AM
  • So what is your offer?

    I don't offer anything, your problem is not solvable under the given requirements.

    btw, we don't paint into a picture box. Cause it is a specialized control for displaying images. We draw simply on the area, we want to. E.g. on a panel:

    namespace WindowsFormsCS
    {
        using System;
        using System.Drawing;
        using System.Windows.Forms;
    
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void panel1_Paint(object sender, PaintEventArgs e)
            {
                Panel panel = sender as Panel;
                Graphics graphics = e.Graphics;
                for (int x = 0; x < panel.Width; x++)
                {
                    int y = (int)(panel.Height / 2.0 + Math.Sin((double)x/panel.Width*Math.PI *2.0) * panel.Height / 2.0);
                    graphics.FillRectangle(Brushes.Black, x, y, 3, 3);
                }
            }
        }
    }

     

    Wednesday, May 30, 2018 12:31 PM
  • You can draw points on the screen as they come in from the serial port but be aware this isn't real time. If your code isn't fast enough to keep up you can lose some data. It might be a good idea to use a little threading if the speed is really fast but try without it first.

    I wouldn't use a PictureBox though. That is designed for images. Instead create a custom control (derived from Control). To make it easier to work with I'd recommend that you have some sort of Add method that takes the coordinates. When called it adds the value to a list being managed by the control. When the control's OnPaint method is called it simply enumerates the points and draws each one. 

    There are lots of things to consider though.

    1. The serial port data receive event may not occur on the UI thread unless you configured it as such so you might have to Invoke over to the UI thread.
    2. The Add method needs to add the point and then draw the point. It could refresh the control but this seems wasteful.
    3. You may want to limit the # of points displayed otherwise you'll just eat up memory over time.
    4. When the control is covered up and then uncovered it needs to repaint itself. Winforms will handle this and by having the values in a list you can easily enumerate back through them to render the image again.
    5. The more points you have the slower it will render.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, May 30, 2018 2:32 PM
  • Hello,

    I don't see problem draw that data but I see some situations that can make your work harder. Not clear info from you if you're Master or Slave on bus. This is first important thing because if you're Master you can much better handle that. You need poll Slave for data and better use your time for comm exchange with draw. Question is how quickly external data change so sugestion about not realtime draw is correct, simply your draw output will be delayed due characteristic of your communication. In case you're Slave, no good probably, all is depended to Master's send frequency and missed data due freq with your recv and draw. What is that freq if this is your case? In other side pls try think you're that application and think about logical steps as program need to do. Then you can have better look to real requirements.


    • Edited by Duris_I Friday, June 1, 2018 10:21 AM
    Friday, June 1, 2018 10:04 AM