远程桌面SerializationException

时间:2016-04-18 15:17:21

标签: c# wpf serialization

我正在尝试创建这个小应用程序。

代码服务器:

1  using System;
2  using System.ComponentModel;
3  using System.Drawing;
4  using System.Drawing.Imaging;
5  using System.Net;
6  using System.Net.Sockets;
7  using System.Runtime.Serialization.Formatters.Binary;
8  using System.Threading;
9  using System.Windows;
10 using System.Windows.Interop;
11 using System.Windows.Media.Imaging;
12
13 namespace ServerConn
14 {
15    public partial class MyWinShare : Window
16    {
17
18        private readonly int port;
19        private TcpClient client;
20        private TcpListener server;
21        private NetworkStream mainStream;
23
24        private readonly Thread Listening;
25        private readonly Thread GetImage;
26        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
27        public static extern bool DeleteObject(IntPtr hObject);
28
29        public MyWinShare(int Port)
30        {
31            port = Port;
32            client = new TcpClient();
33            Listening = new Thread(StartListening);
34            GetImage = new Thread(ReceiveImage);
35
36            InitializeComponent();
37        }
38
39        private void StartListening()
40        {
41            while (!client.Connected)
42            {
43                server.Start();
44                client = server.AcceptTcpClient();
45
46            }
47            GetImage.Start();
48        }
49
50        private void StopListening()
51        {
52            server.Stop();
53            client = null;
54            if (Listening.IsAlive) Listening.Abort();
55            if (GetImage.IsAlive) GetImage.Abort();
56        }
57
58        private void ReceiveImage()
59        {
60            BinaryFormatter binformatter = new BinaryFormatter();
61            while (client.Connected)
62            {
63                mainStream = client.GetStream();
64                myPicture.Source = (BitmapSource)binformatter.Deserialize(mainStream);
65                
66            }
67        }
68
69        protected override void OnInitialized(EventArgs e)
70        {
71            base.OnInitialized(e);
72            server = new TcpListener(IPAddress.Any, port);
73            Listening.Start();
74        }
75
76        protected override void OnClosing(CancelEventArgs e)
77        {
78            base.OnClosing(e);
79            StopListening();
80        }
81    }
82}

代码客户端:

1 using System;
2 using System.Drawing;
3 using System.Drawing.Imaging;
4 using System.Net.Sockets;
5 using System.Runtime.Serialization;
6 using System.Runtime.Serialization.Formatters.Binary;
7 using System.Windows;
8 using System.Windows.Interop;
9 using System.Windows.Media.Imaging;
10
11
12
13 namespace MyClientConn
14 {
15
16  public partial class MainWindow : Window
17  {
18      private readonly TcpClient client = new TcpClient();
19      private NetworkStream mainStream;
20      private int portNumber;
21      [System.Runtime.InteropServices.DllImport("gdi32.dll")]
22      public static extern bool DeleteObject(IntPtr hObject);
23      System.Windows.Threading.DispatcherTimer myDispatcherTimer;
24
25      private static BitmapSource GrabDesktop()
26      {
27          using (var screenBmp = new Bitmap(  (int)SystemParameters.PrimaryScreenWidth,
28                                              (int)SystemParameters.PrimaryScreenHeight,
29                                              System.Drawing.Imaging.PixelFormat.Format32bppArgb))   {
30
31              using (var bmpGraphics = Graphics.FromImage(screenBmp))
32              {
33                  bmpGraphics.CopyFromScreen(0, 0, 0, 0, screenBmp.Size);
34                  return Imaging.CreateBitmapSourceFromHBitmap(
35                      screenBmp.GetHbitmap(),
36                      IntPtr.Zero,
37                      Int32Rect.Empty,
38                      BitmapSizeOptions.FromEmptyOptions());
39              }
40              
41          }
42      }
43      
44
45      private void sendDesktopImage()
46      {
47        
48          using (mainStream = client.GetStream())
49          {
50                  BinaryFormatter binFormatter = new BinaryFormatter();
51                  binFormatter.Serialize(mainStream, GrabDesktop());
52          }
53      }
54
55      public MainWindow()
56      {
57          InitializeComponent();
58      }
59
60      private void bt_Conn(object sender, RoutedEventArgs e)
61      {
62          portNumber = int.Parse(txtPort.Text);
64          try
65          {
66              client.Connect(txtIP.Text, portNumber);
67              MessageBox.Show("Yes :)");
68          }
69          catch (Exception)
70          {
71              MessageBox.Show("Error :(");
72          }
73      }
74
75      private void bt_ShareConn(object sender, RoutedEventArgs e)
76      {
77          if (bt_ShareConn.Content.ToString().StartsWith("Share"))
78          {
79              System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
80              myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1); // 100 Milliseconds 
81              myDispatcherTimer.Tick += myDdispatcherTimer_Tick;
82
83              myDispatcherTimer.Start();
84              bt_ShareConn.Content = "Stop Share";
85          }
86          else
87          {
88              myDispatcherTimer.Stop();
89              bt_ShareConn.Content = "Share Desktop";
90          }
91      }
92
93      private void myDdispatcherTimer_Tick(object sender, EventArgs e)
94      {
95          sendDesktopImage();
96
97      }
98
99      private void bt_Close(object sender, RoutedEventArgs e)
100     {
101         Environment.Exit(0);
102     }
103 }
104 }

这里崩溃了:

  

binFormatter.Serialize(mainStream,GrabDesktop());

     

1 mscorlib.dll中发生System.Runtime.Serialization.SerializationException类型的未处理异常附加信息:O tipo'System.Windows.Interop.InteropBitmap'na assemblagem'IntroductionCore,Version = 4.0.0.0,Culture = neutral。 ......

我做错了什么?

0 个答案:

没有答案