Powered by Blogger.

Monday, July 26, 2010

ASP.NET Graphics Tutorial

This tutorial will show you how to draw pie and font in ASP.NET 2.0 and C# by using the classes of Graphics.

The class of Graphics provide the method of drawing to display device. For instance, the Rectangle, point  and others GDI+ basic components that can be assembled.  The class of Pen is used for drawing line and curve, while the classes derived from the abstract class Brush can be used for fill the shape. The class of FontFamily have the similar design, but have a little bit of differences on modality.
First, you will need to import the System.Drawing, System.Drawing.Imaging, System.Drawing.Text namespace.

The namespace of system.Drawing provides the access to basic GDI+ graphic function. In the namespace of System.Drawing.Drawing2D, System.Drawing.Imaging and System.Drawing.Text , .Net provides more advanced functionalities.


using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text; 
Create canvas and fill the background

Bitmap  objBitmap;  
Graphics  objGraphics; 
objBitmap  =  new  Bitmap(400,  440);  
objGraphics  =  Graphics.FromImage(objBitmap);   
objGraphics.Clear(Color.White);  
Draw the pie and fill

Pen p=new Pen(Color.Yellow,0);
Rectangle rect=new Rectangle(10,10,280,280);
objGraphics.DrawEllipse(p,rect);

Brush b1=new SolidBrush(Color.Red);
Brush b2=new SolidBrush(Color.Green);
Brush b3=new SolidBrush(Color.Blue);
objGraphics.FillPie(b1, rect, 0f, 60f);
objGraphics.FillPie(b2, rect, 60f, 150f);
objGraphics.FillPie(b3, rect, 210f, 150f); 
Draw font

FontFamily fontfml = new FontFamily(GenericFontFamilies.Serif);
Font font = new Font(fontfml, 16);
SolidBrush brush = new SolidBrush(Color.Blue);
objGraphics.DrawString("Drawing Graphics", font, brush, 70, 300); 
Export and save to picture

objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
objBitmap.Save(Server.MapPath("x.jpg"), ImageFormat.Jpeg); 
End drawing

objBitmap.Dispose();
objGraphics.Dispose();  





No comments:

Post a Comment

  ©Template by Dicas Blogger.

TOPO