Monday, 25 July 2011

Drawing in ASP.NET 2.0 and C#


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;

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

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);

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

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);

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

End drawing

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

Looking for the VB.NET 2005 Version? Click Here!


Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment