Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Chart Success: GDI+ Graphics at work. Part 1  »  Drawing Basics
 »  Home  »  Windows Development  »  Graphics  »  Chart Success: GDI+ Graphics at work. Part 1  »  Drawing Basics
Chart Success: GDI+ Graphics at work. Part 1
by Ged Mead | Published  03/06/2005 | .NET Newbie Graphics | Rating:
Drawing Basics

 Drawing and Persisting

     One of the common problems for newcomers to graphics in .Net is the tricky business of persistence.   Unless you get your drawing code correctly written and in the right place it may appear the first time, but will disappear partially or totally when the form is moved or if other windows are displayed on top of it and in fact in several other situations.  

     Different scenarios will call for different solutions to this problem.   When drawing directly on the form you will often be able to avoid the “disappearing graphic” problem simply by placing your code in the form’s OnPaint event.   This is the approach we will use for our Pie Chart demonstration.

   If you haven’t accessed OnPaint before, you can locate it by selecting “(Overrides)”  from the left hand dropdown list in the Code Window and then scrolling down, quite a long way, until you find OnPaint.

   Controlling when and how often drawings are refreshed is a topic we will address many times in the coming series of articles.   For now, we will try to keep it simple. 

The Graphics Object
   The form has a Graphics Object associated with it.   Some people like to think of this object as a canvas on which everything that appears on the
surface of the form is drawn.   An overlay on the form, perhaps.     And for our purposes in this article, that description is certainly accurate enough.

   We write code for this Graphics Object, which opens the way for us to use the wide range of drawing methods and properties that subsequently become available.   You will see a couple of examples of this very shortly.

Getting Started
    First, we create a variable and assign the Form’s Graphics Object to it:

   Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
   Dim g As Graphics = e.Graphics

   If you are entering this code step by step using Visual Studio as you read this article, then try starting a new line of code and typing “g.” .   Intellisense will immediately show you the very impressive range of methods and properties on hand for your use and pleasure.

   For now though, we are going to limit ourselves to setting the SmoothingMode property.   Selecting a SmoothingMode of HighQuality will reduce jagged lines effect to a minimum.  Most quality improvements come at a price and very often, the price is reduced speed of display.    However, our example is undemanding on resources, so this is unlikely to be a factor that you need worry about.

  Add this line to the OnPaint event:

  g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

Location and Size

   First, we have to decide two things:
1. Where we want to place the chart on the form
2. What size we want the chart to be.

   Once these decisions are made, we employ another object from the .Net Graphics Class to make use of this information.   That object is a Rectangle.

   The key concept to keep in mind here is that a Rectangle is an actual object, and not merely the shape that it represents.   It is easy to get confused with objects such as Rectangles, Points, Sizes and so on, because we often tend to see them in our mind’s eye as abstract rather than physical.   But a Rectangle object in the Graphics Class is as much a real object as any other in the Framework.

    Passing our location and size choices to the Rectangle object takes just one line of code:

Dim rect As Rectangle = New Rectangle(100, 105, 150, 150)

    In the above code, the four values represent:
100 – the X Position of the Rectangle,
i.e. the number of pixels from the left hand side of the form to the left side of the Rectangle.
105 – the Y Position of the Rectangle.
         The number of pixels from the top of the form to the top of the Rectangle.
150 – the Width of the Rectangle.
        As the Pie Chart will take up all available space inside this Rectangle, it follows that this will also be the width of the pie.
150 – the Height of the Rectangle
         Therefore also the height of the pie.

   Although it is traditional to use a circle to display a pie chart, this isn’t necessary.   You may for instance prefer to create an oval chart, in which case you need only change the Width or Height values to your preference.

 

 


 

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 03/13/2005)
Rating
The subject you have picked is one of the most interesting ones in programming. But sure enough, that may just be my opinion. When an interesting subject comes with explanations that are detailed but still very readable, then this means that the author has a great talent for writing and it shows immense responsibility and patience with beginning programmers. And that’s exactly what your article has achieved.
 
Comment #2  (Posted by an unknown user on 03/16/2005)
Rating
Very nice, Ged.

A small suggestion for part II: when would you use GDI+ vs. DirectX?
 
Comment #3  (Posted by Ged Mead on 03/22/2005)
Rating
Thanks for the comments. Part 2 is out now .... but I think it'll be a while before I get round to DirectX!
 
Comment #4  (Posted by an unknown user on 06/12/2005)
Rating
Hi Ged,
Nice and the way u explained is excellent .
The explanation is in steps and easy to understand and for me it the best methord to understand .
Good work i dont have word to appriciate u but excellent job.
From
Mohd Sufian
IT Specialist
Eastern Polymer Group
Thailand
 
Comment #5  (Posted by Dave on 10/03/2005)
Rating
Explained quite well! However I have a persistence problem that you mention in your article. I am drawing inside a picture box. My drawing code is in its own function which I call from the Paint() method. When the form first opens my drawing flashes up quite briefly and then disappears. It reappears and remains whenever I resize the window. How do I get it to come up without telling it to refresh which puts it in an endless loop?
 
Comment #6  (Posted by an unknown user on 04/05/2006)
Rating
Great explanation.I would like to know how to integrate database data with GDI.
 
Comment #7  (Posted by Ronan on 04/13/2006)
Rating
Hi i have read these tutorials and am looking your help.
I am trying to develop a pie chart interface in Visual Studio 2003 for a PDA (Smart Device Application) and tried your code. however it is not compatible as it does not recognise the drawPie and FillPie commands, etc. Do you have an example i could use to help me for this?
Thank you, Ronan
 
Comment #8  (Posted by an unknown user on 08/09/2006)
Rating
Excellent article. I have learnt a lot of GDI+ Stuff from it.
 
Comment #9  (Posted by an unknown user on 10/19/2006)
Rating
Very Useful article. Prakash Bajaj
 
Comment #10  (Posted by an unknown user on 12/22/2006)
Rating
really useful
 
Comment #11  (Posted by an unknown user on 12/29/2006)
Rating
no corners cut, all is clear
 
Comment #12  (Posted by an unknown user on 07/09/2007)
Rating
thank you
Very simple
detailed explanation
 
Comment #13  (Posted by an unknown user on 07/22/2007)
Rating
best for beginner's. language is just perfect
 
Comment #14  (Posted by neelabh on 07/22/2007)
Rating
Language is clear nd decription is best for beginner
 
Comment #15  (Posted by an unknown user on 10/01/2007)
Rating
Easy to follow tutorial. Picked up the main points straight away.
 
Comment #16  (Posted by an unknown user on 12/28/2007)
Rating
I have been searching for a good graphics article to get me started...with this one I found what I was looking for...GREAT JOB. Now I need to found out how to do vertical text.
 
Comment #17  (Posted by an unknown user on 02/05/2008)
Rating
Free, good, patient advice. A rare commodity and precious to beginners.
 
Comment #18  (Posted by an unknown user on 02/06/2008)
Rating
thanks for giving such a good article to start GDI+ from scratch.
 
Comment #19  (Posted by an unknown user on 02/20/2008)
Rating
good one
 
Sponsored Links