Draw graphics on a form c#

Posted on 2008-11-13
Last Modified: 2013-12-17

I'm creating a winforms app and need to draw on the for and other controls, but i can't get the graphics to work - nothing displays (except a button i've added). here is my code

public Form1()
      Graphics chartGraphics = CreateGraphics();
      Pen pen = new Pen(Color.Black, 5);
      chartGraphics.DrawEllipse(pen, 50, 50, 50, 50);

Question by:IndigoTeam
    LVL 18

    Accepted Solution

       protected override void OnPaint(PaintEventArgs pe)

            Graphics g = pe.Graphics ;
            Pen pn = new Pen( Color.Blue );
            // Rectangle rect = new Rectangle(50, 50, 200, 100);
            Point pt1 = new Point( 30, 30);
            Point pt2 = new Point( 110, 100);
            g.DrawLine( pn, pt1, pt2 );

    LVL 18

    Expert Comment

    Hi Again,

    You cannot call this method in the Load event handler. The drawn content will not be redrawn if the form is resized or obscured by another form. To make your content automatically repaint, you should override the OnPaint method.



    LVL 26

    Expert Comment

    by:Anurag Thakur
    have a look at the following links
    might be useful

    Graphics Programming Using C#

    C# / C Sharp » 2D Graphics » Mouse Draw

    Author Closing Comment

    thanks a lot

    Author Comment

    thanks all

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now