public void drawPic(Graphics objgraphics, string M_str_sqlstr, string M_str_table, string M_str_Num, string M_str_tbGName, string M_str_title)
{
DataSet myds = this.getds(M_str_sqlstr, M_str_table);
float M_flt_total = 0.0f, M_flt_tmp;
int M_int_iloop;
for (M_int_iloop = 0; M_int_iloop < myds.Tables[0].Rows.Count; M_int_iloop++)
{
M_flt_tmp = Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num]);
M_flt_total += M_flt_tmp;
}
Font fontlegend = new Font("verdana", 9), fonttitle = new Font("verdana", 10, FontStyle.Bold);//设置字体
int M_int_width = 275;//白色背景宽
const int Mc_int_bufferspace = 15;
int M_int_legendheight = fontlegend.Height * (myds.Tables[0].Rows.Count + 1) + Mc_int_bufferspace;
int M_int_titleheight = fonttitle.Height + Mc_int_bufferspace;
int M_int_height = M_int_width + M_int_legendheight + M_int_titleheight + Mc_int_bufferspace;//白色背景高
int M_int_pieheight = M_int_width;
Rectangle pierect = new Rectangle(0, M_int_titleheight, M_int_width, M_int_pieheight);
//加上各种随机色
Bitmap objbitmap = new Bitmap(M_int_width, M_int_height);//创建一个bitmap实例
objgraphics = Graphics.FromImage(objbitmap);
ArrayList colors = new ArrayList();
Random rnd = new Random();
for (M_int_iloop = 0; M_int_iloop < myds.Tables[0].Rows.Count; M_int_iloop++)
colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));
objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, M_int_width, M_int_height);//画一个白色背景
objgraphics.FillRectangle(new SolidBrush(Color.LightYellow), pierect);//画一个亮黄色背景
//以下为画饼图(有几行row画几个)
float M_flt_currentdegree = 0.0f;
for (M_int_iloop = 0; M_int_iloop < myds.Tables[0].Rows.Count; M_int_iloop++)
{
objgraphics.FillPie((SolidBrush)colors[M_int_iloop], pierect, M_flt_currentdegree,
Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num]) / M_flt_total * 360);
M_flt_currentdegree += Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num]) / M_flt_total * 360;
}
//以下为生成主标题
SolidBrush blackbrush = new SolidBrush(Color.Black);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
objgraphics.DrawString(M_str_title, fonttitle, blackbrush, new Rectangle(0, 0, M_int_width, M_int_titleheight), stringFormat);
objgraphics.DrawRectangle(new Pen(Color.Black, 2), 0, M_int_height - M_int_legendheight, M_int_width, M_int_legendheight);
for (M_int_iloop = 0; M_int_iloop < myds.Tables[0].Rows.Count; M_int_iloop++)
{
objgraphics.FillRectangle((SolidBrush)colors[M_int_iloop], 5, M_int_height - M_int_legendheight + fontlegend.Height * M_int_iloop + 5, 10, 10);
objgraphics.DrawString(((String)myds.Tables[0].Rows[M_int_iloop][M_str_tbGName]) + " —— "
+ Convert.ToString(Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num]) * 100 / M_flt_total) + "%", fontlegend, blackbrush,
20, M_int_height - M_int_legendheight + fontlegend.Height * M_int_iloop + 1);
}
//objgraphics.DrawString("总数是:" + Convert.ToString(M_flt_total), fontlegend, blackbrush, 5, M_int_height - fontlegend.Height);
string P_str_imagePath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,
Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
P_str_imagePath += @"\Image\image\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
objbitmap.Save(P_str_imagePath, ImageFormat.Jpeg);
objgraphics.Dispose();
objbitmap.Dispose();
}