public static MemoryStream CreateImage(int h, int w, int avg_degree, int max_degree, int good_num, int better_num, int h_num, int bed_num, int stunum)
{
double d = 50.0 / stunum;
int height = h; int width = w;
Bitmap p_w_picpath = new Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(p_w_picpath);
try
{
{
double d = 50.0 / stunum;
int height = h; int width = w;
Bitmap p_w_picpath = new Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(p_w_picpath);
try
{
//清空图片背景色
g.Clear(Color.Black);
Font font = new Font("Arial", 10, FontStyle.Regular);
Font font1 = new Font("宋体", 20, FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, p_w_picpath.Width, p_w_picpath.Height),
Color.Blue, Color.BlueViolet, 1.2f, true);
g.FillRectangle(Brushes.WhiteSmoke , 0, 0, width, height);
g.DrawString(" 成绩统计柱状图", font1, brush, new PointF(70, 30));
g.Clear(Color.Black);
Font font = new Font("Arial", 10, FontStyle.Regular);
Font font1 = new Font("宋体", 20, FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, p_w_picpath.Width, p_w_picpath.Height),
Color.Blue, Color.BlueViolet, 1.2f, true);
g.FillRectangle(Brushes.WhiteSmoke , 0, 0, width, height);
g.DrawString(" 成绩统计柱状图", font1, brush, new PointF(70, 30));
//画图片的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 0, p_w_picpath.Width - 10, p_w_picpath.Height - 10);
Pen mypen = new Pen(brush, 1);
g.DrawRectangle(new Pen(Color.Blue), 0, 0, p_w_picpath.Width - 10, p_w_picpath.Height - 10);
Pen mypen = new Pen(brush, 1);
//绘制线条
Pen mypen1 = new Pen(Color.Blue, 2);
//绘制横向线条
int x = 60;
g.DrawLine(mypen1, x, 80, x, 340);
Pen mypen1 = new Pen(Color.Blue, 2);
//绘制横向线条
int x = 60;
g.DrawLine(mypen1, x, 80, x, 340);
for (int i = 0; i < 14; i++)
{
x = x + 40;
g.DrawLine(mypen, x, 80, x, 340);
}
{
x = x + 40;
g.DrawLine(mypen, x, 80, x, 340);
}
//绘制纵向线条
int y = 340;
g.DrawLine(mypen1, 60, y, 620, y);
for (int i = 0; i <= (int)(7/d); i++)
{
y = y - (int)(26 * d);
g.DrawLine(mypen, 60, y, 620, y);
}
int y = 340;
g.DrawLine(mypen1, 60, y, 620, y);
for (int i = 0; i <= (int)(7/d); i++)
{
y = y - (int)(26 * d);
g.DrawLine(mypen, 60, y, 620, y);
}
//x轴
String[] n = { "总人数", "90—100", "89—80", "79—60", "不及格", " 及格" };
x = 78;
for (int i = 0; i < 6; i++)
{
g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 348); //设置文字内容及输出位置
x = x + 78;
}
String[] n = { "总人数", "90—100", "89—80", "79—60", "不及格", " 及格" };
x = 78;
for (int i = 0; i < 6; i++)
{
g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 348); //设置文字内容及输出位置
x = x + 78;
}
//y轴
String[] m = { " 0", " 10", " 20", " 30", " 40", " 50", " 60", " 70", " 80", " 90", "100" };
y = 332;
for (int i = 0; i < 11; i++)
{
if (i > (int)(11 / d))
break;
g.DrawString(m[i].ToString(), font, Brushes.Blue, 20, y); //设置文字内容及输出位置
y = y - (int)(26 * d);
}
String[] m = { " 0", " 10", " 20", " 30", " 40", " 50", " 60", " 70", " 80", " 90", "100" };
y = 332;
for (int i = 0; i < 11; i++)
{
if (i > (int)(11 / d))
break;
g.DrawString(m[i].ToString(), font, Brushes.Blue, 20, y); //设置文字内容及输出位置
y = y - (int)(26 * d);
}
int[] Count1 = new int[6];
int[] Count2 = new int[6];
int[] Count2 = new int[6];
Count1[0] = stunum;
Count1[1] = good_num;
Count1[2] = better_num;
Count1[3] = h_num;
Count1[4] = bed_num;
Count1[5] = stunum - bed_num;
Count1[1] = good_num;
Count1[2] = better_num;
Count1[3] = h_num;
Count1[4] = bed_num;
Count1[5] = stunum - bed_num;
Count2[0] = stunum;
Count2[1] = good_num;
Count2[2] = better_num;
Count2[3] = h_num;
Count2[4] = bed_num;
Count2[5] = stunum - bed_num;
Count2[1] = good_num;
Count2[2] = better_num;
Count2[3] = h_num;
Count2[4] = bed_num;
Count2[5] = stunum - bed_num;
//绘制柱状图.
x = 80;
Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
SolidBrush mybrush = new SolidBrush(Color.Red);
SolidBrush mybrush2 = new SolidBrush(Color.Green);
//总人数
g.FillRectangle(mybrush, x, (int)(340 - Count1[0] * 2.6 * d), 20, (int)((Count1[0] * 2.6) * d));
g.DrawString(Count1[0].ToString(), font2, Brushes.Red, x, (int)(340 - Count1[0] * 2.6 * d - 15));
x = x + 20;
g.FillRectangle(mybrush2, x, (int)(340 - Count2[0] * 2.6 * d), 20, (int)((Count2[0] * 2.6) * d));
g.DrawString(Count2[0].ToString(), font2, Brushes.Green, x, (int)(340 - Count2[0] * 2.6 * d - 15));
x = 80;
Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
SolidBrush mybrush = new SolidBrush(Color.Red);
SolidBrush mybrush2 = new SolidBrush(Color.Green);
//总人数
g.FillRectangle(mybrush, x, (int)(340 - Count1[0] * 2.6 * d), 20, (int)((Count1[0] * 2.6) * d));
g.DrawString(Count1[0].ToString(), font2, Brushes.Red, x, (int)(340 - Count1[0] * 2.6 * d - 15));
x = x + 20;
g.FillRectangle(mybrush2, x, (int)(340 - Count2[0] * 2.6 * d), 20, (int)((Count2[0] * 2.6) * d));
g.DrawString(Count2[0].ToString(), font2, Brushes.Green, x, (int)(340 - Count2[0] * 2.6 * d - 15));
//优秀
x = x + 60;
g.FillRectangle(mybrush, x, (int)(340 - Count1[1] * 2.6 * d), 20, (int)((Count1[1] * 2.6) * d));
g.DrawString(Count1[1].ToString(), font2, Brushes.Red, x, (int)(340 - Count1[1] * 2.6 * d - 15));
x = x + 60;
g.FillRectangle(mybrush, x, (int)(340 - Count1[1] * 2.6 * d), 20, (int)((Count1[1] * 2.6) * d));
g.DrawString(Count1[1].ToString(), font2, Brushes.Red, x, (int)(340 - Count1[1] * 2.6 * d - 15));
x = x + 20;
g.FillRectangle(mybrush2, x, (int)(340 - Count2[1] * 2.6 * d), 20, (int)((Count2[1] * 2.6) * d));
g.DrawString(Count2[1].ToString(), font2, Brushes.Green, x, (int)(340 - Count2[1] * 2.6 * d - 15));
g.FillRectangle(mybrush2, x, (int)(340 - Count2[1] * 2.6 * d), 20, (int)((Count2[1] * 2.6) * d));
g.DrawString(Count2[1].ToString(), font2, Brushes.Green, x, (int)(340 - Count2[1] * 2.6 * d - 15));
//良好
x = x + 60;
g.FillRectangle(mybrush, x, (int)(340 - Count1[2] * 2.6 * d), 20, (int)((Count1[2] * 2.6) * d));
g.DrawString(Count1[2].ToString(), font2, Brushes.Red, x, (int)(340 - Count1[2] * 2.6 * d - 15));
x = x + 20;
g.FillRectangle(mybrush2, x, (int)(340 - Count2[2] * 2.6 * d), 20, (int)((Count2[2] * 2.6) * d));
g.DrawString(Count2[2].ToString(), font2, Brushes.Green, x, (int)(340 - Count2[2] * 2.6 * d - 15));
//及格
x = x + 60;
g.FillRectangle(mybrush, x, (int)(340 - Count1[3] * 2.6 * d), 20, (int)((Count1[3] * 2.6) * d));
g.DrawString(Count1[3].ToString(), font2, Brushes.Red, x, (int)(340 - Count1[3] * 2.6 * d - 15));
x = x + 20;
g.FillRectangle(mybrush2, x, (int)(340 - Count2[3] * 2.6 * d), 20, (int)((Count2[3] * 2.6) * d));
g.DrawString(Count2[3].ToString(), font2, Brushes.Green, x, (int)(340 - Count2[3] * 2.6 * d - 15));
//不及格
x = x + 60;
g.FillRectangle(mybrush, x, (int)(340 - Count1[4] * 2.6 * d), 20, (int)((Count1[4] * 2.6) * d));
g.DrawString(Count1[4].ToString(), font2, Brushes.Red, x, (int)(340 - Count1[4] * 2.6 * d - 15));
x = x + 20;
g.FillRectangle(mybrush2, x, (int)(340 - Count2[4] * 2.6 * d), 20, (int)((Count2[4] * 2.6) * d));
g.DrawString(Count2[4].ToString(), font2, Brushes.Green, x, (int)(340 - Count2[4] * 2.6 * d - 15));
//总及格
x = x + 60;
g.FillRectangle(mybrush, x, (int)(340 - Count1[5] * 2.6 * d), 20, (int)((Count1[5] * 2.6) * d));
g.DrawString(Count1[5].ToString(), font2, Brushes.Red, x, (int)(340 - Count1[5] * 2.6 * d - 15));
x = x + 20;
g.FillRectangle(mybrush2, x, (int)(340 - Count2[5] * 2.6 * d), 20, (int)((Count2[5] * 2.6) * d));
g.DrawString(Count2[5].ToString(), font2, Brushes.Green, x, (int)(340 - Count2[5] * 2.6 * d - 15));
x = x + 60;
g.FillRectangle(mybrush, x, (int)(340 - Count1[2] * 2.6 * d), 20, (int)((Count1[2] * 2.6) * d));
g.DrawString(Count1[2].ToString(), font2, Brushes.Red, x, (int)(340 - Count1[2] * 2.6 * d - 15));
x = x + 20;
g.FillRectangle(mybrush2, x, (int)(340 - Count2[2] * 2.6 * d), 20, (int)((Count2[2] * 2.6) * d));
g.DrawString(Count2[2].ToString(), font2, Brushes.Green, x, (int)(340 - Count2[2] * 2.6 * d - 15));
//及格
x = x + 60;
g.FillRectangle(mybrush, x, (int)(340 - Count1[3] * 2.6 * d), 20, (int)((Count1[3] * 2.6) * d));
g.DrawString(Count1[3].ToString(), font2, Brushes.Red, x, (int)(340 - Count1[3] * 2.6 * d - 15));
x = x + 20;
g.FillRectangle(mybrush2, x, (int)(340 - Count2[3] * 2.6 * d), 20, (int)((Count2[3] * 2.6) * d));
g.DrawString(Count2[3].ToString(), font2, Brushes.Green, x, (int)(340 - Count2[3] * 2.6 * d - 15));
//不及格
x = x + 60;
g.FillRectangle(mybrush, x, (int)(340 - Count1[4] * 2.6 * d), 20, (int)((Count1[4] * 2.6) * d));
g.DrawString(Count1[4].ToString(), font2, Brushes.Red, x, (int)(340 - Count1[4] * 2.6 * d - 15));
x = x + 20;
g.FillRectangle(mybrush2, x, (int)(340 - Count2[4] * 2.6 * d), 20, (int)((Count2[4] * 2.6) * d));
g.DrawString(Count2[4].ToString(), font2, Brushes.Green, x, (int)(340 - Count2[4] * 2.6 * d - 15));
//总及格
x = x + 60;
g.FillRectangle(mybrush, x, (int)(340 - Count1[5] * 2.6 * d), 20, (int)((Count1[5] * 2.6) * d));
g.DrawString(Count1[5].ToString(), font2, Brushes.Red, x, (int)(340 - Count1[5] * 2.6 * d - 15));
x = x + 20;
g.FillRectangle(mybrush2, x, (int)(340 - Count2[5] * 2.6 * d), 20, (int)((Count2[5] * 2.6) * d));
g.DrawString(Count2[5].ToString(), font2, Brushes.Green, x, (int)(340 - Count2[5] * 2.6 * d - 15));
//绘制标识
Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Regular);
g.DrawRectangle(new Pen(Brushes.Blue), 170, 400, 250, 50); //绘制范围框
g.FillRectangle(Brushes.Red, 270, 410, 20, 10); //绘制小矩形
g.DrawString("报名人数", font3, Brushes.Red, 292, 408);
g.FillRectangle(Brushes.Green, 270, 430, 20, 10);
g.DrawString("通过人数", font3, Brushes.Green, 292, 428);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
p_w_picpath.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms;
}
finally
{
g.Dispose();
p_w_picpath.Dispose();
}
}
finally
{
g.Dispose();
p_w_picpath.Dispose();
}
}
转载于:https://blog.51cto.com/qianshao/202138