save up to 40%

How to draw a line on picture box control in a Windows application?

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

How to draw a line on picture box control in a Windows application?

Rating:

The steps to draw a line on picture box control are as follows:

  1. Add a picture box control to the Windows form as follows:

    private System.Windows.Forms.PictureBox pictureBox1;

  2. Write the following code in the Form1_Load event:

    pictureBox1.Dock = DockStyle.Fill; //optional
    pictureBox1.BackColor = Color.White; //optional
    pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); this.Controls.Add(pictureBox1);

  3. Write the following code in the pictureBox_Paint Event:

    Graphics g = e.Graphics;
    g.DrawString("This is a diagonal line drawn on the control",
    new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
    g.DrawLine(System.Drawing.Pens.Red, pictureBox1.Left, pictureBox1.Top,
    pictureBox1.Right, pictureBox1.Bottom);


  4. The g.DrawString() method draws the given string on the picture box control and the g.DrawLine() method draws the line on the picture box control.

Note: The DrawLine() method takes the parameters as follows:
g.DrawLine(Pen object, int x1, int y1, int x2, int y2);


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us | News and Press Release | uCertify India
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.