Exemplo n.º 1
0
        private void paintcanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            TextMessage drawShapeMsg = new TextMessage();

            mouseLeftButtonPress     = false;
            drawShapeMsg.enumCommand = Command.Draw;
            drawShapeMsg.strName     = uname_textbox.Text;
            drawShapeMsg.strRoom     = room;

            switch (tool)
            {
            case Command.Pen:
            {
                try
                {
                    Point[]      points             = new Point[pointList.Count];
                    List <Point> distinctPointsList = new List <Point>();
                    //MessageBox.Show("Actual Sise of pen list = "+pointList.Count.ToString());
                    distinctPointsList.AddRange(pointList.Distinct());
                    //MessageBox.Show("Distinct size of pen list = " + distinctPointsList.Count.ToString());
                    int i = 0;
                    drawShapeMsg.strMessage = Command.Pen.ToString() + "?";
                    foreach (Point p in distinctPointsList)
                    {
                        points[i++] = p;
                    }
                    for (int x = 0; x < points.Length; x++)
                    {
                        drawShapeMsg.strMessage += points[x].ToString() + "*";
                    }
                    drawShapeMsg.strMessage += "?" + borderThiknes.ToString() + "?" + selectedColor.ToString() + "?";
                }
                catch (Exception ex)
                {
                    MessageBox.Show("in 'Command.Pen' Switch case" + ex.Message);
                }

                //recieveDrwingBytes(drawShapeMsg.ToByte());
                break;
            }

            case Command.Line:
            {
                drawShapeMsg.strMessage = Command.Line.ToString() + "?" + fPoint.ToString() + "?" + sPoint.ToString() + "?" + borderThiknes.ToString() + "?" + selectedColor.ToString() + "?";
                //recieveDrwingBytes(drawShapeMsg.ToByte());
                break;
            }

            case Command.Rectangle:
            {
                drawShapeMsg.strMessage = Command.Rectangle.ToString() + "?" + fPoint.ToString() + "?" + sPoint.ToString() + "?" + borderThiknes.ToString() + "?" + selectedColor.ToString() + "?" + SelectedfillColor.ToString() + "?";
                //recieveDrwingBytes(drawShapeMsg.ToByte());
                break;
            }

            case Command.Circle:
            {
                drawShapeMsg.strMessage = Command.Circle.ToString() + "?" + fPoint.ToString() + "?" + sPoint.ToString() + "?" + borderThiknes.ToString() + "?" + selectedColor.ToString() + "?" + SelectedfillColor.ToString() + "?";
                //recieveDrwingBytes(drawShapeMsg.ToByte());
                break;
            }

            case Command.Triangle:
            {
                //MessageBox.Show("Fisrt : " + fPoint.ToString() + "Second : " + sPoint.ToString());
                drawShapeMsg.strMessage = Command.Triangle.ToString() + "?" + fPoint.ToString() + "?" + sPoint.ToString() + "?" + borderThiknes.ToString() + "?" + selectedColor.ToString() + "?" + SelectedfillColor.ToString() + "?";
                //recieveDrwingBytes(drawShapeMsg.ToByte());

                //drawTriangle(fPoint, sPoint, borderThiknes, selectedColor, testCanvas, SelectedfillColor);
                break;
            }

            case Command.Capture:
            {
                try
                {
                    DrawingShapes cropImg = new DrawingShapes();
                    newSelectionTool.StrokeThickness = 0;
                    Rectangle locRect = new Rectangle();
                    locRect.Width           = newSelectionTool.Width;
                    locRect.Height          = newSelectionTool.Height;
                    locRect.StrokeThickness = 0;
                    Canvas.SetLeft(locRect, 0);
                    Canvas.SetTop(locRect, 0);

                    WriteableBitmap image       = new WriteableBitmap(paintcanvas, null);
                    WriteableBitmap cropedImage = cropImg.CropImage(image, (int)fPoint.X, (int)fPoint.Y, (int)locRect.Width, (int)locRect.Height);
                    ib = new ImageBrush();
                    MemoryStream stream    = new MemoryStream();
                    BitmapImage  bitmapImg = new BitmapImage();

                    EncodeJpeg(cropedImage, stream);
                    bitmapImg.SetSource(stream);
                    ib.ImageSource = bitmapImg;

                    locRect.Fill          = ib;
                    newSelectionTool.Fill = ib;

                    //testCanvas.Children.Add(locRect);
                    newSelectionTool.StrokeThickness = 0.5;
                    shapeCouter++;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + "\n\n in mouse up event of pantcanvs");
                }

                break;
            }

            case Command.Eraser:
            {
                Point[] points = new Point[pointList.Count];
                int     i      = 0;
                drawShapeMsg.strMessage = Command.Eraser.ToString() + "?";
                foreach (Point p in pointList)
                {
                    points[i++] = p;
                }
                for (int x = 0; x < points.Length; x++)
                {
                    drawShapeMsg.strMessage += points[x].ToString() + "*";
                }
                drawShapeMsg.strMessage += "?" + 5.ToString() + "?" + Colors.White.ToString() + "?";
                break;
            }

            case Command.Highlighter:
            {
                Point[] points = new Point[pointList.Count];
                int     i      = 0;
                drawShapeMsg.strMessage = Command.Highlighter.ToString() + "?";
                foreach (Point p in pointList)
                {
                    points[i++] = p;
                }
                for (int x = 0; x < points.Length; x++)
                {
                    drawShapeMsg.strMessage += points[x].ToString() + "*";
                }
                drawShapeMsg.strMessage += "?" + 10.ToString() + "?" + Colors.Red.ToString() + "?";
                break;
            }

            case Command.Move:
            {
                break;
            }

            default:
            {
                break;
            }
            }

            try
            {
                //myStr = drawShapeMsg.strMessage;
                //MessageBox.Show("Size of mesage : " + myStr.Length.ToString());
                clientobj.Send(drawShapeMsg);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "in the drawShape sending exeption");
            }
            //List<ArraySegment<byte>> list = new List<ArraySegment<byte>>();
            //sendEventArgs.BufferList = list;
            //socket.SendAsync(sendEventArgs);
        }
Exemplo n.º 2
0
        public void recieveDrwingBytes(byte[] drawingBytes, Canvas paintcanvas)
        {
            TextMessage recDrawDataMsg = new TextMessage(drawingBytes);

            if (recDrawDataMsg.enumCommand == Command.Draw)
            {
                string[] drawingInfo  = recDrawDataMsg.strMessage.Split('?');
                string   selectedTool = drawingInfo[0];
                switch (selectedTool)
                {
                case "Pen":
                {
                    try
                    {
                        List <Point> pointList = new List <Point>();
                        Point[]      pointsAsArg;
                        string[]     pointInStr = drawingInfo[1].Split('*');

                        for (int i = 0; i < pointInStr.Length - 1; i++)
                        {
                            string[] point = pointInStr[i].Split(',');
                            pointList.Add(new Point(int.Parse(point[0]), int.Parse(point[1])));
                        }

                        pointsAsArg = new Point[pointList.Count];
                        int x = 0;
                        foreach (Point p in pointList)
                        {
                            pointsAsArg[x++] = p;
                        }
                        drawPenLine(pointsAsArg, int.Parse(drawingInfo[2]), colorCodeConverter(drawingInfo[3]), paintcanvas);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + " in Receive function.");
                    }
                    break;
                }

                case "Highlighter":
                {
                    try
                    {
                        List <Point> pointList = new List <Point>();
                        Point[]      pointsAsArg;
                        string[]     pointInStr = drawingInfo[1].Split('*');

                        for (int i = 0; i < pointInStr.Length - 1; i++)
                        {
                            string[] point = pointInStr[i].Split(',');
                            pointList.Add(new Point(int.Parse(point[0]), int.Parse(point[1])));
                        }

                        pointsAsArg = new Point[pointList.Count];
                        int x = 0;
                        foreach (Point p in pointList)
                        {
                            pointsAsArg[x++] = p;
                        }
                        drawPenLine(pointsAsArg, int.Parse(drawingInfo[2]), colorCodeConverter(drawingInfo[3], "4F"), paintcanvas);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + " in Receive function.");
                    }
                    break;
                }

                case "Line":
                {
                    try
                    {
                        string[] recDataStr  = recDrawDataMsg.strMessage.Split('?');
                        string[] firstPoint  = recDataStr[1].Split(',');
                        string[] secondPoint = recDataStr[2].Split(',');

                        drawLine(new Point(double.Parse(firstPoint[0]), double.Parse(firstPoint[1])),
                                 new Point(double.Parse(secondPoint[0]), double.Parse(secondPoint[1])),
                                 int.Parse(recDataStr[3]),
                                 colorCodeConverter(recDataStr[4], null),
                                 paintcanvas);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + " in Receive function.");
                    }
                    break;
                }

                case "Rectangle":
                {
                    try
                    {
                        string[] recDataStr  = recDrawDataMsg.strMessage.Split('?');
                        string[] firstPoint  = recDataStr[1].Split(',');
                        string[] secondPoint = recDataStr[2].Split(',');

                        drawRectangle(new Point(double.Parse(firstPoint[0]), double.Parse(firstPoint[1])),
                                      new Point(double.Parse(secondPoint[0]), double.Parse(secondPoint[1])),
                                      int.Parse(recDataStr[3]), colorCodeConverter(recDataStr[4]), paintcanvas,
                                      colorCodeConverter(recDataStr[5]));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + " in Receive function.");
                    }
                    break;
                }

                case "Triangle":
                {
                    try
                    {
                        string[] recDataStr  = recDrawDataMsg.strMessage.Split('?');
                        string[] firstPoint  = recDataStr[1].Split(',');
                        string[] secondPoint = recDataStr[2].Split(',');

                        drawTriangle(new Point(double.Parse(firstPoint[0]), double.Parse(firstPoint[1])),
                                     new Point(double.Parse(secondPoint[0]), double.Parse(secondPoint[1])),
                                     int.Parse(recDataStr[3]), colorCodeConverter(recDataStr[4]), paintcanvas,
                                     colorCodeConverter(recDataStr[5]));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + " in Receive function.");
                    }
                    break;
                }

                case "Circle":
                {
                    try
                    {
                        string[] recDataStr  = recDrawDataMsg.strMessage.Split('?');
                        string[] firstPoint  = recDataStr[1].Split(',');
                        string[] secondPoint = recDataStr[2].Split(',');

                        drawCircle(new Point(double.Parse(firstPoint[0]), double.Parse(firstPoint[1])),
                                   new Point(double.Parse(secondPoint[0]), double.Parse(secondPoint[1])),
                                   int.Parse(recDataStr[3]), colorCodeConverter(recDataStr[4]), paintcanvas,
                                   colorCodeConverter(recDataStr[5]));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + " in Receive function.");
                    }
                    break;
                }

                case "Capture":
                {
                    break;
                }

                case "Eraser":
                {
                    try
                    {
                        List <Point> pointList = new List <Point>();
                        Point[]      pointsAsArg;
                        string[]     pointInStr = drawingInfo[1].Split('*');

                        for (int i = 0; i < pointInStr.Length - 1; i++)
                        {
                            string[] point = pointInStr[i].Split(',');
                            pointList.Add(new Point(int.Parse(point[0]), int.Parse(point[1])));
                        }

                        pointsAsArg = new Point[pointList.Count];
                        int x = 0;
                        foreach (Point p in pointList)
                        {
                            pointsAsArg[x++] = p;
                        }
                        drawPenLine(pointsAsArg, int.Parse(drawingInfo[2]), colorCodeConverter(drawingInfo[3]), paintcanvas);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + " in Receive function.");
                    }
                    break;
                }

                case "Clear":
                {
                    paintcanvas.Children.Clear();
                    break;
                }
                }
            }
        }