This article explains the creation of a custom drawing application based on the .NET
Panel is a .NET class which resides in the namespace
System.Windows.Forms which can be used to draw/render several custom shapes. It is similar to a canvas/paint area in the Microsoft Paint (MS Paint) application. This can be used to draw several custom shapes using a
In order to effectively use the .NET
Panel object, one must implement the method
protected override void OnPaint(PaintEventArgs e). This method provides a handle to the
Graphics object which in turn can be used to draw/render several shapes using methods like
Using the Quick Draw Code
In this article, there are several classes used in order to hold several shapes in memory. The following table explains this...
|Holds the shape information like shape, color and rectangle object. The rectangle object holds the co-ordinates of the rectangle like top, left, right and bottom.|
|This class holds a collection of |
|This is the core class of the application. This class inherits from |
|This class handles the form events and fits in all the various parts of this application.|
Shape Information - The ShapeInfo class
The code for the class
ShapeInfo is shown below...
public class ShapeInfo
private Rectangle _rectangle;
private Color _color;
private int _shapeType = 0;
public ShapeInfo(Rectangle rectangle, Color color, int shapeType)
this._rectangle = rectangle;
this._color = color;
this._shapeType = shapeType;
public Rectangle ShapeRectangle
this._rectangle = value;
public Color ShapeColor
this._color = value;
public int Shape
this._shapeType = value;
ShapeInfo class has three main properties:
ShapeRectangle property is used to hold the positional information of the shape. This shall be used to position a particular shape on the
ShapeColor property is used to hold the color related information of the shape. This shall be used to render a particular shape with the specified color on the
Shape property is used to hold the type of the shape. This can be either a 0, 1 or 2 which corresponds to a circle, rectangle, or a square. This shall be used to determine a particular shape.
Painting on the Custom Panel - The DrawingPanel class
This class inherits from the class
System.Windows.Forms.Panel. This is used to write custom painting code. The method
OnPaint(PaintEventArgs e) needs to be overridden to provide the custom drawing information for the
protected override void OnPaint(PaintEventArgs e)
SolidBrush solid = null;
Graphics g = e.Graphics;
ShapeInfo shapeInfo = null;
for(int count = 0; count < _shapeColl.Count; count++)
shapeInfo = (ShapeInfo) _shapeColl[count];
if (shapeInfo != null)
solid = new SolidBrush(shapeInfo.ShapeColor);
Let us walkthrough this code step-by-step...
- First override the method
- Retrieve the
Graphics object using the
- Use the
Graphics object reference to invoke methods like
This would render the desired shape on the
Canvas and a repainting would occur.
Putting all the bits together...
Once the shapes are determined, the shape information collection is passed to the
drawingPanel and the
drawingPanel is invalidated.. This shall allow the shape to be rendered on the screen.
private void mouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
if (_mouseDownFlag && !_mouseUpFlag )
_mouseUpFlag = false;
= new Rectangle(_startX, _startY, (e.X - _startX), (e.Y - _startY));
You have seen how a
Panel class can be used in order to draw/render several kinds of shapes on the
Canvas. Note that you would need to call the
Invalidate method on the
Notice how the binary serialization is done in the code in order to save the images into a file and again restore them back.
Naveen has done his Masters (M.S.) in Computer science, has started his career programming the mainframes and now has more than a decade of programming, development and design experience. Naveen has a sharp eye and keen observation skills. Naveen has worked for several companies and strived hard to build large scale business applications and bringing better solutions to the table.
Quite recently Naveen has built a fairly complex integration platform for a large bank. His hobbies include training, mentoring and research. Naveen spends his free time visiting National Parks nationwide.
Naveen has developed the BizTalk Control Center (BCC)