Asked by:
Draw coordinates in the pictureBox

Question
-
All replies
-
-
-
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); } } } }
-
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.
- 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.
- The Add method needs to add the point and then draw the point. It could refresh the control but this seems wasteful.
- You may want to limit the # of points displayed otherwise you'll just eat up memory over time.
- 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.
- The more points you have the slower it will render.
Michael Taylor http://www.michaeltaylorp3.net
-
Perhaps you should try using a Windows.Forms ChartControl which is what it is for.
Also see;
Tutorial: Creating a Basic Chart
Getting Started with Chart Controls
Windows Forms Samples Environment for Microsoft Chart Controls
La vida loca
-
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