winform中统计图相关

lucky566 2015-05-06 06:02:12
写了个方法 关于条形统计图的,程序运行到最后几步时出错,如下图:

完整代码如下:
private void CreateImage()
{
int height = 500, width = 700;
Bitmap image = new Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);

try
{
//清空图片背景色
g.Clear(Color.White);

Font font = new Font("Arial", 10, FontStyle.Regular);
Font font1 = new Font("宋体", 20, FontStyle.Bold);

LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
Color.Blue, Color.BlueViolet, 1.2f, true);
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
// Brush brush1 = new SolidBrush(Color.Blue);

g.DrawString(this.dateTimePicker1.Text +
" 家庭收支情况图", font1, brush, new PointF(70, 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 < 27; i++)
{
g.DrawLine(mypen, x, 80, x, 600);
x = x + 40;
}
Pen mypen1 = new Pen(Color.Blue, 2);
x = 60;
g.DrawLine(mypen1, x, 80, x, 600);

//绘制纵向线条
int y = 106;
for (int i = 0; i < 10; i++)
{
g.DrawLine(mypen, 60, y, 620, y);
y = y + 26;
}
g.DrawLine(mypen1, 60, y, 620, y);

//x轴
String[] n = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月", "全年统计" };
x = 78;
for (int i = 0; i < 13; i++)
{
g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 610); //设置文字内容及输出位置
x = x + 78;
}

//y轴
String[] m = {"10000","9000", "8000", "7000", "6000", "5000", "4000", " 3000",
" 2000", " 1000", " 0"};
y = 72;
for (int i = 0; i < 11; i++)
{
g.DrawString(m[i].ToString(), font, Brushes.Blue, y, 1000); //设置文字内容及输出位置
y = y + 1000;
}

int[] Count1 = new int[24];
int[] Count2 = new int[24];
//
//连接数据库并返回符合查询条件的值
//
string connectString = "Data Source=.;Initial Catalog=FinSystem;User ID=sa;pwd=1992106713";
SqlConnection sqlCnt = new SqlConnection(connectString);
sqlCnt.Open();
//下列的语句实现的功能是将预算表中的“预算金额”列和“完成金额”列分别按每个月求和(利用SQL Server的子查询及聚合函数)
string cmdtxt = "select * from Table_Budget info where 预算日期 Between '" + dateTimePicker1.Value + "' AND '" + dateTimePicker2.Value + "'";
SqlDataAdapter da = new SqlDataAdapter(cmdtxt, sqlCnt);
DataSet ds = new DataSet();
da.Fill(ds);//将返回的数据填充到数据集ds中
//将数据集ds的表中数据值转换并填充到对应数组中
......
//绘制柱状图.
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, 600 - Count1[0], 20, Count1[0]);
g.DrawString(Count1[0].ToString(), font2, Brushes.Red, x, 600 - Count1[0] - 15);

x = x + 20;
g.FillRectangle(mybrush2, x, 600 - Count2[0], 20, Count2[0]);
g.DrawString(Count2[0].ToString(), font2, Brushes.Green, x, 600 - Count2[0] - 15);


//二月
x = x + 60;
g.FillRectangle(mybrush, x, 600 - Count1[1], 20, Count1[1]);
g.DrawString(Count1[1].ToString(), font2, Brushes.Red, x, 600 - Count1[1] - 15);


x = x + 20;
g.FillRectangle(mybrush2, x, 600 - Count2[1], 20, Count2[1]);
g.DrawString(Count2[1].ToString(), font2, Brushes.Green, x, 600 - Count2[1] - 15);


//三月
x = x + 60;
g.FillRectangle(mybrush, x, 600 - Count1[2], 20, Count1[2]);
g.DrawString(Count1[2].ToString(), font2, Brushes.Red, x, 600- Count1[2] - 15);

x = x + 20;
g.FillRectangle(mybrush2, x, 600 - Count2[2], 20, Count2[2]);
g.DrawString(Count2[2].ToString(), font2, Brushes.Green, x, 600 - Count2[2] - 15);

//四月
x = x + 60;
g.FillRectangle(mybrush, x, 600 - Count1[3], 20, Count1[3]);
g.DrawString(Count1[3].ToString(), font2, Brushes.Red, x, 600 - Count1[3] - 15);

x = x + 20;
g.FillRectangle(mybrush2, x, 600 - Count2[3], 20, Count2[3]);
g.DrawString(Count2[3].ToString(), font2, Brushes.Green, x, 600 - Count2[3] - 15);

//五月
x = x + 60;
g.FillRectangle(mybrush, x, 600 - Count1[4], 20, Count1[4]);
g.DrawString(Count1[4].ToString(), font2, Brushes.Red, x, 600 - Count1[4] - 15);

x = x + 20;
g.FillRectangle(mybrush2, x, 600 - Count2[4], 20, Count2[4]);
g.DrawString(Count2[4].ToString(), font2, Brushes.Green, x, 600 - Count2[4] - 15);

//六月
x = x + 60;
g.FillRectangle(mybrush, x, 600 - Count1[5], 20, Count1[5]);
g.DrawString(Count1[5].ToString(), font2, Brushes.Red, x, 600 - Count1[5] - 15);

x = x + 20;
g.FillRectangle(mybrush2, x, 600 - Count2[5], 20, Count2[5]);
g.DrawString(Count2[5].ToString(), font2, Brushes.Green, x, 600 - Count2[5] - 15);
......................

//全年
x = x + 60;
g.FillRectangle(mybrush, x, 600 - Count1[12], 20, Count1[12]);
g.DrawString(Count1[12].ToString(), font2, Brushes.Red, x, 600 - Count1[12] - 15);


x = x + 20;
g.FillRectangle(mybrush2, x, 600 - Count2[12], 20, Count2[12]);
g.DrawString(Count2[12].ToString(), font2, Brushes.Green, x, 600 - Count2[12] - 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();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
System.Web.HttpContext.Current.Response.ClearContent();运行到这行出错---未将对象引用设置到对象的实例。
System.Web.HttpContext.Current.Response.ContentType = "image/Jpeg";
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
高手们帮忙看看吧 ,毕设很急(代码是看网上然后自己改的,貌似是ASP.NET的,我的程序是winform)
...全文
116 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xdashewan 2015-05-08
  • 打赏
  • 举报
回复
如果不是真要把图片发送给客户端,那么 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);执行完就可以够了,后面几句可以无视
xian_wwq 2015-05-08
  • 打赏
  • 举报
回复
System.Web相关类是asp.net的,winform中用不了的
Justin-Liu 2015-05-07
  • 打赏
  • 举报
回复
图片太小了有点儿看不清啊,是空引用错误吧? 你用来操作的变量为null,报错了

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧