SIGN UP MEMBER LOGIN:    
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
share 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
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor