private void btnDeleteShape_Click(object sender, EventArgs e)
        {
            if (comboBoxCustomShape.SelectedIndex != -1)
            {
                ListOfCustomShapes.RemoveAt(comboBoxCustomShape.SelectedIndex);
                comboBoxCustomShape.Items.RemoveAt(comboBoxCustomShape.SelectedIndex);
                JsonSerializer jsonSerializer = new JsonSerializer();
                jsonSerializer.TypeNameHandling = TypeNameHandling.Objects;

                using (StreamWriter writer = new StreamWriter(CustomShapesFile))
                {
                    using (JsonWriter jsonWriter = new JsonTextWriter(writer))
                    {
                        try
                        {
                            jsonSerializer.Serialize(jsonWriter, ListOfCustomShapes);
                        }
                        catch (Exception exception)
                        {
                            MessageBox.Show(exception.Message, "Serialization Error");
                        }
                    }
                }
                user        = false;
                customShape = null;
            }
//            ListOfCustomShapes.Remove(comboBoxCustomShape.SelectedIndex)
        }
        /*
         * private void btnPlugin_Click(object sender, EventArgs e)
         * {
         *  try
         *  {
         *      if (!plugin)
         *      {
         *          a = Assembly.Load(PluginFile);
         *          /*
         *          object[] construct = new object[2];
         *          construct[0] = Current;
         *          construct[1] = penWidth;
         *          //Object o = a.CreateInstance("Pencil",false,0,null, construct,null,null);
         *          Type t = a.GetType(PluginFile + ".Pencil");
         *          Object o = Activator.CreateInstance(t, construct);
         */
        //            this.figure = o as Shape;

        /*
         * MethodInfo mi = t.GetMethod("Draw");
         * object[] parameters = new object[7];
         * parameters[0] = Bmp;
         * parameters[1] = 100;
         * parameters[2] = 200;
         * parameters[3] = 200;
         * parameters[4] = 200;
         * parameters[5] = new Point(10, 100);
         * parameters[6] = new Point(200, 200);
         * this.figure = (Shape)o;
         * Bmp = (Bitmap)mi.Invoke(o, parameters);
         * //            Type tmp = o.GetType();
         * ShapePictureBox.Image = Bmp;
         */
        //ListOfShapes.Add(o as Shape);//Cast Exception Base-class to .dll

        /*
         * plugin = true;
         * //                    btnPlugin.Text = "JustLine";
         * btnPlugin.Text = "ClosedCurve";
         *
         * }
         * else
         * {
         * object[] construct = new object[2];
         * construct[0] = Current;
         * construct[1] = penWidth;
         * //Object o = a.CreateInstance("Pencil",false,0,null, construct,null,null);
         * Type t = a.GetType(PluginFile + ".ClosedCurve");
         * Object o = Activator.CreateInstance(t, construct);
         * this.figure = o as Shape;
         * }
         * }
         * catch (Exception exception)
         * {
         * MessageBox.Show(exception.Message, "Loading plugin error with message");
         * }
         *
         * }
         */

        private void btnShape_Click(object sender, EventArgs e)
        {
            customShape = null;
            comboBoxCustomShape.SelectedIndex = -1;
            user = false;
            ((Button)ShapeSender).Text = ((Button)sender).Name;
            this.figure = FactoriesList[((Button)ShapeSender).Text].Create(Current, penWidth);
        }
 private void btnCustomShape_Click(object sender, EventArgs e)
 {
     if (comboBoxCustomShape.SelectedIndex != -1)
     {
         customShape = (CustomShape)ListOfCustomShapes.ElementAt <Shape>(comboBoxCustomShape.SelectedIndex);
         //            string str = comboBoxCustomShape.SelectedItem.ToString();
         //            ((Button)ShapeSender).Text = ((Button)sender).Name;
         user = true;
     }
 }
        private void btnCreateShape_Click(object sender, EventArgs e)
        {
            this.figure = null;
            if (ListOfShapes.Count != 0)
            {
                if (textBoxCustomShape.Text.Trim(' ') != "")
                {
                    try
                    {
                        foreach (Shape shp in ListOfShapes)
                        {
                            ListOfUserShapes.Add(shp);
                        }

                        this.customShape = new CustomShape(textBoxCustomShape.Text.Trim(' '), new List <Shape>(ListOfUserShapes), ShapePictureBox.Width, ShapePictureBox.Height);
                        ListOfCustomShapes.Add(this.customShape);
                        textBoxCustomShape.Text = "";
                        comboBoxCustomShape.Items.Add(customShape.name);
                        ListOfUserShapes.Clear();
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message, "Creating Shape Error");
                    }
                }
                else
                {
                    MessageBox.Show("Enter any character!", "Creating Shape Error");
                }
            }
            else
            {
                MessageBox.Show("Draw any shape first!", "Creating Shape Error");
            }


//                press = false;


//                FindMinAndMaxPoints();

/*
 *              foreach (Shape usershape in ListOfUserShapes)
 *              {
 *                  usershape.pos1.X = usershape.pos1.X - MinP.X;
 *                  usershape.pos1.Y = usershape.pos1.Y - MinP.Y;
 *                  usershape.x = usershape.pos1.X;
 *                  usershape.y = usershape.pos1.Y;
 *                  usershape.w = usershape.w - MaxP.X;
 *                  usershape.h = usershape.h - MaxP.Y;
 *              }
 */
        }
        private void ShapePictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (figure != null && !user)
            {
                Cursor.Current        = Cursors.Default;
                press                 = false;
                two                   = e.Location;
                Bmp                   = figure.Draw(Bmp, one, two);
                ShapePictureBox.Image = Bmp;
                figure.pos1           = one;
                figure.pos2           = two;

                /*
                 * figure.x = x;
                 * figure.y = y;
                 * figure.h = h;
                 * figure.w = w;
                 */
                figure.pWidth = penWidth;
                figure.clr    = Current;
                ListOfShapes.Add(this.figure);
                this.figure = FactoriesList[((Button)ShapeSender).Text].Create(Current, penWidth);
            }
            if (user && customShape != null)
            {
                Cursor.Current = Cursors.Default;
//                user = false;
                press = false;
                two   = e.Location;
                customShape.Draw(Bmp, one, two);


/*
 *              foreach (Shape figure in ListOfUserShapes)
 *              {
 *                  int newX1 = one.X + (two.X - one.X) * figure.pos1.X / ShapePictureBox.Width;//MaxP.X - MinP.X
 *                  int newY1 = one.Y + (two.Y - one.Y) * figure.pos1.Y / ShapePictureBox.Height;//MaxP.Y - MinP.Y
 *                  int newX2 = one.X + (two.X - one.X) * Math.Abs(figure.pos2.X - figure.pos1.X) / ShapePictureBox.Width;
 *                  int newY2 = one.Y + (two.Y - one.Y) * Math.Abs(figure.pos2.Y - figure.pos1.Y) / ShapePictureBox.Height;
 *                  /*
 *                  x = one.X + (figure.pos1.X - 0) * (two.X - one.X) / (ShapePictureBox.Width - 0);
 *                  y = one.Y + (figure.pos1.Y - 0) * (two.Y - one.Y) / (ShapePictureBox.Height - 0);
 *                  h = x + (figure.pos2.X - figure.pos1.X) * (two.X - one.X) / (ShapePictureBox.Width - 0);
 *                  w = y + (figure.pos2.Y - figure.pos1.Y) * (two.Y - one.Y) / (ShapePictureBox.Height - 0);
 */
//                    Bmp = figure.Draw(Bmp, one, two);
//                    ShapePictureBox.Image = Bmp;
//                }
                ShapePictureBox.Image = Bmp;
                int          i   = 0;
                List <Shape> tmp = new List <Shape>();
                foreach (Shape shp in customShape.components)
                {
                    tmp.Add(shp.Clone(customShape.newpoints[i][0], customShape.newpoints[i][1], shp.clr, shp.pWidth));
                    i++;
                }
                ListOfShapes.AddRange(tmp);

                /*
                 * List<Shape> tmp = new List<Shape>(customShape.components);
                 * foreach (Shape shp in tmp)
                 * {
                 *  shp.pos1 = customShape.newpoints[i][0];
                 *  shp.pos2 = customShape.newpoints[i][1];
                 *
                 *  ListOfShapes.Add(tmp);
                 *  i++;
                 * }
                 */
                /*
                 * foreach(Shape shp in customShape.components)
                 * {
                 *  shp.pos1 = customShape.newpoints[i][0];
                 *  shp.pos2 = customShape.newpoints[i][1];
                 *  i++;
                 *  ListOfShapes.Add(shp);
                 * }
                 */
                /*
                 * foreach(Shape shp in customShape.SerializeList)
                 * {
                 *  ListOfShapes.Add(shp);
                 * }
                 */
                /*
                 * foreach (Shape shp in customShape.SerializeList)
                 * {
                 *  ListOfShapes.Add(shp);
                 * }
                 */
                //                customShape = (CustomShape)ListOfCustomShapes.ElementAt<Shape>(comboBoxCustomShape.SelectedIndex);

                CustomShapeFactory csf = new CustomShapeFactory();
                customShape = (CustomShape)csf.Create(customShape.name, customShape.components, customShape.width, customShape.height);
            }
        }