Ejemplo n.º 1
0
        public override void readText(StreamReader sr)
        {
            FreeLine myFreeLine = new FreeLine();

            Form1.Instance.penWidth = Convert.ToInt32(Form1.Instance.dataArray[1]);
            Form1.Instance.penColor = Color.FromArgb(int.Parse(Form1.Instance.dataArray[2], System.Globalization.NumberStyles.AllowHexSpecifier));
            Pen myPen = new Pen(Form1.Instance.penColor, Form1.Instance.penWidth);

            myFreeLine = new FreeLine(myPen);
            for (int i = 3; i < Form1.Instance.dataArray.Length; i = i + 2)
            {
                myFreeLine.pt1.X = Convert.ToInt32(Form1.Instance.dataArray[i]);
                myFreeLine.pt1.Y = Convert.ToInt32(Form1.Instance.dataArray[i + 1]);
                myFreeLine.FreeList.Add(myFreeLine.pt1);
            }
            Form1.Instance.shapeList.Add(myFreeLine);
        }
Ejemplo n.º 2
0
        public override void readBinary(BinaryReader br)
        {
            Form1.Instance.penWidth = br.ReadInt32();
            Form1.Instance.penColor = Color.FromArgb(br.ReadInt32());
            Pen      myPen      = new Pen(Form1.Instance.penColor, Form1.Instance.penWidth);
            FreeLine myFreeLine = new FreeLine(myPen);
            int      count      = br.ReadInt32();
            int      i          = 0;

            while (i < count) // DONE//
            {
                pt1.X = br.ReadInt32();
                pt1.Y = br.ReadInt32();
                myFreeLine.FreeList.Add(pt1);
                i++;
            }
            Form1.Instance.shapeList.Add(myFreeLine);
        }
Ejemplo n.º 3
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result;

            using (OpenFileDialog fileChooser = new OpenFileDialog())
            {
                fileChooser.Filter          = "Text Files (*.txt) |*.txt| Binary Files (*.bin) |*.bin";
                fileChooser.DefaultExt      = "txt";
                fileChooser.CheckFileExists = false; //Don't need to verify file name after save.
                result      = fileChooser.ShowDialog();
                currentFile = fileChooser.FileName;
                extension   = Path.GetExtension(fileChooser.FileName);
            }
            try
            {
                if (result == DialogResult.OK && extension == ".txt")
                {
                    shapeList.Clear();
                    Invalidate();
                    FileStream   fs = new FileStream(currentFile, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs);


                    // Reading of Text Data
                    while (true)
                    {
                        string dataLine = sr.ReadLine();
                        if (dataLine != null)
                        {
                            dataArray = dataLine.Split(',', ';', ':'); // Splits at every , ; :

                            if (dataArray[0] == "Line")
                            {
                                Line Line = new Line(this);
                                Line.readText(sr);
                                penWidth = Convert.ToInt32(dataArray[5]);
                                penColor = Color.FromArgb(int.Parse(dataArray[6], System.Globalization.NumberStyles.AllowHexSpecifier));//string hex converted to int
                                Pen myPen = new Pen(penColor, penWidth);
                                Line = new Line(Line.Pt1, Line.Pt2, myPen);

                                shapeList.Add(Line);
                            }
                            else if (dataArray[0] == "Rectangle")
                            {
                                Rect myRect = new Rect();
                                myRect.readText(sr);
                                penWidth = Convert.ToInt32(dataArray[5]);
                                penColor = Color.FromArgb(int.Parse(dataArray[6], System.Globalization.NumberStyles.AllowHexSpecifier));
                                Pen myPen = new Pen(penColor, penWidth);
                                myRect = new Rect(myRect.Pt1, myRect.Pt2, myPen);

                                shapeList.Add(myRect);
                            }
                            else if (dataArray[0] == "FreeLine")
                            {
                                FreeLine myFreeLine = new FreeLine();
                                myFreeLine.readText(sr);
                            }
                        }
                        else
                        {
                            sr.Close();
                            Invalidate();
                            break;
                        }
                    }
                    savedFileExists = true;
                }
                else if (result == DialogResult.OK && extension == ".bin")
                {
                    string shapeType;
                    shapeList.Clear();
                    Invalidate();
                    FileStream   fs = new FileStream(currentFile, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);

                    // Reading Binary Data
                    while (true)
                    {
                        shapeType = null;
                        if (br.BaseStream.Position != br.BaseStream.Length)
                        {
                            shapeType = br.ReadString();//Read string of the bin file
                        }
                        if (shapeType != null)
                        {
                            if (shapeType == "Line")
                            {
                                pt1.X    = br.ReadInt32();//reads 4 bytes
                                pt1.Y    = br.ReadInt32();
                                pt2.X    = br.ReadInt32();
                                pt2.Y    = br.ReadInt32();
                                penWidth = br.ReadInt32();
                                penColor = Color.FromArgb(br.ReadInt32());
                                Pen  myPen  = new Pen(penColor, penWidth);
                                Line myLine = new Line(pt1, pt2, myPen);
                                shapeList.Add(myLine);
                            }
                            else if (shapeType == "Rect")
                            {
                                pt1.X    = br.ReadInt32();
                                pt1.Y    = br.ReadInt32();
                                pt2.X    = br.ReadInt32();
                                pt2.Y    = br.ReadInt32();
                                penWidth = br.ReadInt32();
                                penColor = Color.FromArgb(br.ReadInt32());
                                Pen  myPen  = new Pen(penColor, penWidth);
                                Rect myRect = new Rect(pt1, pt2, myPen);
                                shapeList.Add(myRect);
                            }
                            else if (shapeType == "FreeLine")
                            {
                                penWidth = br.ReadInt32();
                                penColor = Color.FromArgb(br.ReadInt32());
                                Pen      myPen      = new Pen(penColor, penWidth);
                                FreeLine myFreeLine = new FreeLine(myPen);
                                int      count      = br.ReadInt32();
                                int      i          = 0;
                                while (i < count) // DONE//
                                {
                                    pt1.X = br.ReadInt32();
                                    pt1.Y = br.ReadInt32();
                                    myFreeLine.FreeList.Add(pt1);
                                    i++;
                                }
                                shapeList.Add(myFreeLine);
                            }
                        }
                        else
                        {
                            br.Close();
                            Invalidate();
                            break;
                        }
                    } //close while
                    savedFileExists = true;
                }     //close elseIf
            }
            catch (IOException)
            {
                MessageBox.Show("Error reading from file",
                                "File Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 4
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result;

            using (OpenFileDialog fileChooser = new OpenFileDialog())
            {
                fileChooser.Filter          = "Text Files (*.txt) |*.txt| Binary Files (*.bin) |*.bin| Serialized Object Files (*.ser) |*.ser| XML Files (*.xml) |*.xml";
                fileChooser.DefaultExt      = "txt";
                fileChooser.CheckFileExists = false; //Don't need to verify file name after save.
                result      = fileChooser.ShowDialog();
                currentFile = fileChooser.FileName;
                extension   = Path.GetExtension(fileChooser.FileName);
            }
            try
            {
                if (result == DialogResult.OK && extension == ".txt")
                {
                    shapeList.Clear();
                    Invalidate();
                    FileStream   fs = new FileStream(currentFile, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs);


                    // Reading of Text Data
                    while (true)
                    {
                        string dataLine = sr.ReadLine();
                        if (dataLine != null)
                        {
                            dataArray = dataLine.Split(',', ';', ':'); // Splits at every , ; :

                            if (dataArray[0] == "Line")
                            {
                                //Line tmpLine = new Line(this);
                                Line tmpLine = new Line();
                                tmpLine.readText(sr);
                            }
                            else if (dataArray[0] == "Rectangle")
                            {
                                Rect tmpRect = new Rect();
                                tmpRect.readText(sr);
                            }
                            else if (dataArray[0] == "FreeLine")
                            {
                                FreeLine tmpFreeLine = new FreeLine();
                                tmpFreeLine.readText(sr);
                            }
                        }
                        else
                        {
                            sr.Close();
                            Invalidate();
                            break;
                        }
                    }
                    savedFileExists = true;
                }//end text file
                else if (result == DialogResult.OK && extension == ".bin")
                {
                    string shapeType;
                    shapeList.Clear();
                    Invalidate();
                    FileStream   fs = new FileStream(currentFile, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);

                    // Reading Binary Data
                    while (true)
                    {
                        shapeType = null;
                        if (br.BaseStream.Position != br.BaseStream.Length)
                        {
                            shapeType = br.ReadString();//Read string of the bin file
                        }
                        if (shapeType != null)
                        {
                            if (shapeType == "Line")
                            {
                                Line tmpLine = new Line();
                                tmpLine.readBinary(br);
                            }
                            else if (shapeType == "Rect")
                            {
                                Rect tmpRect = new Rect();
                                tmpRect.readBinary(br);
                            }
                            else if (shapeType == "FreeLine")
                            {
                                FreeLine tmpFreeLine = new FreeLine();
                                tmpFreeLine.readBinary(br);
                            }
                        }
                        else
                        {
                            br.Close();
                            Invalidate();
                            break;
                        }
                    } //close while
                    savedFileExists = true;
                }     //close elseIf

                //////////////////WIP/////////////////////////////////
                else if (result == DialogResult.OK && extension == ".xml")
                {
                    shapeList.Clear();
                    Invalidate();

                    XDocument menu = XDocument.Load(currentFile);
                    //
                    //
                    foreach (XElement xe in menu.Root.Elements())
                    {
                        foreach (XAttribute fe in xe.Attributes())
                        {
                            if (fe.Value == "Line")
                            {
                                // MessageBox.Show("sfsefse");
                                pt1.X    = int.Parse(xe.Element("StartPoint_X").Value);
                                pt1.Y    = int.Parse(xe.Element("StartPoint_Y").Value);
                                pt2.X    = int.Parse(xe.Element("EndPoint_X").Value);
                                pt2.Y    = int.Parse(xe.Element("EndPoint_Y").Value);
                                penWidth = int.Parse(xe.Element("PenWidth").Value);
                                penColor = Color.FromArgb(int.Parse(xe.Element("PenColor").Value, System.Globalization.NumberStyles.AllowHexSpecifier));
                                Pen  myPen  = new Pen(penColor, penWidth);
                                Line myLine = new Line(pt1, pt2, myPen);
                                shapeList.Add(myLine);
                            }
                            else if (fe.Value == "Rectangle")
                            {
                                pt1.X    = int.Parse(xe.Element("StartPoint_X").Value);
                                pt1.Y    = int.Parse(xe.Element("StartPoint_Y").Value);
                                pt2.X    = int.Parse(xe.Element("EndPoint_X").Value);
                                pt2.Y    = int.Parse(xe.Element("EndPoint_Y").Value);
                                penWidth = int.Parse(xe.Element("PenWidth").Value);
                                penColor = Color.FromArgb(int.Parse(xe.Element("PenColor").Value, System.Globalization.NumberStyles.AllowHexSpecifier));
                                Pen  myPen  = new Pen(penColor, penWidth);
                                Rect myRect = new Rect(pt1, pt2, myPen);
                                shapeList.Add(myRect);
                            }
                            else if (fe.Value == "FreeLine")
                            {
                                penWidth = int.Parse(xe.Attribute("PenWidth").Value);
                                penColor = Color.FromArgb(int.Parse(xe.Attribute("PenColor").Value, System.Globalization.NumberStyles.AllowHexSpecifier));
                                Pen      myPen      = new Pen(penColor, penWidth);
                                FreeLine myFreeLine = new FreeLine(myPen);
                                foreach (XElement point in xe.Descendants())
                                {
                                    if (point.Name == "Point") // if <Point> element
                                    {
                                        pt1.X = int.Parse(point.Attribute("X").Value);
                                        pt1.Y = int.Parse(point.Attribute("Y").Value);
                                        myFreeLine.FreeList.Add(pt1);
                                    }
                                }
                                shapeList.Add(myFreeLine);
                            }
                        }
                    }//end root element
                    savedFileExists = true;
                    Invalidate();
                }

                else if (result == DialogResult.OK && extension == ".ser")//Deserialization
                {
                    shapeList.Clear();
                    Invalidate();
                    FileStream      fs = new FileStream(currentFile, FileMode.Open, FileAccess.Read);
                    BinaryFormatter bf = new BinaryFormatter();
                    shapeList.Clear();
                    this.shapeList = (List <Shape>)bf.Deserialize(fs);

                    fs.Close();
                    Invalidate();
                }
            }//end try
            catch (IOException)
            {
                MessageBox.Show("Error reading from file",
                                "File Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 5
0
        private void ts_click(object sender, EventArgs e)
        {
            Console.WriteLine("  " + sender.ToString());
            toolStripTextBox1.Text = sender.ToString();
            SqlDataReader reader = null;
            SqlCommand    com    = new SqlCommand();

            com.Connection = myConnection;
            Graphics g = this.CreateGraphics();

            g.Clear(Form1.DefaultBackColor); //clear the form
            shapeList.Clear();
            com.CommandText = "select l.* from Line l " +
                              "join Drawing d on l.pictureid=d.drawing_id where drawing_name='" + sender.ToString() + "'";
            Console.WriteLine(com.CommandText);


            try {
                reader = com.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine("readine line ");
                    Shape s1 = new Line();
                    s1.getData(reader);
                    s1.Draw(g);
                    Console.WriteLine(s1.ToString());
                    shapeList.Add(s1);
                }
                reader.Close();
                SqlCommand com1 = new SqlCommand();
                com1.Connection  = myConnection;
                com1.CommandText = "select r.* from Rectangle r " +
                                   "join Drawing d on r.pictureid=d.drawing_id where drawing_name='" + sender.ToString() + "'";
                reader = com1.ExecuteReader();
                Console.WriteLine(com1.CommandText);
                while (reader.Read())
                {
                    Shape s2 = new Rect();
                    s2.getData(reader);
                    s2.Draw(g);
                    Console.WriteLine("Rectangle data " + s2.ToString());
                    shapeList.Add(s2);
                }
                reader.Close();

                SqlCommand comm2 = new SqlCommand();

                comm2.CommandText = "select * from Text where textid in (select textid from draw_text where drawid= (select drawing_id from drawing where drawing_name='" + sender.ToString() + "'))";
                Console.WriteLine("query to get the text data" + comm2.CommandText);
                comm2.Connection = myConnection;
                reader           = comm2.ExecuteReader();
                while (reader.Read())
                {
                    Shape t = new Text();
                    t.getData(reader);
                    t.Draw(g);
                    shapeList.Add(t);
                    Console.WriteLine("Text to be added to the paint screen " + t.ToString());
                }
                reader.Close();

                String         q             = "select * from freeline where freelineid in (select freelineid from draw_freeline where drawid= (select drawing_id from drawing where drawing_name='" + sender.ToString() + "'))";
                SqlDataAdapter adapter       = new SqlDataAdapter(q, myConnection);
                IDbCommand     selectCommand = myConnection.CreateCommand();
                DataTable      dt            = new DataTable();
                DataSet        selectResults = new DataSet();
                adapter.Fill(selectResults); // get dataset
                selectCommand.Dispose();
                foreach (DataRow row in selectResults.Tables[0].Rows)
                {
                    FreeLine f = new FreeLine();
                    f.getData(row, myConnection);
                    f.Draw(g);
                    shapeList.Add(f);
                    Console.WriteLine("freeline to be added to the paint screen " + f.ToString());
                }
                selectResults.Clear();
                dataModified = false;

                /* while (reader1.Read())
                 * {
                 *   FreeLine f = new FreeLine();
                 *   f.getData(reader1, myConnection);
                 *   f.Draw(g);
                 *   shapeList.Add(f);
                 *   Console.WriteLine("freeline to be added to the paint screen " + f.ToString());
                 * }*/
            }



            catch (Exception ec)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                reader.Close();
                Console.WriteLine("Error " + ec.Message);
            }
        }