我被困在如何拍摄我的Windows Phone 7.5的屏幕截图并通过TCP发送它。我没有做套接字程序和I / O的经验,我正在通过互联网上的教程做我能做的事情。这就是我所做的。
从下面的代码中我发现我如何通过TCP编码发送writeableBitMap作为在WP7.5背景中定期运行的Jpeg,从而桌面上的程序将其作为jpeg图像接收,以便可以显示创建一个Windows桌面流媒体效果。
我的windows phone 7.5应用程序的主页,使用的是我从教程中创建的用于处理套接字连接的库。
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();
}
}
}
套接字库
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<SocketAsyncEventArgs>(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<SocketAsyncEventArgs>(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();
}
}
}
}