The following code can be called in the Page Load Event. the Code given here will be displayed from the (x,y) position (0,0). If the pie chart to be drawn from the top of the page, then the following code is OK. If it is need to be created in the center of the page or on the fly means, create a new aspx page and copy the code in the page load event and pass the aspx file as source for an image tag.
Add the following NameSpace
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
int startAngle = 0;
int sweepAngle = 45;
const int width = 500, height = 500;
Response.ContentType = "image/jpeg";
Bitmap objBitmap = new Bitmap(width, height);
Graphics objGraphics = Graphics.FromImage(objBitmap);
// Create a black background for the border
objGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
//To Draw pie chart
SolidBrush objBrush = new SolidBrush(Color.Aqua);
objGraphics.SmoothingMode = SmoothingMode.Default;
Random rand = new Random();
//Loop through 180 back around to 135 degress so it gets drawn correctly.
for(int iLoop = 0;iLoop<= 315;iLoop += 45)
{
startAngle = (iLoop + 180) % 360;
objBrush.Color = Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255));
objGraphics.FillPie(objBrush, 50, 50, 300, 300, startAngle, sweepAngle);
}
// Save the image to a file
objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
// clean up...
objGraphics.Dispose();
objBitmap.Dispose();
The Output is like this: