绘图(包括绘坐标,图片)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
using System.Data.SqlClient;

public partial class Image : System.Web.UI.Page              //制作人:陆永群  
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //获取上个页面传来的值
        ArrayList p = (ArrayList)Cache[Request.QueryString["param"]];     
        int intMapID = int.Parse(p[0].ToString());
        string strCoor = p[1].ToString();
        string[] str = strCoor.Split(',');
        //图片参数
        int iImageWidth = 0, iImageHeight = 0;                                   
        int iPosX, iPosY;

        //从数据库获取数据流
        string strConn;
        string strSQL;
        strConn = System.Configuration.ConfigurationManager.AppSettings["MapConnectionString"];
        SqlConnection sqlConn = new SqlConnection(strConn);
        sqlConn.Open();
        strSQL = "SELECT * FROM tMap WHERE MMapID=@PMapID";
        SqlCommand sqlComm = new SqlCommand(strSQL, sqlConn);
        sqlComm.Parameters.Add(new SqlParameter("@PMapID", SqlDbType.NVarChar));
        sqlComm.Parameters["@PMapID"].Value = intMapID;
        SqlDataReader sqlDR = sqlComm.ExecuteReader();
        MemoryStream ms1=new MemoryStream();
        if (sqlDR.Read())
        {
            ms1 = new MemoryStream((byte[])sqlDR["MMapData"]);
        }

        if (sqlDR != null) sqlDR.Close();
        if (sqlConn != null) sqlConn.Close();
       

        Bitmap bmTemp = new Bitmap(ms1);                     // 用数据流填充图片  并画出来
        iImageWidth = bmTemp.Width;
        iImageHeight = bmTemp.Height;
        Bitmap bmMain = new Bitmap(iImageWidth + 16, iImageHeight + 16);
        Graphics g = Graphics.FromImage(bmMain);
        g.Clear(Color.White);
        iPosX = 15;
        iPosY = 15;
        g.DrawImage(bmTemp, iPosX, iPosY, iImageWidth, iImageHeight);
        bmTemp.Dispose();


        Point p1 = new Point();                             //画坐标轴!
        Point p2 = new Point();
        Pen pen1 = new Pen(new SolidBrush(Color.FromArgb(0, 0, 0)));
        pen1.Color = Color.Black;
        p1.X = 0;
        p1.Y = 0;
        p2.X = iImageWidth + 15;
        p2.Y = 0;
        g.DrawLine(pen1, p1, p2);                   //画外 上线
        p1.X = iImageWidth + 15;
        p1.Y = iImageHeight + 15;
        g.DrawLine(pen1, p2, p1);                   //画外 右线
        p2.X = 0;
        p2.Y = iImageHeight + 15;
        g.DrawLine(pen1, p1, p2);                   //画外 下线
        p1.X = 0;
        p1.Y = 0;
        g.DrawLine(pen1, p2, p1);                   //画外 左线

        p1.X = 15;
        p1.Y = 15;
        p2.X = iImageWidth + 15;
        p2.Y = 15;
        g.DrawLine(pen1, p1, p2);                   //画内
        p2.X = 15;
        p2.Y = iImageHeight + 15;
        g.DrawLine(pen1, p1, p2);                   //画内线
        //横标尺
        int intX = 15;
        while (intX <= iImageWidth + 15)
        {
            p1.X = intX;
            p1.Y = 10;
            p2.X = intX;
            p2.Y = 15;
            if ((intX - 15) % 100 == 0)
            {
                p1.Y = 5;
                p2.Y = iImageHeight + 15;

            }
            g.DrawLine(pen1, p1, p2);
            intX = intX + 10;
        }

        //竖标尺
        int intY = 15;
        while (intY <= iImageHeight + 15)
        {
            p1.X = 10;
            p1.Y = intY;
            p2.X = 15;
            p2.Y = intY;
            if ((intY - 15) % 100 == 0)
            {
                p1.X = 5;
                p2.X = iImageWidth + 15;
            }
            g.DrawLine(pen1, p1, p2);
            intY = intY + 10;
        }
        //为横坐标增加数字标示

        intX = 15;
        int drawInt = 0;
        while (intX <= iImageWidth + 15)
        {

            if ((intX - 15) % 100 == 0)
            {
                drawInt = intX - 15;
                String drawString = drawInt.ToString();
                SolidBrush drawBrush = new SolidBrush(Color.Black);
                Font drawFont = new Font("Arial", 7);
                iPosX = intX;
                iPosY = 0;
                g.DrawString(drawString, drawFont, drawBrush, iPosX, iPosY);
                intX = intX + 100;
            }

        }
        //为纵坐标增加数字标示

        intY = 15;
        drawInt = 0;
        while (intY <= iImageHeight + 15)
        {
            if ((intY - 15) % 100 == 0)
            {
                drawInt = intY - 15;
                String drawString = drawInt.ToString();
                SolidBrush drawBrush = new SolidBrush(Color.Black);
                Font drawFont = new Font("Arial", 7);
                iPosX = 0;
                iPosY = intY;
                g.DrawString(drawString, drawFont, drawBrush, iPosX, iPosY);
                intY = intY + 100;
            }

        }
        //为图片增加数字表示
        intX = 315;
        intY = 115;
        drawInt = 0;
        int drawInt1 = 0;
        while (intY <= iImageHeight + 15)
        {
            while (intX <= iImageWidth + 15)
            {
                if ((intY - 15) % 100 == 0 && (intX - 15) % 300 == 0)
                {
                    drawInt = intX - 15;
                    drawInt1 = intY - 15;
                    String drawString = "("+drawInt.ToString() + "," + drawInt1.ToString()+")";
                    SolidBrush drawBrush = new SolidBrush(Color.Black);
                    Font drawFont = new Font("Arial", 7);
                    iPosX = intX;
                    iPosY = intY;
                    g.DrawString(drawString, drawFont, drawBrush, iPosX, iPosY);

                }
                intX = intX + 300;

            }
            intY = intY + 100;
            intX = 315;
        }

 

        //画数据库原有的热点区域
        sqlConn.Open();
        strSQL = "SELECT * FROM tArea WHERE MMapID=@PMapID";
        SqlCommand sqlComm1 = new SqlCommand(strSQL, sqlConn);
        sqlComm1.Parameters.Add(new SqlParameter("@PMapID", SqlDbType.NVarChar));
        sqlComm1.Parameters["@PMapID"].Value = intMapID;
        SqlDataReader sqlDR1 = sqlComm1.ExecuteReader();
        while (sqlDR1.Read())
        {
            string strArea = sqlDR1["MCoords"].ToString();
            string[] str1 = strArea.Split(',');
            pen1.Color = Color.Coral;
            p1.X = int.Parse(str1[0]) + 15;                   //取第一点
            p1.Y = int.Parse(str1[1]) + 15;
            int i = 2;
            while (i < str1.Length)                           //循环取出其他的点并连接起来。
            {

                p2.X = int.Parse(str1[i]) + 15;
                p2.Y = int.Parse(str1[i + 1]) + 15;
                g.DrawLine(pen1, p1, p2);
                p1.X = p2.X;
                p1.Y = p2.Y;
                i += 2;
            }
            p1.X = int.Parse(str1[0]) + 15;                  //把图形封闭起来
            p1.Y = int.Parse(str1[1]) + 15;
            p2.X = int.Parse(str1[str1.Length - 2]) + 15;
            p2.Y = int.Parse(str1[str1.Length - 1]) + 15;
            g.DrawLine(pen1, p1, p2);
        }

        if (sqlDR != null) sqlDR.Close();
        if (sqlConn != null) sqlConn.Close();

 

        if (strCoor != "")                                      //画预览区域
        {
            pen1.Color = Color.Blue;
            p1.X = int.Parse(str[0])+15;                        //取第一点
            p1.Y = int.Parse(str[1])+15;
            int i = 2;
            while (i < str.Length)                              //循环取出其他的点并连接起来。
            {

                p2.X = int.Parse(str[i])+15;
                p2.Y = int.Parse(str[i+1])+15;
                g.DrawLine(pen1, p1, p2);
                p1.X = p2.X;
                p1.Y = p2.Y;
                i+= 2;
            }
            p1.X = int.Parse(str[0]) + 15;                       //把图形封闭起来
            p1.Y = int.Parse(str[1])+ 15;
            p2.X = int.Parse(str[str.Length - 2]) + 15;
            p2.Y = int.Parse(str[str.Length - 1]) + 15;
            g.DrawLine(pen1, p1, p2);
        }

        bmMain.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
        g.Dispose();
        bmMain.Dispose();                                               //完成


    }
}

你可能感兴趣的:(图片)