title: Taking screenshot of windows phone  7.5 and sending over through TCP
body: <p>i am stuck on how to take a screenshot of my windows phone 7.5 and sending it over TCP. i have no experience in doing socket program and I/O and am doing what i can through tutorials over the internet. This is what i have done.</p>
  
  <p>From the codes below i am stuck in how i can send the writeableBitMap over TCP encoded as a Jpeg periodically running in the WP7.5 background, whereby a program on a desktop will receive it as a jpeg image so it can be displayed creating a windows phone to desktop streaming effect.</p>
  
  <p>my mainPage of my windows phone 7.5 application using a library i have created from a tutorial for taking care of the sockets connection.</p>
  
  <pre><code>    using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media.Imaging;
  using System.Windows.Media.Animation;
  using System.Windows.Shapes;
  using Microsoft.Phone.Controls;
  using Microsoft.Phone;
  using System.Windows.Media;
  using System.IO;
  
  namespace helloworld
  {
      public partial class MainPage : PhoneApplicationPage
      {
          // Constructor
          SocketLibrary.socketLib sl = new SocketLibrary.socketLib();
          private string hostIP = "127.0.0.1";
          public MainPage()
          {
              InitializeComponent();
          }
  
          private void btnConnect_Click(object sender, RoutedEventArgs e)
          {
              bool retVal;
  
              retVal = sl.EstablishTCPConnection(hostIP);
  
              WriteableBitmap bmpCurrentScreenImage = new WriteableBitmap((int)this.ActualWidth, (int)this.ActualHeight);
  
              var ms = new MemoryStream();
              // Send the picture.
              bmpCurrentScreenImage.SaveJpeg(ms, bmpCurrentScreenImage.PixelWidth, bmpCurrentScreenImage.PixelHeight, 0, 90);
              ms.Seek(0, SeekOrigin.Begin);
              retVal = sl.Send(ms);
  
              sl.CloseSocket();
          }
  
  
      }
  }
  </code></pre>
  
  <p>the socket library</p>
  
  <pre><code>   namespace SocketLibrary
  {
      public class socketLib
      {
          Socket s = null;
          static ManualResetEvent done = new ManualResetEvent(false);
          private Int16 portNo = 3334;
  
          public socketLib()
          {
  
  
          }
          public bool EstablishTCPConnection(string host)
          {
              s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
              ProtocolType.Tcp);
              SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
              socketEventArg.RemoteEndPoint = new DnsEndPoint(host, portNo);
              socketEventArg.Completed += new
              EventHandler&lt;SocketAsyncEventArgs&gt;(delegate(object o, SocketAsyncEventArgs e)
              {
                  done.Set();
              });
              done.Reset();
              s.ConnectAsync(socketEventArg);
              return done.WaitOne(10000);
          }
  
          public bool Send(MemoryStream data)
          {
              byte[] msData = data.ToArray();
              if (s != null)
              {
                  SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
                  socketEventArg.RemoteEndPoint = s.RemoteEndPoint;
                  socketEventArg.UserToken = null;
  
                  socketEventArg.Completed += new
                  EventHandler&lt;SocketAsyncEventArgs&gt;(delegate(object o, SocketAsyncEventArgs e)
                  {
                      done.Set();
                  });
  
                  socketEventArg.SetBuffer(msData, 0, msData.Length);
                  done.Reset();
                  s.SendAsync(socketEventArg);
                  return done.WaitOne(10000);
              }
              return false;
          }
  
          public void CloseSocket()
          {
              if (s != null)
              {
                  s.Close();
              }
          }
      }
  
  
  }
  </code></pre>