前段时间项目需要,写了一个简单的图标统计类.决定以后写点东西到Blog来.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;



namespace CrystalReportsDemo

...{

/**//// <summary>
/// createReport绘制图形报表-柱图。
/// </summary>
public class createReport

...{

public createReport()

...{
//
// TODO: 在此处添加构造函数逻辑
//
}

public Bitmap CreatePictureReport(string[] x,int[] y,string ReportName)

...{
if(x.Length != y.Length)

...{
throw new Exception("X和Y数目不一致");
}
int colNum=y.Length;
int colScale=880/(colNum+1);


Bitmap bmp = new Bitmap(900,700,PixelFormat.Format24bppRgb);
Graphics dc = Graphics.FromImage(bmp);
dc.FillRectangle(new SolidBrush(Color.White),0,0,900,700);

Pen scalePen = new Pen(Color.Black,2);
Pen gridPen = new Pen(Color.Black,1);

dc.DrawLine(scalePen,20,680,880,680);
dc.DrawLine(scalePen,20,680,20,20);

dc.DrawLine(gridPen,20,20,880,20);
dc.DrawLine(gridPen,880,20,880,680);

Point drawPoint;
int XPoint;
int YHeight;
if(colNum==0)

...{
throw new Exception("无数据值,colNum值为0作为除数,抛出异常");
}
int drawWidth=880/(4*colNum);
//int drawWidth=580/(4*colNum+1);//4*colNum+1 确保colNum不为0.否则580/(4*0);的话会异常.0不能作为除数
int maxNum=getMaxNum(y);
Rectangle rct;
Random rnd = new Random();
int red,green,blue;

for(int i=20;i<680;i=i+96)

...{
dc.DrawLine(gridPen,20,i,880,i);
}

for(int i=0;i<colNum;i++)

...{
red=rnd.Next(0,255);
green=rnd.Next(0,255);
blue=rnd.Next(0,255);
SolidBrush brush=new SolidBrush(Color.FromArgb(red,green,blue));
XPoint=20+((i+1)*colScale)-drawWidth;
YHeight=y[i]*650/maxNum;
drawPoint=new Point(XPoint,680-YHeight);
rct=new Rectangle(drawPoint,new Size(drawWidth*2,YHeight));
dc.FillRectangle(brush,rct);
dc.DrawRectangle(gridPen,rct);
//绘制X数值
dc.DrawString(x[i],new Font("宋体",9),new SolidBrush(Color.OrangeRed),new PointF((float)(XPoint+drawWidth/2),680-YHeight/2));
//绘制Y数值
dc.DrawString(y[i].ToString(), new Font("宋体", 9), new SolidBrush(Color.Black), new PointF((float)(XPoint + drawWidth / 2), 680 - YHeight-15));
}
dc.DrawString(ReportName,new Font("宋体",10),Brushes.Black,3,3);
return bmp;

}
private int getMaxNum(int[] arr)

...{
int max=arr[0];
for(int i=1;i<arr.Length;i++)

...{
if(arr[i]>max)

...{
max=arr[i];
}

}
return max;
}
}
}

























































































































调用这个类的方法.贴一下
createReport crpt= new createReport();
string[] x ={"1月","2月","3月"};
int[] y = {25,17,29};
Bitmap bmp = crpt.CreatePictureReport(x,y,"ReportTitle");
String fname="Bar.png";
bmp.Save(Server.MapPath(fname),ImageFormat.Png);
this.Image1.ImageUrl=fname;