在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 绘图并保存至数据库

绘图并保存至数据库

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.30M
  • 下载次数:17
  • 浏览次数:73
  • 发布时间:2019-09-18
  • 实例类别:C#语言基础
  • 发 布 人:龙普成
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 数据库 数据 保存 绘图

实例介绍

【实例简介】

【实例截图】

from clipboard


from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Xml;

namespace 实验9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        enum DrawType { NoDraw, Point, Line, Polygon,Information }
        enum PN {主教,电信楼,体育场,逸夫楼,生科楼}
        DrawType dt = DrawType.NoDraw;
        bool isPolygonCompelete = false;
        //bool isMouseDown = false;
        //bool pd=false;
        Point pt;
        //Point prevPoint;
        Line l;
        FileStream fs;
        Polygon ply = new Polygon();
        List<Point> points = new List<Point>();
        List<Line> myLine = new List<Line>();
        List<Polygon> myPolygon = new List<Polygon>();
        OleDbConnection conn;
        OleDbDataAdapter oda;
        OleDbCommand comm;
        OleDbCommandBuilder ocb;
        DataSet ds=new DataSet ();
        //DataTable datt;
        //string Access;
        //XmlNode newNode;
        class Line
        {
            public Point p1 = new Point();
            public Point p2 = new Point();
            public Line()
            {
                List<Line> lines = new List<Line>();
            }
        }
        class Polygon
        {
            public List<Point> polygons;
            public Polygon()
            { polygons = new List<Point>(); }
        }
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (dt == DrawType.Point)
            {
                pt = new Point();
                pt = e.Location;
                points.Add(pt);
                Invalidate();
            }
            if (dt == DrawType.Line)
            {
                //isMouseDown = true;
                l = new Line();
                pt = new Point();
                pt = e.Location;
                l.p1 = pt;
                //prevPoint.X = e.X; prevPoint.Y = e.Y;
                myLine.Add(l);
            }
            if (dt == DrawType.Polygon)
            {
                if (isPolygonCompelete)
                {
                    ply = new Polygon();
                    isPolygonCompelete = false;
                }
                if (e.Button == MouseButtons.Left)
                {
                    pt = new Point();
                    pt = e.Location;
                    ply.polygons.Add(pt);
                    myPolygon.Add(ply);
                }
                if (e.Button == MouseButtons.Right)
                {
                    isPolygonCompelete = true;
                    Invalidate();
                }
                
            }
            if (dt == DrawType.Information)
            {
                comm = new OleDbCommand();
                comm.Connection = conn;
                comm.CommandText = "select * from point";
                OleDbDataReader read = comm.ExecuteReader();
                while (read.Read())
                {
                    Int32 k1 = e.X - Int32.Parse(read["X"].ToString());
                    Int32 k2 = e.Y - Int32.Parse(read["Y"].ToString());
                    if (Math.Pow(k1, 2)   Math.Pow(k2, 2) < 25)
                    {
                        MessageBox.Show(read["p_name"].ToString());
                        break;
                    }
                }
                read.Close();
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            /*Point p = new Point(e.X, e.Y);
            if (isMouseDown)
            {

                ControlPaint.DrawReversibleLine(PointToScreen(pt), PointToScreen(prevPoint), Color.Azure);
                ControlPaint.DrawReversibleLine(PointToScreen(pt), PointToScreen(p), Color.Azure);
                prevPoint.X = e.X; prevPoint.Y = e.Y;
            }*/
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (dt == DrawType.Line)
            {
                pt = new Point();
                pt = e.Location;
                l.p2 = pt;
                Invalidate();
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen myPen = new Pen(Color.Red, 3);
            SolidBrush myBrush = new SolidBrush(Color.Black);
            //if (dt == DrawType.Point)
            {
                foreach (Point pt in points)
                    g.FillRectangle(myBrush, pt.X, pt.Y, 3, 3);
            }
            //if (dt == DrawType.Line)
            {
                foreach (Line l in myLine)
                    g.DrawLine(myPen, l.p1, l.p2);
            }
            //if (dt == DrawType.Polygon)
            for (int i = 0; i < myPolygon.Count; i  )
            {

                g.DrawPolygon(myPen, myPolygon[i].polygons.ToArray());
            }
        }

        private void 绘点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dt = DrawType.Point;
        }

        private void 绘线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dt = DrawType.Line;
        }

        private void 绘多边形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dt = DrawType.Polygon;
        }

        private void 打开点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            oda = new OleDbDataAdapter("select * from point", conn);
            oda.Fill(ds, "point");
            dataGridView1.DataSource = ds.Tables["point"];
            for (int i = 0; i < ds.Tables[0].Rows.Count; i  )
            {
                pt = new Point();
                string str;
                str = ds.Tables["point"].Rows[i]["X"].ToString();
                pt.X=Convert.ToInt32(str);
                str = ds.Tables["point"].Rows[i]["Y"].ToString();
                pt.Y = Convert.ToInt32(str);
                points.Add(pt);
                Invalidate();
            }
            
        }

        private void 打开线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ds = new DataSet();
            oda = new OleDbDataAdapter("select * from 线", conn);
            if (ds.Tables["线"] != null)
                ds.Tables["线"].Clear();
            oda.Fill(ds, "线");
            dataGridView1.DataSource = ds.Tables["线"];
            for (int i = 0; i < ds.Tables[0].Rows.Count; i  )
            {
                l = new Line();
                string str;
                str = ds.Tables["线"].Rows[i]["X1"].ToString();
                l.p1.X = Convert.ToInt32(str);
                str = ds.Tables["线"].Rows[i]["Y1"].ToString();
                l.p1.Y = Convert.ToInt32(str);
                str = ds.Tables["线"].Rows[i]["X2"].ToString();
                l.p2.X = Convert.ToInt32(str);
                str = ds.Tables["线"].Rows[i]["Y2"].ToString();
                l.p2.Y = Convert.ToInt32(str);
                myLine.Add(l);
                Invalidate();
            }
        }

        private void 打开多边形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /*XmlDocument xd = new XmlDocument();
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "XML文件|*.xml";
            if (ofd.ShowDialog() == DialogResult.Cancel) return;
            xd.Load(ofd.FileName);
            XmlNodeList xn = xd.SelectSingleNode("graphics").SelectSingleNode("points").ChildNodes;
            foreach (XmlElement xe in xn)
            {
                Polygon pgon = new Polygon();
                XmlNodeList xnchild = xe.ChildNodes;
                foreach (XmlElement xe1 in xnchild)
                {
                    Point pt = new Point();
                    pt.X = Int32.Parse(xe1.GetAttribute("x"));
                    pt.Y = Int32.Parse(xe1.GetAttribute("y"));
                    pgon.polygons.Add(pt);
                }
                myPolygon.Add(pgon);
            }*/
            ds = new DataSet();
            oda = new OleDbDataAdapter("select * from 多边形", conn);
            if (ds.Tables["多边形"] != null)
                ds.Tables["多边形"].Clear();
            oda.Fill(ds, "多边形");
            dataGridView1.DataSource = ds.Tables["多边形"];
            for (int i = 0; i < ds.Tables[0].Rows.Count; i  )
            {
                l = new Line();
                string str;
                str = ds.Tables["多边形"].Rows[i]["X1"].ToString();
                l.p1.X = Convert.ToInt32(str);
                str = ds.Tables["多边形"].Rows[i]["Y1"].ToString();
                l.p1.Y = Convert.ToInt32(str);
                str = ds.Tables["多边形"].Rows[i]["X2"].ToString();
                l.p2.X = Convert.ToInt32(str);
                str = ds.Tables["多边形"].Rows[i]["Y2"].ToString();
                l.p2.Y = Convert.ToInt32(str);
                myLine.Add(l);
                Invalidate();
            }
            Invalidate();
        }

        private void 保存点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            comm = new OleDbCommand();
            comm.CommandText = "delete from point";
            comm.Connection = conn;
            comm.ExecuteNonQuery();
            oda = new OleDbDataAdapter("select * from point", conn);
            oda.Fill(ds, "point");
            for (int i = 0; i < points.Count; i  )
            {
                DataRow r1 = ds.Tables["point"].NewRow();
                r1["X"] = points[i].X.ToString();
                r1["Y"] = points[i].Y.ToString();
                ds.Tables[0].Rows.Add(r1);
            }
            dataGridView1.DataSource = ds.Tables["point"];
            //dataGridView1.Columns[0].Visible = false;
            ocb = new OleDbCommandBuilder(oda);
                try { oda.Update(ds, "point"); }
                catch (Exception ex)
                { MessageBox.Show("数据修改不正确", "提示"); }
        }

        private void 保存线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            comm = new OleDbCommand();
            comm.CommandText = "delete from 线";
            comm.Connection = conn;
            comm.ExecuteNonQuery();
            ds = new DataSet();
            oda = new OleDbDataAdapter("select * from 线", conn);
            oda.Fill(ds, "线");
            for (int i = 0; i < myLine.Count; i  )
            {
                DataRow r1 = ds.Tables[0].NewRow();
                r1["X1"] = myLine[i].p1.X.ToString();
                r1["Y1"] = myLine[i].p1.Y.ToString();
                r1["X2"] = myLine[i].p2.X.ToString();
                r1["Y2"] = myLine[i].p2.Y.ToString();
                ds.Tables[0].Rows.Add(r1);
            }
            dataGridView1.DataSource = ds.Tables["线"];
            ocb = new OleDbCommandBuilder(oda);
                try { oda.Update(ds, "线"); }
                catch (Exception ex)
                { MessageBox.Show("数据修改不正确", "提示"); }
        }

        private void 保存多边形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            comm = new OleDbCommand();
            comm.CommandText = "delete from 多边形";
            comm.Connection = conn;
            comm.ExecuteNonQuery();
            XmlDocument xd = new XmlDocument();
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "XML文件|*.xml";
            if (sfd.ShowDialog() == DialogResult.Cancel) return;
            if (!File.Exists(sfd.FileName))//文件若不存在,则创建这个文件,并写入XML根
            {
                fs = new FileStream(sfd.FileName, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("<graphics>");
                sw.WriteLine("</graphics>");
                sw.Close();
                fs.Close();
            }
            xd.Load(sfd.FileName);
            XmlNode xn = xd.SelectSingleNode("graphics").SelectSingleNode("points");
            if (xn == null)//没有points, 则添加此节点
            {
                XmlNode newNode = xd.CreateElement("points");
                xn = xd.SelectSingleNode("graphics");
                xn.AppendChild(newNode);
                xn = xd.SelectSingleNode("graphics").SelectSingleNode("points");
            }
            foreach (Polygon pg in myPolygon)
            {
                XmlNode newNode = xd.CreateElement("polygon");
                foreach (Point pt in pg.polygons)
                {
                    XmlElement xe1 = xd.CreateElement("points");
                    xe1.SetAttribute("x", pt.X.ToString());
                    xe1.SetAttribute("y", pt.Y.ToString());
                    newNode.AppendChild(xe1);
                }
                xn.AppendChild(newNode);
            }
            xd.Save(sfd.FileName);
            ds.ReadXml(sfd.FileName);
            foreach (DataTable table in ds.Tables)
            {
                textBox1.Text = "表名:"   table.TableName   "\r\n";
                foreach (DataRow row in table.Rows)
                {
                    foreach (DataColumn column in table.Columns)
                    {
                        textBox1.Text = textBox1.Text   "\t"  row[column];
                        string[] s= textBox1.Text.Split('\t');
                        comm = new OleDbCommand("insert into 多边形(X,Y,ct) values(s[1],s[3],s[5])", conn);
                        comm.ExecuteNonQuery();
                        comm.CommandType = CommandType.Text;
                        conn.Close();
                    }
                    textBox1.Text = textBox1.Text   "\r\n";
                }
            } 
            oda.Fill(ds,"多边形");
            oda.Update(ds);
            dataGridView1.DataSource = ds;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            conn = new OleDbConnection();
            conn.ConnectionString = 
                "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "表2.accdb");
            conn.Open();
        }

        private void 信息查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dt = DrawType.Information;
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            conn.Close();
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {

        }
    }
}

实例下载地址

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警