GDI+ - Next Generation Graphics Device Interface
All graphical user interface (GUI) applications interact with hardware devices (monitors, printers, scanners), which can be represented as readable data. However, the application does not communicate directly with the device, otherwise different user interface code must be written for each device. To avoid this duplication of work, we can use a third component between the application and the device. This component will convert and transfer the data sent to the device, and the data sent by the device to the program. And this component is GDI+. GDI+ is the entrance to interact with graphics devices in the .NET Framework. GDI+ is a set of C++ classes located in a class library called Gdiplus.dll. Gdiplus.dll is a built-in component in Windows XP and Windows Server 2003 operating systems. Okay, I won’t waste much time talking about academic stuff here. Let’s use GDI+ to draw a picture in Web Form.
You must have seen pictures like this. Of course, this picture is not standard. The code is posted here. If you are interested, you can give it a try!
namespace Sky_MsdnDataGrid
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description of AspxChart.
/// </summary>
public class AspxChart : System.Web.UI.Page
{
private Bitmap bitmap;
private graphics graphics;
private int[] arrValues;
private string[] arrValueNames;
private void Page_Load(object sender, System.EventArgs e)
{
arrValues = new int[6];
arrValueNames = new string[6];
arrValues[0] = 100;
arrValues[1] = 135;
arrValues[2] = 115;
arrValues[3] = 125;
arrValues[4] = 75;
arrValues[5] = 120;
arrValueNames[0] = "January";
arrValueNames[1] = "February";
arrValueNames[2] = "March";
arrValueNames[3] = "April";
arrValueNames[4] = "May";
arrValueNames[5] = "June";
this.Init_Bitmap();
this.Draw_Rectangle();
this.Draw_Pie();
//Save the drawn image in Gif format to the output stream of the current page response
bitmap.Save(this.Response.OutputStream,ImageFormat.Gif);
}
/// <summary>
/// Perform initial actions on the bitmap to be drawn (can be imagined as a canvas)
/// </summary>
private void Init_Bitmap()
{
bitmap = new Bitmap(400,200);
graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.DrawString("Sales of Company
}
#region Code generated by Web Form design tools
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This is a call required by the ASP.NET Web Form design tool.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// This method is required for design tool support - do not use a code editor to modify it
/// The content of this method.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/// <summary>
/// Draw a rectangle on the canvas
/// </summary>
private void Draw_Rectangle()
{
int i;
PointF symbolLeg = new PointF(335,20);
PointF descLeg = new PointF(360,16);
for (i = 0; i < arrValueNames.Length; i++)
{
graphics.FillRectangle(new SolidBrush(GetColor(i)),symbolLeg.X,symbolLeg.Y,20,10);
graphics.DrawRectangle(Pens.Black,symbolLeg.X,symbolLeg.Y,20,10);
graphics.DrawString(arrValueNames[i].ToString(),new Font("New Detailed Body",8),Brushes.Black,descLeg);
symbolLeg.Y += 15;
descLeg.Y += 16;
}
for (i = 0; i < arrValues.Length; i++)
{
graphics.FillRectangle(new SolidBrush(GetColor(i)),(i*35) + 15,200 - arrValues[i],20,arrValues[i]);
graphics.DrawRectangle(Pens.Black,(i*35) + 15,200 - arrValues[i],20,arrValues[i]);
}
}
/// <summary>
/// Draw a pie shape on the canvas
/// </summary>
private void Draw_Pie()
{
int i;
// currentangle represents the current angle totalangle represents the maximum angle totalvalues represents the maximum sales
float sglCurrentAngle = 0, sglTotalAngle = 0, sglTotalValues = 0;
// Calculate maximum sales
for (i = 0; i < arrValues.Length; i++)
sglTotalValues += arrValues[i];
for (i = 0; i < arrValues.Length; i++)
{
// Angle value of the current month: sales of the current month / maximum sales * 360
sglCurrentAngle = arrValues[i] / sglTotalValues * 360;
graphics.FillPie(new SolidBrush(GetColor(i)),240,95,100,100,sglTotalAngle,sglCurrentAngle);
graphics.DrawPie(Pens.Black,240,95,100,100,sglTotalAngle,sglCurrentAngle);
sglTotalAngle += sglCurrentAngle;
}
}
#region Helper Function
private Color GetColor(int itemIndex)
{
Color color;
switch(itemIndex)
{
case 0:
color = Color.Blue;
break;
case 1:
color = Color.Red;
break;
case 2:
color = Color.Yellow;
break;
case 3:
color = Color.Purple;
break;
case 4:
color = Color.Orange;
break;
case 5:
color = Color.Brown;
break;
default:
color = Color.Blue;
break;
}
return color;
}
#endregion
}
}