我尝试使用WPF(客户端和服务器端WPF)从我的PC接收桌面屏幕截图 这是我的代码
private void ViewReceivedImage(byte[] buffer)
{
try
{
using (MemoryStream memoryStream = new MemoryStream(buffer))
{
BitmapImage imageSource = new BitmapImage();
imageSource.BeginInit();
imageSource.StreamSource = memoryStream;
imageSource.EndInit();
// Assign the Source property of your image
MyImage.Source = imageSource;
}
//MemoryStream ms = new MemoryStream(buffer);
//BitmapImage bi = new BitmapImage();
//bi.SetSource = ms;
//MyImage.Source = bi;
//ms.Close();
}
catch (Exception) { }
finally
{
StartReceiving();
}
}
上面的注释行是针对Windows手机应用程序的,我已经测试了它并且它适用于WP8(客户端),我在WPF客户端只有问题。
WPF服务器端和WP8客户端工作 WPF客户端无法正常工作但连接成功
此方法发送图像
void StartSending()
{
while (!stop)
try
{
System.Drawing.Image oldimage = scr.Get_Resized_Image(wToCompare, hToCompare, scr.GetDesktopBitmapBytes());
//Thread.Sleep(1);
System.Drawing.Image newimage = scr.Get_Resized_Image(wToCompare, hToCompare, scr.GetDesktopBitmapBytes());
byte[] buffer = scr.GetDesktop_ResizedBytes(wToSend, hToSend);
//float difference = scr.difference(newimage, oldimage);
//if (difference >= 1)
//{
SenderSocket.Send(buffer);
//}
}
catch (Exception) { }
}
和这个Get_Resized_Image
public Image Get_Resized_Image(int w, int h, byte[] image)
{
MemoryStream ms = new MemoryStream(image);
Image bt = Image.FromStream(ms);
try
{
Size sizing = new Size(w, h);
bt = new System.Drawing.Bitmap(bt, sizing);
}
catch (Exception) { }
return bt;
}
编辑 这是输出