Click here to Skip to main content
15,896,153 members
Articles / Programming Languages / C#

Silver Draw - A Silverlight Based Collaboration White Board with Drawing and Chat

Rate me:
Please Sign up or sign in to vote.
4.98/5 (37 votes)
2 Nov 2009CPOL9 min read 140.3K   3.1K   137  
Silver Draw shows how to use Silverlight and WCF Polling Duplex services to create realtime collaboration apps.
  • silverdraw.zip
    • Client
      • .svn
        • all-wcprops
        • entries
        • format
        • prop-base
        • props
        • text-base
          • App.xaml.cs.svn-base
          • App.xaml.svn-base
          • Page.xaml.cs.svn-base
          • Page.xaml.svn-base
          • ScreenObject.cs.svn-base
          • ServiceReferences.ClientConfig.svn-base
          • Silverdraw.Client.csproj.svn-base
          • Silverdraw.Client.csproj.user.svn-base
        • tmp
          • prop-base
          • props
          • text-base
      • App.xaml
      • App.xaml.cs
      • Bin
        • .svn
          • all-wcprops
          • entries
          • format
          • prop-base
          • props
          • text-base
          • tmp
            • prop-base
            • props
            • text-base
        • Debug
          • .svn
            • all-wcprops
            • entries
            • format
            • prop-base
              • SilverPaintGadget.dll.svn-base
              • SilverPaintGadget.pdb.svn-base
              • SilverPaintGadget.xap.svn-base
              • System.Json.dll.svn-base
              • System.Runtime.Serialization.Json.dll.svn-base
              • System.ServiceModel.PollingDuplex.dll.svn-base
            • props
            • text-base
              • AppManifest.xaml.svn-base
              • SilverPaintGadget.dll.svn-base
              • SilverPaintGadget.pdb.svn-base
              • SilverPaintGadget.xap.svn-base
              • System.Json.dll.svn-base
              • System.Json.xml.svn-base
              • System.Runtime.Serialization.Json.dll.svn-base
              • System.Runtime.Serialization.Json.xml.svn-base
              • System.ServiceModel.PollingDuplex.dll.svn-base
              • System.ServiceModel.PollingDuplex.xml.svn-base
              • TestPage.html.svn-base
            • tmp
              • prop-base
              • props
              • text-base
          • AppManifest.xaml
          • de
            • .svn
              • all-wcprops
              • entries
              • format
              • prop-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • props
              • text-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • tmp
                • prop-base
                • props
                • text-base
            • System.Json.resources.dll
            • System.Runtime.Serialization.Json.resources.dll
            • System.ServiceModel.PollingDuplex.resources.dll
            • System.Windows.Controls.resources.dll
          • es
            • .svn
              • all-wcprops
              • entries
              • format
              • prop-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • props
              • text-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • tmp
                • prop-base
                • props
                • text-base
            • System.Json.resources.dll
            • System.Runtime.Serialization.Json.resources.dll
            • System.ServiceModel.PollingDuplex.resources.dll
            • System.Windows.Controls.resources.dll
          • fr
            • .svn
              • all-wcprops
              • entries
              • format
              • prop-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • props
              • text-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • tmp
                • prop-base
                • props
                • text-base
            • System.Json.resources.dll
            • System.Runtime.Serialization.Json.resources.dll
            • System.ServiceModel.PollingDuplex.resources.dll
            • System.Windows.Controls.resources.dll
          • it
            • .svn
              • all-wcprops
              • entries
              • format
              • prop-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • props
              • text-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • tmp
                • prop-base
                • props
                • text-base
            • System.Json.resources.dll
            • System.Runtime.Serialization.Json.resources.dll
            • System.ServiceModel.PollingDuplex.resources.dll
            • System.Windows.Controls.resources.dll
          • ja
            • .svn
              • all-wcprops
              • entries
              • format
              • prop-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • props
              • text-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • tmp
                • prop-base
                • props
                • text-base
            • System.Json.resources.dll
            • System.Runtime.Serialization.Json.resources.dll
            • System.ServiceModel.PollingDuplex.resources.dll
            • System.Windows.Controls.resources.dll
          • ko
            • .svn
              • all-wcprops
              • entries
              • format
              • prop-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • props
              • text-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • tmp
                • prop-base
                • props
                • text-base
            • System.Json.resources.dll
            • System.Runtime.Serialization.Json.resources.dll
            • System.ServiceModel.PollingDuplex.resources.dll
            • System.Windows.Controls.resources.dll
          • Silverdraw.Client.dll
          • SilverPaintGadget.dll
          • SilverPaintGadget.xap
          • System.Json.dll
          • System.Json.xml
          • System.Runtime.Serialization.Json.dll
          • System.Runtime.Serialization.Json.xml
          • System.ServiceModel.PollingDuplex.dll
          • System.ServiceModel.PollingDuplex.xml
          • System.Windows.Controls.dll
          • System.Windows.Controls.xml
          • TestPage.html
          • zh-Hans
            • .svn
              • all-wcprops
              • entries
              • format
              • prop-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • props
              • text-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • tmp
                • prop-base
                • props
                • text-base
            • System.Json.resources.dll
            • System.Runtime.Serialization.Json.resources.dll
            • System.ServiceModel.PollingDuplex.resources.dll
            • System.Windows.Controls.resources.dll
          • zh-Hant
            • .svn
              • all-wcprops
              • entries
              • format
              • prop-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • props
              • text-base
                • System.Json.resources.dll.svn-base
                • System.Runtime.Serialization.Json.resources.dll.svn-base
                • System.ServiceModel.PollingDuplex.resources.dll.svn-base
              • tmp
                • prop-base
                • props
                • text-base
            • System.Json.resources.dll
            • System.Runtime.Serialization.Json.resources.dll
            • System.ServiceModel.PollingDuplex.resources.dll
            • System.Windows.Controls.resources.dll
      • ColorPicker
        • .svn
          • all-wcprops
          • entries
          • format
          • prop-base
          • props
          • text-base
            • ColorPicker.xaml.cs.svn-base
            • ColorPicker.xaml.svn-base
          • tmp
            • prop-base
            • props
            • text-base
        • ColorPicker.xaml
        • ColorPicker.xaml.cs
      • Helpers
      • Page.xaml
      • Page.xaml.cs
      • Properties
        • .svn
          • all-wcprops
          • entries
          • format
          • prop-base
          • props
          • text-base
            • AppManifest.xml.svn-base
            • AssemblyInfo.cs.svn-base
          • tmp
            • prop-base
            • props
            • text-base
        • AppManifest.xml
        • AssemblyInfo.cs
      • Resources
        • .svn
          • all-wcprops
          • entries
          • format
          • prop-base
            • brush.jpg.svn-base
            • circle.PNG.svn-base
            • pen.jpg.svn-base
            • pencil.jpg.svn-base
            • rect.PNG.svn-base
            • sticky.jpg.svn-base
          • props
          • text-base
            • brush.jpg.svn-base
            • circle.PNG.svn-base
            • pen.jpg.svn-base
            • pencil.jpg.svn-base
            • rect.PNG.svn-base
            • sticky.jpg.svn-base
          • tmp
            • prop-base
            • props
            • text-base
        • brush.jpg
        • circle.PNG
        • pen.jpg
        • pencil.jpg
        • rect.PNG
        • sticky.jpg
      • ScreenObject.cs
      • Service References
        • .svn
          • all-wcprops
          • entries
          • format
          • prop-base
          • props
          • text-base
          • tmp
            • prop-base
            • props
            • text-base
        • Proxy
          • .svn
            • all-wcprops
            • entries
            • format
            • prop-base
            • props
            • text-base
              • configuration.svcinfo.svn-base
              • configuration91.svcinfo.svn-base
              • DuplexDrawService.disco.svn-base
              • DuplexDrawService.wsdl.svn-base
              • DuplexDrawService.xsd.svn-base
              • DuplexDrawService1.xsd.svn-base
              • DuplexDrawService2.xsd.svn-base
              • Reference.cs.svn-base
              • Reference.svcmap.svn-base
            • tmp
              • prop-base
              • props
              • text-base
          • configuration.svcinfo
          • configuration91.svcinfo
          • DuplexDrawService.disco
          • DuplexDrawService.wsdl
          • DuplexDrawService.xsd
          • DuplexDrawService1.xsd
          • DuplexDrawService2.xsd
          • Reference.cs
          • Reference.svcmap
      • ServiceReferences.ClientConfig
      • Silverdraw.Client.csproj
      • Silverdraw.Client.csproj.user
    • Server
      • .svn
        • all-wcprops
        • dir-prop-base
        • entries
        • format
        • prop-base
        • props
        • text-base
          • ClientAccessPolicy.xml.svn-base
          • DrawData.cs.svn-base
          • DuplexDrawService.svc.cs.svn-base
          • DuplexDrawService.svc.svn-base
          • IDuplexDrawCallback.cs.svn-base
          • IDuplexDrawService.cs.svn-base
          • Silverdraw.html.svn-base
          • Silverdraw.Publish.xml.svn-base
          • Silverdraw.Server.csproj.svn-base
          • Silverdraw.Server.csproj.user.svn-base
          • Silverlight.js.svn-base
          • Web.config.svn-base
          • Web.Debug.config.svn-base
          • Web.Release.config.svn-base
        • tmp
          • prop-base
          • props
          • text-base
      • App_Data
        • .svn
          • all-wcprops
          • entries
          • format
          • prop-base
          • props
          • text-base
          • tmp
            • prop-base
            • props
            • text-base
      • bin
        • Silverdraw.Server.dll
        • System.Data.DataSetExtensions.dll
        • System.ServiceModel.PollingDuplex.dll
        • System.Web.Extensions.dll
      • ClientAccessPolicy.xml
      • ClientBin
        • SilverPaintGadget.xap
      • default.htm
      • DrawData.cs
      • DuplexDrawService.svc
      • DuplexDrawService.svc.cs
      • IDuplexDrawCallback.cs
      • IDuplexDrawService.cs
      • Properties
        • .svn
          • all-wcprops
          • entries
          • format
          • prop-base
          • props
          • text-base
            • AssemblyInfo.cs.svn-base
          • tmp
            • prop-base
            • props
            • text-base
        • AssemblyInfo.cs
      • Service References
        • .svn
          • all-wcprops
          • entries
          • format
          • prop-base
          • props
          • text-base
          • tmp
            • prop-base
            • props
            • text-base
      • Silverdraw.Publish.xml
      • Silverdraw.Server.csproj
      • Silverdraw.Server.csproj.user
      • Silverdraw.Server.Publish.xml
      • Silverlight.js
      • Web.config
      • Web.Debug.config
      • Web.Release.config
    • Silverdraw.sln
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;

namespace Silverdraw.Client
{
    public class CanvasHelper
    {


        /// <summary>
        /// Constructs a screen object from a shape
        /// </summary>
        /// <param name="shp"></param>
        /// <returns></returns>
        public ScreenObject Shape2Object(Shape shp)
        {
           
            if (shp != null)
            {
                var line = shp as Line;
                if (line != null)
                {
                    return
                        (new ScreenObject()
                        {
                            StrokeColor = (line.Stroke as SolidColorBrush).Color.ToString(),
                            X1 = line.X1,
                            X2 = line.X2,
                            Y1 = line.Y1,
                            Y2 = line.Y2,
                            Thickness = line.StrokeThickness,
                            Type=ScreenObjectType.Line
 
                        }
                    );
                }
                else
                {
                   var obj=
                        (new ScreenObject()
                        {
                            X1 = Canvas.GetLeft(shp),
                            Width = shp.Width,
                            Y1 = Canvas.GetTop(shp),
                            Height = shp.Height,
                            Thickness = shp.StrokeThickness
                        }
                    );

                   if (shp is Rectangle)
                       obj.Type = ScreenObjectType.Rectangle;
                   else if (shp is Ellipse)
                       obj.Type = ScreenObjectType.Ellipse;
                   try
                   {
                     obj.FillColor=(shp.Fill as SolidColorBrush).Color.ToString();
                     obj.StrokeColor = (shp.Stroke as SolidColorBrush).Color.ToString();
                   }
                   catch { }

                   return obj;
                }
            }
            else
                return null;
        }


        /// <summary>
        /// Constructs a screen object from a shape
        /// </summary>
        /// <param name="shp"></param>
        /// <returns></returns>
        public Shape Object2Shape(ScreenObject obj)
        {

            if (obj != null)
            {
                if (obj.Type == ScreenObjectType.Line)
                {
                    return new Line()
                    {
                        X1 = obj.X1,
                        X2 = obj.X2,
                        Y1 = obj.Y1,
                        Y2 = obj.Y2,
                        StrokeThickness = obj.Thickness,
                        Stroke = new SolidColorBrush(ColorHelper.ColorFromString(obj.StrokeColor))
                    };
                }

                else if (obj.Type==ScreenObjectType.Ellipse)
                {
                    var shp = new Ellipse();
                    CopyValues(obj, shp);
                    return shp;
                }

                else if (obj.Type == ScreenObjectType.Rectangle)
                {
                    var shp = new Rectangle();
                    CopyValues(obj, shp);
                    return shp;
                }
            }

            return null;
        }

        /// <summary>
        /// Copy the values to shape
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="shp"></param>
        private void CopyValues(ScreenObject obj, Shape shp)
        {
            Canvas.SetLeft(shp, obj.X1);
            Canvas.SetTop(shp, obj.Y1);
            shp.Width = obj.Width;
            shp.Height = obj.Height;
            shp.StrokeThickness = obj.Thickness;
            try
            {
                shp.Fill = new SolidColorBrush(ColorHelper.ColorFromString(obj.FillColor));
                shp.Stroke = new SolidColorBrush(ColorHelper.ColorFromString(obj.StrokeColor));
            }
            catch { }
            
        }


       
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Architect, Developer, Speaker | Wannabe GUT inventor & Data Scientist | Microsoft MVP in C#

Comments and Discussions