ARTICLE

Graphics using GDI+

Posted by Mike Gold Articles | GDI+ & Graphics February 05, 2002
This sample project shows how to draw various graphics objects on a Form using GDI+ objects.
Reader Level:
Download Files:
 

In .NET, GDI+ functionality resides in the System.Drawing.dll. Before you start using GDI+ classes, you must add reference to the System.Drawing.dll and import System.Drawing namespace. In this sample, I use class GraphicsPath, which is defined in the System.Drawing.Drawing2D namespace.

Creating this project is simple. Create a Windows application and add reference to the System.Drawing.dll using Project->Add Reference menu item. See Figure 1.



Figure 1. Adding reference to System.Drawing.dll

After that add these two lines.

using System.Drawing;
using System.Drawing.Drawing2D;

Note: If you create a Windows application using VS.NET, you only need write only one line.

using System.Drawing.Drawing2D;

After that add a Form_Paint event handler using the Properties Window. See Figure 2.

Figure 2. Adding Form_Paint event handler.

And write the following code on the Form_Paint event handler.

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics ;
Rectangle rect =
this.ClientRectangle;
// Rectangle rect = new Rectangle(50, 30, 100, 100);
// paints a gradiant background to the whole window
LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Red, Color.Yellow,
LinearGradientMode.BackwardDiagonal);
g.FillRectangle(lBrush, rect);
// draws an arc
Pen pn = new Pen( Color.Blue );
rect =
new Rectangle(50, 50, 200, 100);
g.DrawArc( pn, rect, 12, 84 );
// Rectangle rect = new Rectangle(50, 50, 200, 100);
// draws a line with a blue violet pen
Pen pn2 = new Pen( Color.BlueViolet );
Point pt1 =
new Point( 30, 30);
Point pt2 =
new Point( 110, 100);
g.DrawLine( pn2, pt1, pt2 );
// illustrates how to draw graphic test
g.DrawString("Welcome to the Graphics World", this.Font, new SolidBrush(Color.Azure ), 10,10);
// draws an ellipse in the bottom corner of the screen
Pen pn3 = new Pen( Color.Aqua , 5 );
rect =
new Rectangle(ClientRectangle.Right - 60, ClientRectangle.Bottom - 60, 50, 50);
g.DrawEllipse( pn3, rect );
// draw a graphic path with bezier curves
GraphicsPath path = new GraphicsPath(new Point[] {
new Point(40, 140), new Point(275, 200),
new Point(105, 225), new Point(190, ClientRectangle.Bottom),
new Point(50, ClientRectangle.Bottom), new Point(20, 180), },
new byte[] {
(
byte)PathPointType.Start,
(
byte)PathPointType.Bezier,
(
byte)PathPointType.Bezier,
(
byte)PathPointType.Bezier,
(
byte)PathPointType.Line,
(
byte)PathPointType.Line,
});
PathGradientBrush pgb =
new PathGradientBrush(path);
pgb.SurroundColors =
new Color[] { Color.Green,Color.Yellow,Color.Red, Color.Blue,
Color.Orange, Color.White, };
g.FillPath(pgb, path);
}

Compile and run the project. The output looks like Figure 3.

Figure 3.

Login to add your contents and source code to this article
post comment
     

i want to drow shapes with mouse in my application

Posted by fazal rehman Oct 13, 2007

Hi, I have developed an GDI interface to show lines and polygons. I want to show screentips for lines/polygons when user hovers mouse on the line/polygon. Can you give me an idea how to implement this functionality with .net graphics. Thanks, Kanth.

Posted by Lakshmi Kanth B Sep 12, 2007

Hello, I an fairly new to C#, and I have a question. User can input the dimensions of rectangle, and rectangle is drawn on tab page, but if input is too big, it goes off tab page. How can I resize? Thank you, Goran

Posted by Goran Mitic Jul 08, 2007

Alternately, you could generate the vectors for the hexagon as follows

new Point(50, 50),
new Point(50+side, 50),
new Point(50+side + (int)(side * Math.Cos(Math.PI/3)), 50 + (int)(side * Math.Sin(Math.PI/3))),
new Point(50+side, 50 + (int)(side * 2 * Math.Sin(Math.PI/3))),
new Point(50, 50 + (int)(side * 2 * Math.Sin(Math.PI/3))),
new Point(50 - side/2, 50 + (int)(side * Math.Sin(Math.PI/3))), new Point(50, 50)

Posted by Mike Gold May 09, 2007

Here you go:

 

private GraphicsPath gp = null;

public Form1()

{

InitializeComponent();

int side = 20;

gp = new GraphicsPath(new PointF[7]

{

new Point(50, 50),

new Point(50+side, 50),

new Point(50+side + side/2, 50 + (int)(side * Math.Sqrt(3)/2)),

new Point(50+side, 50 + (int)(side * Math.Sqrt(3))),

new Point(50, 50 + (int)(side * Math.Sqrt(3))),

new Point(50 - side/2, 50 + (int)(side * Math.Sqrt(3)/2)),

new Point(50, 50)

},

new byte[7]

{

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

}

);

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

Graphics g = e.Graphics;

g.DrawPath(Pens.Black, gp);

}

Posted by Mike Gold May 09, 2007
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts