August
---
2003




Oscilloscope in C#

Still working with software for flow cytometry instruments, I needed a quick and informal way of visuall representing an incoming variable stream of data as a trace, just like an oscilloscope. Using GDI+ in C# to trace a line defined by points and redrawn every millisecond with random values. Ofcourse, instead of the random values one could use values provided by a source and display a correct trace.

oscilloscope oscilloscope

Oscilloscope.zip (6kb)

cs code

 private void initHistogram(){
  myRectangle	= new Rectangle(10, 10, 200, 200);
  mDataPen	= new Pen(Color.Navy, 2);
  myGraphic	= this.CreateGraphics();
  this.button1.Enabled = false;
  timer1.Stop();
  timer1.Enabled = false;
  timer1.Enabled = true;
 }

 private void button1_Click(object sender, System.EventArgs e) {			
  this.button1.Enabled = false;
  timer1.Stop();
  timer1.Enabled = false;
  timer1.Enabled = true;
 }

 private void DrawData(){
  populateArray2();
  Point[] pts = new Point[mData.Count];

  for (int i=0; i < mData.Count; i++){
   pts[i].X = LeftOffset + ((Point)mData[i]).X;
   pts[i].Y = myRectangle.Height  - (BottomOffset + ((Point)mData[i]).Y);
  }

  myGraphic.Clear(System.Drawing.SystemColors.Control);
  myGraphic.DrawRectangle(
   Pens.Black, 
   4, 
   4, 
   myRectangle.Width - 1, 
   myRectangle.Height - 1);

  pMaxValue = myRectangle.Height-(BottomOffset + pMax);
  this.label1.Text = pMax.ToString();
  this.label1.Location = new Point(10, pMaxValue-25);

  myGraphic.DrawLine(Pens.Black,10,pMaxValue,180,pMaxValue);
  myGraphic.DrawCurve(mDataPen, pts);
 }

 public void timer1_Tick(object sender, System.EventArgs e){
  DrawData();
 }

 private void populateArray2(){
  mData.Clear();

  for(int i=0;i<loopCount;i=i+trackBar3.Value){
   if (
   i < (loopCount/2) - trackBar2.Value || 
   i > (loopCount/2) + trackBar2.Value){
    rndnum = r.Next(0,10);
   }else{
    rndnum = r.Next(trackBar1.Value - trackBar4.Value/2,trackBar1.Value);
   }

   mData.Add(new Point(i, rndnum));

   if (i == 0){
    pMax = rndnum;
   }else{
    if(rndnum > pMax){
     pMax = rndnum;
    }
   }
  }
 }