照着资料改的一个画图方法中间出现了一大块黑色区域 没搞明白为啥 //自己写的一个画图方法
public void DrawCurve(Page page, DataSet ds, int sum)
{
#region 图片的设置 //获取记录数量
int count = ds.Tables[0].Rows.Count;
int wd = 80 + 20 * (count - 1);
if (wd < 800)
//{
wd = 800;// } Bitmap img = new Bitmap(wd, 800);
Graphics g = Graphics.FromImage(img);
Pen Bp = new Pen(Color.Black);
Pen Rp = new Pen(Color.Red);
Pen Sp = new Pen(Color.Silver);
Font BigTitleFont = new Font("Arial", 12, FontStyle.Bold);
Font NomalFont = new Font("Arial", 6);
Font BigFont = new Font("Arial", 9);
#endregion
g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height);
//画标题的brush
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);
LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);
//绘制大标题
string bigtitle = "历史记录曲线图";
g.DrawString(bigtitle, BigTitleFont, brush, 40, 5);
//取得当前发送量
double nums = 0;
for (int i = 0; i < count; i++)
{
var value = ds.Tables[0].Rows[i]["recordvalue"].ToString().Split('|');
if (value.Length == 2)
{
nums += Convert.ToDouble(value[1].ToString());
break;
}
if (value.Length == 1)
{
nums += Convert.ToDouble(value[0].ToString());
break;
}
if (value.Length == 3)
{
nums += Convert.ToDouble(value[2].ToString());
break;
}
}
//绘制信息简报
string info = "这是信息简报";//"记录时间:" + ds.Tables[0].Rows[0]["recordtime"].ToString() + " 曲线图生成时间:" + DateTime.Now.ToString() + " 记录总量:" + sum.ToString() + " 当前发送总量:" + nums.ToString();
g.DrawString(info, BigFont, Bluebrush, 40, 25);
//绘制图片边框
g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1); //绘制竖坐标线
for (int i = 0; i < count; i++)
{
g.DrawLine(Sp, 40 + 20 * i, 60, 40 + 20 * i, 720);
}
//绘制时间轴坐标标签
for (int a = 0; a < ds.Tables[0].Rows.Count;a++)
{ string st = Convert.ToDateTime(ds.Tables[0].Rows[a][1]).ToShortTimeString();
g.DrawString(st.ToString(), NomalFont, brush, 30 + 20 * a, 370);
}
//绘制横坐标线
for (int i = 0; i < 21; i++)
{
g.DrawLine(Sp, 40, 60 + 30 * i, 40 + 20 * (count - 1), 60 + 30 * i);
int s = 100 - i * 10;
//绘制发送量轴坐标标签
g.DrawString(s.ToString(), NomalFont, brush, 10, 60 + 30 * i);
} //绘制竖坐标轴
g.DrawLine(Bp, 40, 55, 40, 360);
//绘制横坐标轴
g.DrawLine(Bp, 40, 360, 45 + 20 * (count - 1), 360); //定义曲线转折点
Point[] p = new Point[count];
for (int i = 0; i < count; i++)
{
var value = ds.Tables[0].Rows[i]["recordvalue"].ToString().Split('|');
string thevalue = "";
if (value.Length == 2)
{
thevalue = value[1].ToString(); }
if (value.Length == 1)
{
thevalue = value[0].ToString(); }
if (value.Length == 3)
{
thevalue = value[2].ToString(); }
p[i].X = 40 + 20 * i;
thevalue = Math.Round(Convert.ToDouble(thevalue),0).ToString();
p[i].Y =360 - Convert.ToInt32(thevalue)*3 ;// 360 - Convert.ToInt32(thevalue) / 5 * 3 / 5;
}
//绘制发送曲线
g.DrawLines(Rp, p);
for (int i = 0; i < count; i++)
{
var value = ds.Tables[0].Rows[i]["recordvalue"].ToString().Split('|');
string thevalue = "";
if (value.Length == 2)
{
thevalue = value[1].ToString();
}
if (value.Length == 1)
{
thevalue= value[0].ToString();
}
if (value.Length == 3)
{
thevalue = value[2].ToString();
}
//绘制发送记录点的发送量
g.DrawString(thevalue, NomalFont, Bluebrush, p[i].X, p[i].Y - 10);
//绘制发送记录点
g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
}
//绘制竖坐标标题
g.DrawString("数值", BigFont, brush, 5, 40);
//绘制横坐标标题
g.DrawString("时间", BigFont, brush, 40, 385); #region 保存绘制的图片,图片输出 MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Jpeg); page.Response.Clear();
page.Response.ContentType = "image/jpeg";
page.Response.BinaryWrite(stream.ToArray());
#endregion
}GDI+asp.net
public void DrawCurve(Page page, DataSet ds, int sum)
{
#region 图片的设置 //获取记录数量
int count = ds.Tables[0].Rows.Count;
int wd = 80 + 20 * (count - 1);
if (wd < 800)
//{
wd = 800;// } Bitmap img = new Bitmap(wd, 800);
Graphics g = Graphics.FromImage(img);
Pen Bp = new Pen(Color.Black);
Pen Rp = new Pen(Color.Red);
Pen Sp = new Pen(Color.Silver);
Font BigTitleFont = new Font("Arial", 12, FontStyle.Bold);
Font NomalFont = new Font("Arial", 6);
Font BigFont = new Font("Arial", 9);
#endregion
g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height);
//画标题的brush
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);
LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);
//绘制大标题
string bigtitle = "历史记录曲线图";
g.DrawString(bigtitle, BigTitleFont, brush, 40, 5);
//取得当前发送量
double nums = 0;
for (int i = 0; i < count; i++)
{
var value = ds.Tables[0].Rows[i]["recordvalue"].ToString().Split('|');
if (value.Length == 2)
{
nums += Convert.ToDouble(value[1].ToString());
break;
}
if (value.Length == 1)
{
nums += Convert.ToDouble(value[0].ToString());
break;
}
if (value.Length == 3)
{
nums += Convert.ToDouble(value[2].ToString());
break;
}
}
//绘制信息简报
string info = "这是信息简报";//"记录时间:" + ds.Tables[0].Rows[0]["recordtime"].ToString() + " 曲线图生成时间:" + DateTime.Now.ToString() + " 记录总量:" + sum.ToString() + " 当前发送总量:" + nums.ToString();
g.DrawString(info, BigFont, Bluebrush, 40, 25);
//绘制图片边框
g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1); //绘制竖坐标线
for (int i = 0; i < count; i++)
{
g.DrawLine(Sp, 40 + 20 * i, 60, 40 + 20 * i, 720);
}
//绘制时间轴坐标标签
for (int a = 0; a < ds.Tables[0].Rows.Count;a++)
{ string st = Convert.ToDateTime(ds.Tables[0].Rows[a][1]).ToShortTimeString();
g.DrawString(st.ToString(), NomalFont, brush, 30 + 20 * a, 370);
}
//绘制横坐标线
for (int i = 0; i < 21; i++)
{
g.DrawLine(Sp, 40, 60 + 30 * i, 40 + 20 * (count - 1), 60 + 30 * i);
int s = 100 - i * 10;
//绘制发送量轴坐标标签
g.DrawString(s.ToString(), NomalFont, brush, 10, 60 + 30 * i);
} //绘制竖坐标轴
g.DrawLine(Bp, 40, 55, 40, 360);
//绘制横坐标轴
g.DrawLine(Bp, 40, 360, 45 + 20 * (count - 1), 360); //定义曲线转折点
Point[] p = new Point[count];
for (int i = 0; i < count; i++)
{
var value = ds.Tables[0].Rows[i]["recordvalue"].ToString().Split('|');
string thevalue = "";
if (value.Length == 2)
{
thevalue = value[1].ToString(); }
if (value.Length == 1)
{
thevalue = value[0].ToString(); }
if (value.Length == 3)
{
thevalue = value[2].ToString(); }
p[i].X = 40 + 20 * i;
thevalue = Math.Round(Convert.ToDouble(thevalue),0).ToString();
p[i].Y =360 - Convert.ToInt32(thevalue)*3 ;// 360 - Convert.ToInt32(thevalue) / 5 * 3 / 5;
}
//绘制发送曲线
g.DrawLines(Rp, p);
for (int i = 0; i < count; i++)
{
var value = ds.Tables[0].Rows[i]["recordvalue"].ToString().Split('|');
string thevalue = "";
if (value.Length == 2)
{
thevalue = value[1].ToString();
}
if (value.Length == 1)
{
thevalue= value[0].ToString();
}
if (value.Length == 3)
{
thevalue = value[2].ToString();
}
//绘制发送记录点的发送量
g.DrawString(thevalue, NomalFont, Bluebrush, p[i].X, p[i].Y - 10);
//绘制发送记录点
g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
}
//绘制竖坐标标题
g.DrawString("数值", BigFont, brush, 5, 40);
//绘制横坐标标题
g.DrawString("时间", BigFont, brush, 40, 385); #region 保存绘制的图片,图片输出 MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Jpeg); page.Response.Clear();
page.Response.ContentType = "image/jpeg";
page.Response.BinaryWrite(stream.ToArray());
#endregion
}GDI+asp.net
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货