?
Solved

Draw graphics on a form c#

Posted on 2008-11-13
5
Medium Priority
?
715 Views
Last Modified: 2013-12-17
Hi,

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()
{
      InitializeComponent();
      Graphics chartGraphics = CreateGraphics();
      Pen pen = new Pen(Color.Black, 5);
      chartGraphics.DrawEllipse(pen, 50, 50, 50, 50);
      
      pen.Dispose();
      chartGraphics.Dispose();
}

thanks
0
Comment
Question by:IndigoTeam
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
DarrenD earned 2000 total points
ID: 22949282
   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 );

    }
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 22949305
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.

See:

http://msdn.microsoft.com/en-us/library/cyh3c8h8.aspx

Cheers,

Darren
0
 
LVL 26

Expert Comment

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

Graphics Programming Using C#
http://www.developer.com/net/csharp/article.php/1435391

C# / C Sharp » 2D Graphics » Mouse Draw
http://www.java2s.com/Code/CSharp/2D-Graphics/DrawingShapes.htm
0
 

Author Closing Comment

by:IndigoTeam
ID: 31516337
thanks a lot
0
 

Author Comment

by:IndigoTeam
ID: 22949711
thanks all
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

749 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