private void CreateImage()
{
//把连接字串指定为一个常量
SqlConnection Con = new SqlConnection("Server=(Local);
Database=committeeTraining;Uid=sa;Pwd=**");
Con.Open();
string cmdtxt = selectString; // "select * from ##Count"; //
//SqlCommand Com = new SqlCommand(cmdtxt, Con);
DataSet ds = new DataSet();
SqlDataAdapter Da = new SqlDataAdapter(cmdtxt, Con);
Da.Fill(ds);
Con.Close();
float Total =
Total = Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]);
//设置字体,fonttitle为主标题的字体
Font fontlegend = new Font("verdana", 9);
Font fonttitle = new Font("verdana", 10, FontStyle.Bold);
int width = 350;
int bufferspace = 15;
int legendheight = fontlegend.Height * 10 + bufferspace; //高度
int titleheight = fonttitle.Height + bufferspace;
int height = width + legendheight + titleheight + bufferspace;//白色背景高
int pieheight = width;
Rectangle pierect = new Rectangle(0, titleheight, width, pieheight);
ArrayList colors = new ArrayList();
Random rnd = new Random();
for (int i = 0; i < 2; i++)
colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));
Bitmap objbitmap = new Bitmap(width, height);
Graphics objgraphics = Graphics.FromImage(objbitmap);
objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect);
float currentdegree =
objgraphics.FillPie((SolidBrush)colors[1], pierect, currentdegree,
Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360);
currentdegree += Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360;
objgraphics.FillPie((SolidBrush)colors[0], pierect, currentdegree,
((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]))-(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360);
currentdegree += ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) -
(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360;
//以下为生成主标题
SolidBrush blackbrush = new SolidBrush(Color.Black);
SolidBrush bluebrush = new SolidBrush(Color.Blue);
string title = " 机关单位成绩统计饼状图: "
+ "\n \n\n";
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
new Rectangle(0, 0, width, titleheight), stringFormat);
objgraphics.DrawRectangle(new Pen(Color.Red, 2), 0, height + 10 - legendheight, width, legendheight + 50);
fontlegend, bluebrush, 20, height - legendheight + fontlegend.Height * 1 + 1);
objgraphics.DrawString("统计单位: " + this.ddlTaget.SelectedItem.Text,
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 3 + 1);
objgraphics.DrawString("统计年份: " + this.ddlYear.SelectedItem.Text,
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 4 + 1);
objgraphics.DrawString("统计期数: " + this.ddlSpan.SelectedItem.Text,
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 5 + 1);
objgraphics.DrawString("报名总人数: " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])),
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 7 + 1);
objgraphics.FillRectangle((SolidBrush)colors[0], 5, height - legendheight + fontlegend.Height * 9 + 1, 10, 10);
objgraphics.DrawString("通过总人数: " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]])),
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 8 + 1);
objgraphics.DrawString("未通过人数: " + ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) -
(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))), fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 9 + 1);
Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) * 100)+ " %", fontlegend,
blackbrush, 20, height - legendheight + fontlegend.Height * 10 + 1);
objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
objgraphics.Dispose();
objbitmap.Dispose();