Visual Studio .NET Developer
I finished my first pass at the Advanced ASP.NET course.
Here's a snippet of my section on graphics...
-
Add a new web form named Chart.aspx.
-
Modify Page_Load as follows:
private void Page_Load(object sender, System.EventArgs e)
{
Random r = new Random();
// A. Create a bitmap object
Bitmap bmp = new Bitmap(250,150);
// B. Create a graphics object to draw on
// based on the bitmap.
Graphics g = Graphics.FromImage(bmp);
// C. Draw on the graphics object
g.FillRectangle(new SolidBrush(Color.Beige), 0,0, bmp.Width, bmp.Height);
SolidBrush b = new SolidBrush(Color.Navy);
Font f = new Font("Arial", 14);
Font legend = new Font("Arial", 6);
for (int x = 0; x < 10; x++)
{
int h = r.Next(1,100);
// rectangles fill upside down
g.FillRectangle(b, x*20, bmp.Height - h - 10, 15, h);
g.DrawString((x+1).ToString(), legend, b, x*20, bmp.Height - 10);
}
g.DrawString("Hello World", f, new SolidBrush(Color.Red), 10, 10);
// D. Save the bitmap to the response outputstream
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
// E. Dispose of the objects
g.Dispose();
bmp.Dispose();
}
-
Press F5 and navigate to the Chart.aspx page
Now how do you include the chart in another page?
-
Open Default.aspx and drag an Image control on the bottom of the page.
-
Modify the ImageUrl property to Chart.aspx.
-
Press F5.
Pretty cool!
Anonymous comments are disabled