protected void Page_Load(object sender, EventArgs e)
{
int height = 600, width = 600;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(height, width);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.Wheat);
Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
Font font1 = new System.Drawing.Font("隶书", 20, FontStyle.Regular);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
g.DrawString("" + "统计统计统计统计统计", font1, brush1, new PointF(100, 30));
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
Pen mypen = new Pen(brush, 1);
int x = 100;
for (int i = 0; i < 11; i++)
{ g.DrawLine(mypen, x, 80, x, 340); x = x + 40; }
Pen mypen1 = new Pen(Color.Blue, 2);
g.DrawLine(mypen1, x - 480, 80, x - 480, 340);
int y = 106;
for (int i = 0; i < 9; i++)
{ g.DrawLine(mypen, 60, y, 540, y); y = y + 26; }
g.DrawLine(mypen1, 60, y, 540, y);
string[] n = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };
x = 62;
for (int i = 0; i < 12; i++)
{ g.DrawString(n[i].ToString(), font, Brushes.Red, x, 348); x = x + 40; }
string[] m = { "100%", "90%", "80%", "70%", "60%", "50%", "40%", "30%", "20%", "10%", "0%" };
y = 85;
for (int i = 0; i < 11; i++)
{ g.DrawString(m[i].ToString(), font, Brushes.Red, 25, y); y = y + 26; }
//
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.BinaryWrite(ms.ToArray());
}
本文介绍了一个使用C#绘制简单统计图表的方法。通过创建Bitmap对象并利用Graphics类进行绘图,包括填充背景、绘制文本、线条及矩形等元素,实现了一个基本的条形统计图表。





