Click here to Skip to main content
14,356,475 members
   

C#

 
AnswerRe: Help to get only IPv4 address Pin
Richard Deeming8-Oct-19 9:54
mveRichard Deeming8-Oct-19 9:54 
GeneralRe: Help to get only IPv4 address Pin
mniceguy819-Oct-19 4:51
membermniceguy819-Oct-19 4:51 
GeneralRe: Help to get only IPv4 address Pin
Richard Deeming9-Oct-19 8:36
mveRichard Deeming9-Oct-19 8:36 
GeneralRe: Help to get only IPv4 address Pin
mniceguy819-Oct-19 8:41
membermniceguy819-Oct-19 8:41 
QuestionDoes the Visual studio C#.net have a progressBar and a control line ? Pin
Member 24584677-Oct-19 0:34
memberMember 24584677-Oct-19 0:34 
AnswerRe: Does the Visual studio C#.net have a progressBar and a control line ? Pin
Afzaal Ahmad Zeeshan7-Oct-19 0:44
mveAfzaal Ahmad Zeeshan7-Oct-19 0:44 
GeneralRe: Does the Visual studio C#.net have a progressBar and a control line ? Pin
Member 24584677-Oct-19 17:41
memberMember 24584677-Oct-19 17:41 
QuestionDrawing from a Class library to a pictureBox Pin
Ali Zigon6-Oct-19 6:27
memberAli Zigon6-Oct-19 6:27 
Hi there!

I'm playing around with graphics in C#. I've managed to create a simple application using graphic object to draw some lines and scroling text to a pictureBox. Now I've decided to put my code into a Class (like for further re-use... and mostly for the learning purposes!), but I'm stuck on how to get the graphics back from Class to my pictureBox. The problem (I think!) lies as I'm doing all my drawings in the timer_tick event...

Here's the current code I'm using...
CLASS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Windows.Forms;

namespace Osciloscope
{
    
    public class Osciloscope
    {
        int SCOPE_WIDTH, SCOPE_HEIGHT; // the width and the height of the osciloscope screen
        int SCOPE_HORIZONTAL_CENTER, SCOPE_VERTICAL_CENTER; //center point of the screen
        int SCAN_POINT_X; //number of points in the line (the width of the scope)
        int SCAN_POINT_Y; //as above but vertical (input...)
        int SCAN_POINT_X_NEW;
        int SCAN_POINT_Y_NEW;


        //Image PictureBox;
        //Graphics _Image; // get the bitmap
                                //Timer t;

        int TIMER_INTERVAL;
        Bitmap _Image;
        Graphics g;
        Pen p;

        public Osciloscope(PictureBox picture, bool Run, int Interval)
        {
            SCOPE_HEIGHT = picture.Height;
            SCOPE_WIDTH = picture.Width;
            TIMER_INTERVAL = Interval;
             //calculate mid point if the scope
            SCOPE_HORIZONTAL_CENTER = SCOPE_WIDTH / 2;
            SCOPE_VERTICAL_CENTER = SCOPE_HEIGHT / 2;
            SCAN_POINT_X = 1;// leftmost point on the screen (start scan)
            SCAN_POINT_Y = SCOPE_VERTICAL_CENTER; //the line runs in the middle of the screen

            //_Image = img;
            _Image= new Bitmap(SCOPE_HEIGHT, SCOPE_WIDTH);
            Timer t = new Timer();
            t.Tick += new EventHandler(t_Tick);
            if (Run)
            {
                t.Enabled = true;
                t.Interval = TIMER_INTERVAL;
                t.Start();
            }
            else
            {
                t.Stop();
            }

        }

        void t_Tick(object sender, EventArgs e)
        {
            g = Graphics.FromImage(_Image);
            //g.Clear(Color.Blue);
            
            p = new Pen(Color.Green, 2f);

            SCAN_POINT_X_NEW = SCAN_POINT_X + 1;
            SCAN_POINT_Y_NEW = SCAN_POINT_Y; //Math.Sin(SCAN_POINT_X);
            // draw the line
            g.DrawLine(p, SCAN_POINT_X, SCAN_POINT_Y, SCAN_POINT_X_NEW, SCAN_POINT_Y_NEW);

            //bmp = _Image;
            //DrawGraph = _Image;
            // = _scope;
            
            p.Dispose();
            g.Dispose();

            SCAN_POINT_X++;
            if(SCAN_POINT_X==SCOPE_WIDTH)
            {
                SCAN_POINT_X = 1;
            }

        }

        //private Image drawGraph
public Image DrawGraph
        {
            get
            {
                return _Image;
            }
        }
    }
}


... and the form itself...

namespace Osciloscope
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        Osciloscope Osciloscope;
        private void FrmMain_Load(object sender, EventArgs e)
        {
            Osciloscope = new Osciloscope(Scope, true, 5);
            Scope.Image= Osciloscope.DrawGraph;
            //Osciloscope.Image = Scope;
            //Scope.Image = Osciloscope.Image;
           //
        }

    }
}


As I said: I just can't figure out how to get the drawing into the pictureBox... (Scope is the name of the pictureBox!)

Any help woul be highly appreciated!

Regards!

// EDIT: There's no scroling text in current code as I left it out for simplicity...
AnswerRe: Drawing from a Class library to a pictureBox Pin
Richard MacCutchan6-Oct-19 6:48
protectorRichard MacCutchan6-Oct-19 6:48 
GeneralRe: Drawing from a Class library to a pictureBox Pin
Ali Zigon6-Oct-19 7:19
memberAli Zigon6-Oct-19 7:19 
GeneralRe: Drawing from a Class library to a pictureBox Pin
Richard MacCutchan6-Oct-19 8:08
protectorRichard MacCutchan6-Oct-19 8:08 
GeneralRe: Drawing from a Class library to a pictureBox Pin
Ali Zigon6-Oct-19 9:37
memberAli Zigon6-Oct-19 9:37 
GeneralRe: Drawing from a Class library to a pictureBox Pin
Richard MacCutchan6-Oct-19 23:17
protectorRichard MacCutchan6-Oct-19 23:17 
GeneralRe: Drawing from a Class library to a pictureBox Pin
Ali Zigon7-Oct-19 4:31
memberAli Zigon7-Oct-19 4:31 
QuestionReading Access Into SQL - OLE Provider Errors Pin
Kevin Marois4-Oct-19 6:27
professionalKevin Marois4-Oct-19 6:27 
AnswerRe: Reading Access Into SQL - OLE Provider Errors Pin
Richard MacCutchan4-Oct-19 6:42
protectorRichard MacCutchan4-Oct-19 6:42 
GeneralRe: Reading Access Into SQL - OLE Provider Errors Pin
Kevin Marois4-Oct-19 7:46
professionalKevin Marois4-Oct-19 7:46 
GeneralRe: Reading Access Into SQL - OLE Provider Errors Pin
Richard MacCutchan4-Oct-19 7:54
protectorRichard MacCutchan4-Oct-19 7:54 
GeneralRe: Reading Access Into SQL - OLE Provider Errors Pin
Kevin Marois4-Oct-19 8:20
professionalKevin Marois4-Oct-19 8:20 
GeneralRe: Reading Access Into SQL - OLE Provider Errors Pin
Richard MacCutchan4-Oct-19 8:30
protectorRichard MacCutchan4-Oct-19 8:30 
GeneralRe: Reading Access Into SQL - OLE Provider Errors Pin
Kevin Marois4-Oct-19 8:40
professionalKevin Marois4-Oct-19 8:40 
GeneralRe: Reading Access Into SQL - OLE Provider Errors Pin
Richard MacCutchan4-Oct-19 23:37
protectorRichard MacCutchan4-Oct-19 23:37 
GeneralRe: Reading Access Into SQL - OLE Provider Errors Pin
Eddy Vluggen5-Oct-19 10:17
mveEddy Vluggen5-Oct-19 10:17 
GeneralRe: Reading Access Into SQL - OLE Provider Errors Pin
Kevin Marois6-Oct-19 7:25
professionalKevin Marois6-Oct-19 7:25 
GeneralRe: Reading Access Into SQL - OLE Provider Errors Pin
Eddy Vluggen6-Oct-19 7:52
mveEddy Vluggen6-Oct-19 7:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.