GDI+图形图像编程实例(二)
来源:互联网 发布:普陀山求子是迷信 知乎 编辑:程序博客网 时间:2024/06/04 13:09
GDI提供了丰富的图形图像处理功能,主要由二维矢量图形、图像处理等组成。并且GDI对各种字体、字号和样式显示文本提供大量的支持。使用GDI需要引入命名空间using System.Drawing;下面用两个实例来继续说明GDI的使用。
实例三:绘制折线图形
绘制折线图形之前,首先要从数据库中检索出相应的数据,然后根据比列绘制图形。大体上可以分为以下几个步骤:
1.画图之前,首先建立一个画布。使用Bitmap 和Graphics对象,以便能够完成图形绘制。
2.绘制背景墙、网格线以及坐标轴。
3.为绘制的坐标轴绘制数据标记。X轴显示学院名称,Y轴显示学生人数。
4.将从数据库中检索出的数据按一定的比列绘制到图像中。
5.将绘制好的折线图形显示在页面上。
protected void Page_Load(object sender, EventArgs e) { int height = 400, width = 600; Bitmap image = new Bitmap(width, height); Graphics g = Graphics.FromImage(image); try { g.Clear(Color.White); Font font = new Font("Arial", 9, FontStyle.Regular); Font font1 = new Font("宋体", 20, FontStyle.Bold); Font font2 = new Font("宋体", 8, FontStyle.Italic); LinearGradientBrush myLinearGradientBrush = 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.DrawString("西安思源学院各分院人数统计", font1, myLinearGradientBrush, new PointF(130, 40)); g.DrawString("电信学院:王红刚制作", font2, myLinearGradientBrush, new PointF(470, 380)); g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);//绘制边框 Pen mypen2 = new Pen(Color.Red, 2); Pen mypen = new Pen(myLinearGradientBrush, 1);//绘制纵向线条 int x = 100; for (int i = 0; i < 10; i++) { g.DrawLine(mypen, x, 80, x, 340); x = x + 48; } Pen mypen1 = new Pen(Color.Blue, 2);//绘制横向线条 int y = 106; for (int i = 0; i < 6; i++) { g.DrawLine(mypen1, 100, y, 540, y); y = y + 40; } g.DrawLine(mypen1, 100, y, 540, y); String[] n = { "电信", "经贸", "管理", "汽车", "人文", "机电", "外语", "城建", "高职", "文管" };//x轴 x = 85; for (int i = 0; i < 10; i++) { g.DrawString(n[i].ToString(), font, Brushes.Black, x, 348); //设置文字内容及输出位置 x = x + 48; } String[] m = { "3000", "2500", " 2000", " 1500", " 1000", " 500", " 0" }; //y轴 y = 100; for (int i = 0; i < 7; i++) { g.DrawString(m[i].ToString(), font, Brushes.Black, 60, y); //设置文字内容及输出位置 y = y + 40; } int[] Count = new int[10];//查询数据将查询结果放到Count中 SqlConnection conn = new SqlConnection("Server=localhost;Database=ASPNET_tech;Uid=sa;Pwd=sa"); conn.Open(); SqlDataAdapter da; DataSet ds = new DataSet(); for (int i = 0; i < 10; i++) { string str = "select Coll_Num from student_info where ID=" + (i + 1); da = new SqlDataAdapter(str, conn); da.Fill(ds); if (ds.Tables[0].Rows.Count == 0) { Count[i] = 0; } else { Count[i] = Convert.ToInt32(ds.Tables[0].Rows[i]["Coll_Num"].ToString())*40/500; } } conn.Close(); SolidBrush mybrush = new SolidBrush(Color.Red);//绘制折线 Point[] points1 = new Point[10]; points1[0].X = 100; points1[0].Y = 345 - Count[0]; points1[1].X = 148; points1[1].Y = 345 - Count[1]; points1[2].X = 196; points1[2].Y = 345 - Count[2]; points1[3].X = 244; points1[3].Y = 345 - Count[3]; points1[4].X = 292; points1[4].Y = 345 - Count[4]; points1[5].X = 340; points1[5].Y = 345 - Count[5]; points1[6].X = 388; points1[6].Y = 345 - Count[6]; points1[7].X = 436; points1[7].Y = 345 - Count[7]; points1[8].X = 484; points1[8].Y = 345 - Count[8]; points1[9].X = 532; points1[9].Y = 345 - Count[9]; g.DrawLines(mypen2, points1); g.FillRectangle(Brushes.Red, 250, 380, 20, 10); //绘制标识 g.DrawString("人数", font2, Brushes.Red, 270, 380) System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } }
实例四:绘制饼图
protected void Page_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Server=localhost;Database=ASPNET_tech;Uid=sa;Pwd=sa"); conn.Open(); SqlDataAdapter da; DataSet ds = new DataSet(); string str = "select Coll_Name,Coll_Num from student_info"; da = new SqlDataAdapter(str, conn); da.Fill(ds); float Total = 0.0f, Tmp; for (int i = 0; i < ds.Tables[0].Rows.Count; i++)//计算总人数,以确定百分比 { Tmp= Convert.ToInt32(ds.Tables[0].Rows[i]["Coll_Num"].ToString()); Total += Tmp; } conn.Close(); Font fontlegend = new Font("verdana", 9); Font fonttitle = new Font("verdana", 10, FontStyle.Bold); int width = 230; int bufferspace = 15; int legendheight = fontlegend.Height * (ds.Tables[0].Rows.Count + 1) + bufferspace; int titleheight = fonttitle.Height + bufferspace; int height = width + legendheight + titleheight + bufferspace;//白色背景高 int pieheight = width; Rectangle pierect = new Rectangle(0, titleheight, width, pieheight); ArrayList colors = new ArrayList(); //加上各种随机色 Random rnd = new Random(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)))); Bitmap objbitmap = new Bitmap(width, height); //创建一个bitmap实例 Graphics objgraphics = Graphics.FromImage(objbitmap); objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height); //画一个白色背景 objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect); //画一个亮黄色背景 float currentdegree = 0.0f; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) //以下为画饼图(有几行row画几个) { objgraphics.FillPie((SolidBrush)colors[i], pierect, currentdegree, Convert.ToSingle(ds.Tables[0].Rows[i]["Coll_Num"]) / Total * 360); currentdegree += Convert.ToSingle(ds.Tables[0].Rows[i]["Coll_Num"]) / Total * 360; } SolidBrush blackbrush = new SolidBrush(Color.Black); //以下为生成主标题 string title = " 西安思源学院个学院人数统计"; StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; objgraphics.DrawString(title, fonttitle, blackbrush, new Rectangle(0, 0, width, titleheight), stringFormat); objgraphics.DrawRectangle(new Pen(Color.Black, 2), 0, height - legendheight, width, legendheight); //列出各字段与得数目 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { objgraphics.FillRectangle((SolidBrush)colors[i], 5, height - legendheight + fontlegend.Height * i + 5, 10, 10); objgraphics.DrawString(((String)ds.Tables[0].Rows[i]["Coll_Name"]) + " —— " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[i]["Coll_Num"]) * 100 / Total).Substring(0, 5) + "%", fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * i + 1); } objgraphics.DrawString("思源学院总人数:" + Convert.ToString(Total) + "人", fontlegend, blackbrush, 5, height - fontlegend.Height);//图像输出 Response.ContentType = "image/Jpeg"; objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); objgraphics.Dispose(); objbitmap.Dispose(); }
附表中记录:
- GDI+图形图像编程实例(二)
- GDI+图形图像编程实例(一)
- .NET GDI+图形图像编程教程
- GDI(GDI+) - 混合编程【实例】
- 超全面的.NET GDI+图形图像编程教程
- 超全面的.NET GDI+图形图像编程教程
- 超全面的.NET GDI+图形图像编程教程
- GDI+图形图像-----C#
- Windows GDI和GDI+编程实例剖析
- Windows GDI和GDI+编程实例剖析
- Windows GDI和GDI+编程实例剖析
- Windows GDI和GDI+编程实例剖析
- Windows GDI和GDI+编程实例剖析
- Windows GDI和GDI+编程实例剖析
- GDI和GDI+编程实例剖析
- GDI和GDI+编程实例剖析
- Windows GDI和GDI+编程实例剖析
- GDI和GDI+编程实例剖析
- c++ const用法总结
- 不可错过的10个超棒jQuery表单操作代码片段
- C#多线程参数传递
- php字符串转换日期时间格式表
- Dom4j 解析XML
- GDI+图形图像编程实例(二)
- ios 中文件的读写
- usaco--heritage
- [OpenWrt] 新版OpenWrt编译教程
- 反编译Apk得到Java源代码
- netty-discardServer
- Android图片内存优化的几点心得
- 数据导入:从文本文件导入用户信息。 数据导出:将用户信息导出到文本文件。
- UIPickerView的代码实现