问题描述
写了个方法关于条形统计图的,程序运行到最后几步时出错,如下图:完整代码如下:privatevoidCreateImage(){intheight=500,width=700;Bitmapimage=newBitmap(width,height);//创建Graphics类对象Graphicsg=Graphics.FromImage(image);try{//清空图片背景色g.Clear(Color.White);Fontfont=newFont("Arial",10,FontStyle.Regular);Fontfont1=newFont("宋体",20,FontStyle.Bold);LinearGradientBrushbrush=newLinearGradientBrush(newRectangle(0,0,image.Width,image.Height),Color.Blue,Color.BlueViolet,1.2f,true);g.FillRectangle(Brushes.WhiteSmoke,0,0,width,height);//Brushbrush1=newSolidBrush(Color.Blue);g.DrawString(this.dateTimePicker1.Text+"家庭收支情况图",font1,brush,newPointF(70,30));//画图片的边框线g.DrawRectangle(newPen(Color.Blue),0,0,image.Width-1,image.Height-1);Penmypen=newPen(brush,1);//绘制线条//绘制横向线条intx=100;for(inti=0;i<27;i++){g.DrawLine(mypen,x,80,x,600);x=x+40;}Penmypen1=newPen(Color.Blue,2);x=60;g.DrawLine(mypen1,x,80,x,600);//绘制纵向线条inty=106;for(inti=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(inti=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(inti=0;i<11;i++){g.DrawString(m[i].ToString(),font,Brushes.Blue,y,1000);//设置文字内容及输出位置y=y+1000;}int[]Count1=newint[24];int[]Count2=newint[24];////连接数据库并返回符合查询条件的值//stringconnectString="DataSource=.;InitialCatalog=FinSystem;UserID=sa;pwd=1992106713";SqlConnectionsqlCnt=newSqlConnection(connectString);sqlCnt.Open();//下列的语句实现的功能是将预算表中的“预算金额”列和“完成金额”列分别按每个月求和(利用SQLServer的子查询及聚合函数)stringcmdtxt="select*fromTable_Budgetinfowhere预算日期Between'"+dateTimePicker1.Value+"'AND'"+dateTimePicker2.Value+"'";SqlDataAdapterda=newSqlDataAdapter(cmdtxt,sqlCnt);DataSetds=newDataSet();da.Fill(ds);//将返回的数据填充到数据集ds中//将数据集ds的表中数据值转换并填充到对应数组中......//绘制柱状图.x=80;Fontfont2=newSystem.Drawing.Font("Arial",10,FontStyle.Bold);SolidBrushmybrush=newSolidBrush(Color.Red);SolidBrushmybrush2=newSolidBrush(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);//绘制标识Fontfont3=newSystem.Drawing.Font("Arial",10,FontStyle.Regular);g.DrawRectangle(newPen(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.MemoryStreamms=newSystem.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)
解决方案
本帖最后由 lucky566 于 2015-05-06 18:04:48 编辑
解决方案二:
图片太小了有点儿看不清啊,是空引用错误吧?你用来操作的变量为null,报错了
解决方案三:
System.Web相关类是asp.net的,winform中用不了的
解决方案四:
如果不是真要把图片发送给客户端,那么image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);执行完就可以够了,后面几句可以无视