I finished my first pass at the Advanced ASP.NET course.

Here's a snippet of my section on graphics...

  1. Add a new web form named Chart.aspx.

  2. 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();
    }
  3. Press F5 and navigate to the Chart.aspx page

Now how do you include the chart in another page?

  1. Open Default.aspx and drag an Image control on the bottom of the page.

  2. Modify the ImageUrl property to Chart.aspx.

  3. Press F5.

Pretty cool!