showImage .cs
上传用户:ah_jiwei
上传日期:2022-07-24
资源大小:54044k
文件大小:4k
源码类别:

数据库编程

开发平台:

Visual C++

  1. using System;
  2. using System.Web;
  3. using System.Web.UI;
  4. using System.Drawing;
  5. using System.Collections;
  6. using System.Drawing.Imaging;
  7. using System.Drawing.Drawing2D;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.Web.Configuration;
  11. public class showImage 
  12. {
  13.     struct department
  14.     {
  15.         public string name;
  16.         public int number;
  17.     }
  18.     
  19.     public void ProcessRequest ()
  20.     {
  21.         ArrayList departments = new ArrayList();
  22.         department dp;
  23.        
  24.         SolidBrush sb = new SolidBrush(Color.Aqua);
  25.         Random rd = new Random();
  26.         SqlConnection connection = DB.connect_la();
  27.         int sum = 0;
  28.         int success = 0;
  29.         int startAngle = 0;
  30.         int sweepAngle = 45;
  31.         //connect the database
  32.         try
  33.         {
  34.             connection.Open();
  35.            
  36.              SqlCommand comm1 = new SqlCommand("select count(sut_id) from stu_tea where state = '已选择'", connection);
  37.              SqlDataReader dr1 = comm1.ExecuteReader();
  38.              if (dr1.Read())
  39.              {
  40.                  dp.name = "已确定导师";
  41.                  dp.number = Convert.ToInt32(dr1.GetInt32(0));
  42.                  success = dr1.GetInt32(0);
  43.                  departments.Add(dp);
  44.              }
  45.              dr1.Close();
  46.              SqlCommand comm2 = new SqlCommand("select count(stu_id) from stu_id_num", connection);
  47.              SqlDataReader dr2 = comm2.ExecuteReader();
  48.              if (dr2.Read())
  49.              {
  50.                  dp.name = "未确定导师";
  51.                  sum = dr2.GetInt32(0);
  52.                  dp.number = sum-success;
  53.                  departments.Add(dp);
  54.              }
  55.              dr2.Close();
  56.          }
  57.          catch (Exception ex)
  58.          {
  59.              throw new Exception(ex.Message);
  60.          }
  61.          finally
  62.          {
  63.              connection.Close();
  64.          }
  65.          //Draw the pie of the every department
  66.          if (departments.Count > 0)
  67.          {
  68.              using (Bitmap bm = new Bitmap(200,260))
  69.              {
  70.                  using (Graphics gp = Graphics.FromImage(bm))
  71.                  {
  72.                      gp.SmoothingMode = SmoothingMode.AntiAlias;
  73.                      gp.Clear(Color.White);
  74.                      //Rectangle rect = new Rectangle(0, 0, 200, 200);
  75.                      int pointX = 0;
  76.                      int pointY = 20;
  77.                      gp.DrawLine(Pens.Black, 10, 204, 190, 204);
  78.                      bool single = true;
  79.                      int y = 210;
  80.                      for (int i = 0; i < departments.Count; i++)
  81.                      {
  82.                         dp = (department)departments[i];
  83.                          sb.Color = Color.FromArgb(rd.Next(255), rd.Next(255), rd.Next(255));
  84.                          sweepAngle = Convert.ToInt32(360 * Convert.ToSingle(dp.number) / Convert.ToSingle(sum));
  85.                          //gp.FillPie(sb, rect, startAngle, sweepAngle);
  86.                         if (startAngle < 180)
  87.                         {
  88.                             for (int height = 0; height < 8; height++)
  89.                             {
  90.                                 gp.FillPie(Brushes.Black, pointX, pointY + height, 200, 100, startAngle, sweepAngle);
  91.                             }
  92.                         }
  93.                         gp.FillPie(sb, pointX, pointY, 200, 100, startAngle, sweepAngle);
  94.                         startAngle += sweepAngle;
  95.                         if (single)
  96.                         {
  97.                             gp.FillRectangle(sb, new Rectangle(10, y, 20, 15));
  98.                             gp.DrawString(dp.name, new Font("Tahoma", 8, FontStyle.Regular), Brushes.Black, new PointF(30, y));
  99.                             single = false;
  100.                         }
  101.                         else
  102.                         {
  103.                             gp.FillRectangle(sb, new Rectangle(110, y, 20, 15));
  104.                             gp.DrawString(dp.name, new Font("Tahoma", 8, FontStyle.Regular), Brushes.Black, new PointF(130, y));
  105.                             single = true;
  106.                             y += 20;
  107.                         }
  108.                    }
  109.                     //save the image in the page
  110.                     gp.DrawLine(Pens.Black, 10, 258, 190, 258);
  111.                     string path = System.AppDomain.CurrentDomain.BaseDirectory+"\image\graph1.gif";
  112.                     bm.Save(path);
  113.                 }
  114.             }
  115.         }
  116.     }
  117.  
  118.    
  119.    
  120. }