0

Hi friends I know how to draw on form etc. But I am not able to find the logic how to draw waform and then make it seems scrolling like in ECG display units it scrolls. Please help with some simple example.

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Momerath
0
  • Create a form and add a picture box of size 201, 201.
  • Set the BackColor of the picture box to Black
  • Add a label, a button and a timer.
  • Set the Timer Tick event to timer1_Tick.
  • Set the button click event to button1_Click.
  • Set the timer interval to whatever makes you happy. I used 10.

Put this code as your form:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        const int SRCCOPY = 0xcc0020;

        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern int BitBlt(
          IntPtr hdcDest,     // handle to destination DC (device context)
          int nXDest,         // x-coord of destination upper-left corner
          int nYDest,         // y-coord of destination upper-left corner
          int nWidth,         // width of destination rectangle
          int nHeight,        // height of destination rectangle
          IntPtr hdcSrc,      // handle to source DC
          int nXSrc,          // x-coordinate of source upper-left corner
          int nYSrc,          // y-coordinate of source upper-left corner
          System.Int32 dwRop  // raster operation code
          );
        
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            timer1.Enabled = !timer1.Enabled;
        }

        private void timer1_Tick(object sender, EventArgs e) {
            label1.Text = angle.ToString();
            MoveDisplay();
            PlotPoint();
        }

        private void MoveDisplay() {
            int w = pictureBox1.Size.Width;
            int h = pictureBox1.Size.Height;

            Bitmap bsrc = (Bitmap)pictureBox1.Image;
            Bitmap bdst = new Bitmap(w, h);

            Graphics gsrc = pictureBox1.CreateGraphics();
            Graphics gdst = Graphics.FromImage(bdst);

            IntPtr dcsrc = gsrc.GetHdc();
            IntPtr dcdst = gdst.GetHdc();

            BitBlt(dcdst, 0, 0, w - 1, h, dcsrc, 1, 0, SRCCOPY);

            for (int j = 0; j < h; j++) {
                bdst.SetPixel(w - 1, j, Color.Black);
            }

            gsrc.ReleaseHdc();
            gdst.ReleaseHdc();

            pictureBox1.Image = bdst;
        }

        int angle = 0;

        private void PlotPoint() {
            double rad = angle * Math.PI / 180.0;
            double s = Math.Sin(rad);
            int result = (int)((s + 1) * 100);
            angle = (angle + 1) % 360;

            Bitmap b = (Bitmap)pictureBox1.Image;
            b.SetPixel(b.Size.Width - 1, result, Color.Green);
            pictureBox1.Image = b;
        }
    }
}

This will draw a sin wave on the picturebox.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.