这个只有TC上能用,你如果要学学画线,画图这类的,可以去学一下 opengl
这样说是不对的, Windows 提供了丰富的图形图像API接口, 例如基础的 GDI, 通过C语言来进行在 Windows 上的图形的绘制以及输出是完全没有问题的。
@wid: 呵呵,对的,我只是估计他想问graphic.h里面的函数怎么用,我以前刚学C语言时也有同样的困惑
嗯 谢谢指点
@DayDayUpCQ: 不知可以把graphic.h包含的函数下载到VC++的库函数中然后在编译
private void CreateImg() { String[] m = m1;
if (getMax() >= 60 && getMax() < 80) { m = m2; } else if (getMax() >= 80) { m = m3; }
int height = Convert.ToInt32(ImgHeight), width = Convert.ToInt32(ImgWidth); Bitmap image = new Bitmap(width, height); //创建Graphics类对象 Graphics g = Graphics.FromImage(image); try { Font font = xTextFont;//X轴字体 Font font2 = yTextFont;//Y轴字体 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); g.Clear(Color.Transparent);//此代码放的位置决定显示//清空图片背景色为透明 //g.Clear(Color.White); // Brush brush1 = new SolidBrush(Color.Blue); //g.DrawString("成绩统计柱状图", font1, brush, new PointF(70, 30)); //画图片的边框线 //g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1); //绘制线条 //绘制竖向线条 //for (int i = 0; i < 14; i++) //{ //g.DrawLine(mypen, x, 80, x, 340);//绘制除y轴外的其它竖向线条 // x = x + 40; //} //x = 0; //绘制横向线条 //for (int i = 0; i < 9; i++) //{ //g.DrawLine(mypen, 60, y, 620, y);//绘制除x轴外的横向线条 // y = y + 26; //} Pen mypen = new Pen(brush, 1); Pen mypen1 = new Pen(yColor, pyWidth); g.DrawLine(mypen1, xWidth, 0, xWidth, height - yHeight);//绘制y轴 Pen mypen2 = new Pen(xColor, pxWidth); g.DrawLine(mypen2, xWidth, height - yHeight, width, height - yHeight);//绘制X轴 //x轴 int xrowWidth = (int)(Math.Ceiling(g.MeasureString(getTextWidth(), font).Width));//文字的宽度 int xrowHeight = (int)(Math.Ceiling(g.MeasureString("测试", font, 100, StringFormat.GenericTypographic).Height));//文字的高度 int xspan = (int)Math.Ceiling((float)(Convert.ToInt32(ImgWidth) - xWidth - chartXSpan - pyWidth - xrowWidth) / (cachede.Count - 1));//绘制的坐标为起始坐标,正常绘制的长度会多出一组字符串的长度(长度为x,绘制n组字符串,则算坐标区间时应减去一组字符串的长度) int x = xWidth + chartXSpan; foreach (KeyValuePair<string, string> kvp in cachede) { g.DrawString(kvp.Key, font, xTextBrush, x, Convert.ToInt32(ImgHeight) - xrowHeight); //设置文字内容及输出位置 x = x + xspan; } //y轴 int yspan = 0; int rowHeight = (int)(Math.Ceiling(g.MeasureString("50", font2).Height));//文字的高度 int y = Convert.ToInt32(ImgHeight) - yHeight - rowHeight - pxWidth; yspan = (int)Math.Ceiling((float)(Convert.ToInt32(ImgHeight) - yHeight - pxWidth - (rowHeight + ytopspan)) / (m.Length - 1));//减去rowHeight+20:绘制长度y减去了一个rowheight,此处算yspan时也要减去,另外20是留出y轴最后的文字与顶端的距离。 for (int i = 0; i < m.Length; i++) { g.DrawString(m[i].ToString(), font2, yTextBrush, 0, y); //设置文字内容及输出位置 y = y - yspan; } //绘制柱状图. int ximg = xWidth + chartXSpan;//绘制图x轴坐标点 int textwidth = 0;//文字的宽度 int ximgspan = 0;//绘制图形与对应X轴文字对应位置 SolidBrush mybrush = new SolidBrush(chartColor); foreach (KeyValuePair<string, string> kvp in cachede) //绘制x轴柱状图 { textwidth = (int)(Math.Ceiling(g.MeasureString(kvp.Key, font).Width)); ximgspan = (int)Math.Ceiling((float)(textwidth - chartWidth) / 2); //考虑显示最高的百分比数值 int yRealHeight = (int)Math.Ceiling((float)((Convert.ToInt32(ImgHeight) - yHeight - pxWidth - ytopspan) * 100 * Convert.ToDouble(kvp.Value)) / (100 * getMaxY(m)));//计算柱状图所占高度 g.FillRectangle(mybrush, ximg + ximgspan, ImgHeight - yHeight - pxWidth - yRealHeight, chartWidth, yRealHeight); g.DrawString(kvp.Value + "%", chartTextFont, chartTextBrush, ximg + ximgspan - 5, ImgHeight - yHeight - pxWidth - yRealHeight - rowHeight); ximg = ximg + xspan; } //绘制标识 //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.Png); System.Web.HttpContext.Current.Response.ClearContent(); System.Web.HttpContext.Current.Response.ContentType = "image/Png"; System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } }
谢谢