public static byte[] Cut_Screen() { int width = Screen.PrimaryScreen.Bounds.Width; int height = Screen.PrimaryScreen.Bounds.Height; Bitmap bmp = new Bitmap(width, height); Image myimage = bmp; Graphics g = Graphics.FromImage(myimage); g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)); //获取鼠标 CURSORINFO pci; pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(out pci); System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor); cur.Draw(g, new Rectangle(pci.ptScreenPos.x - 10, pci.ptScreenPos.y - 10, cur.Size.Width, cur.Size.Height)); Image cutImage = ChangeImageWithByte.GetThumbnail(myimage); //转换大小 byte[] imageByte = ChangeImageWithByte.ImgToByt(cutImage); return(imageByte); }
// 当客户端发送消息时执行该函数,接收Socket中的数据 public void ReceiveCallback(IAsyncResult ia) //接收回调 ReceiveContent为接收数据 { key = 0; StateObject _so = ia.AsyncState as StateObject; _socket = _so.theSocket; if (Pattern == "File sharing") { try { int n = _socket.EndReceive(ia); // 接收到的字节数 ReceiveContent = Encoding.UTF8.GetString(_so.Buffer, 0, n); Console.WriteLine(ReceiveContent, DateTime.Now.ToLongTimeString()); string[] type = ReceiveContent.Split('?'); if (type[0].Equals("size")) //获取数据大小 { Console.WriteLine(type[0] + "+" + type[1], DateTime.Now.ToLongTimeString()); key = 1; string findContent_Size = cmd.Exec(type[1]); if (findContent_Size.Equals("HQNR")) // 如果数据为Byte[] { int i = cmd.Get_Byte().Length; SendCommand(i); } else if (findContent_Size.Equals("PMTS")) { SendCommand(cmd.Get_Byte()); } else { byte[] buffer = System.Text.Encoding.UTF8.GetBytes(findContent_Size); //如果是字符串 SendCommand(buffer.Length); // 发送大小 } Console.WriteLine("size+string" + findContent_Size, DateTime.Now.ToLongTimeString()); } else if (type[0].Equals("play")) //跳转到屏幕传输 { PlayBells.StartPlayMedia("DoorBell.wav"); key = 1; if (MessageBox.Show("请求分享风景?", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { PlayBells.StopPlayMedia(); SendCommand("OK"); Pattern = "Scenic sharing"; Thread thread_show = new Thread(ShowShow); thread_show.Start(); threadClose = new Thread(ShowClose); threadClose.Start(); } else { PlayBells.StopPlayMedia(); SendCommand("NO"); } } else if (type[0].Equals("doorbells")) { PlayBells.StartPlayMedia("DoorBell.wav"); key = 1; if (MessageBox.Show("是否接受视频通话?", "客人来访", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { PlayBells.StopPlayMedia(); SendCommand("OK"); Pattern = "Scenic sharing"; Thread thread_show = new Thread(ShowShow);//创建新线程 thread_show.Start(); } else { PlayBells.StopPlayMedia(); SendCommand("NO"); } } else if (type[0].Equals("voice")) //跳转到屏幕传输 { PlayBells.StartPlayMedia("DoorBell.wav"); key = 1; if (MessageBox.Show("请求语音通话?", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { PlayBells.StopPlayMedia(); SendCommand("OK"); Pattern = "Voice Calls"; threadVoice = new Thread(ShowVoice);//创建新线程 threadVoice.Start(); } else { PlayBells.StopPlayMedia(); SendCommand("NO"); } } else if (type[0].Equals("data")) { key = 1; Pattern = "DataSharing"; } else if (type[0].Equals("")) { return; } else //获取数据或执行功能 { Console.WriteLine(type[0], DateTime.Now.ToLongTimeString()); findContent = cmd.Exec(type[0]); } } catch (Exception ex) { } if (!findContent.Equals("wcz") && !findContent.Equals("HQNR") && !findContent.Equals("PMTS") && key == 0) { Console.WriteLine("string" + findContent, DateTime.Now.ToLongTimeString()); SendCommand(findContent); // 发送字符串 findContent = ""; } else if (findContent.Equals("HQNR") && key == 0) { SendCommand(cmd.Get_Byte()); // 发送byte[] findContent = ""; } ReceiveContent = ""; } else if (Pattern == "Scenic sharing") { try { int n = _socket.EndReceive(ia);//接收到的字节数 Console.WriteLine(n.ToString(), DateTime.Now.ToLongTimeString()); ReceiveContent = Encoding.UTF8.GetString(_so.Buffer, 0, n); if (_so.Buffer == null || ReceiveContent == "|||***|||") { threadClose.Abort(); Pattern = "File sharing"; shareform.Close(); } else { closei = 0; Console.WriteLine("shenjing", DateTime.Now.ToLongTimeString()); byte[] buffer = new byte[n]; Image im = ChangeImageWithByte.BytToImg(_so.Buffer, n); // 位图和字符流之间的转换 shareform.Back_Image(im); } } catch (Exception ex) { } ReceiveContent = ""; } else if (Pattern == "Voice Calls") { MemoryStream stream = new MemoryStream(); WavEncoder wav = new WavEncoder(); try { int n = _socket.EndReceive(ia);//接收到的字节数 stream.Write(_so.Buffer, 0, n); ReceiveContent = Encoding.UTF8.GetString(_so.Buffer, 0, n); if (_so.Buffer == null || ReceiveContent == "|||***|||") { Pattern = "File sharing"; Voice_t.Abort(); sharevoice.Close(); } else if (ReceiveContent == "start") { sharevoice.threadvoi = new Thread(MyAudio.Send_Voice); sharevoice.threadvoi.Start(); Voice_t.Abort(); sharevoice.Change(); } else { sound.Stream = wav.Encode(stream); //System.Threading.Thread.Sleep(50); Voice_t = new Thread(new ThreadStart(sound.Play)); Voice_t.Start(); } } catch { } ReceiveContent = ""; } else if (Pattern == "DataSharing") { //name = "哈哈.mp4"; try { int n = _socket.EndReceive(ia);//接收到的字节数 Console.WriteLine(n.ToString(), DateTime.Now.ToLongTimeString()); ReceiveContent = Encoding.UTF8.GetString(_so.Buffer, 0, n); if (ReceiveContent != "|||***|||") { string[] data = ReceiveContent.Split('|'); if (data[0] == "FileName") { name = data[1]; } else { writeFile(_so.Buffer, @"C:\Users\Public\IHomeFiles\" + name, n); MyDos.ExecDosInstructions(@"C:\Users\Public\IHomeFiles\" + name); } } else { Console.WriteLine("shareform.Close();", DateTime.Now.ToLongTimeString()); Pattern = "File sharing"; //shareform.Close(); } } catch (Exception ex) { } ReceiveContent = ""; } // 继续接收 _so = new StateObject(); _so.theSocket = _socket; try { _socket.BeginReceive(_so.Buffer, 0, _so.Buffer.Length, SocketFlags.None, new AsyncCallback(this.ReceiveCallback), _so); } catch (Exception ex) { } }