void SenBimap() { int i = 0; int post = 18179; while (true) { int screenWidth = System.Windows.Forms.SystemInformation.VirtualScreen.Width; //屏幕宽度 int screenHeight = System.Windows.Forms.SystemInformation.VirtualScreen.Height; //屏幕高度 //创建图象,保存将来截取的图象 Bitmap image = new Bitmap(screenWidth, screenHeight); Graphics imgGraphics = Graphics.FromImage(image); //设置截屏区域 imgGraphics.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight), CopyPixelOperation.SourceCopy); imgGraphics.Dispose(); //保存 //SaveImage(image); TcpClient TcpYclj = new TcpClient(); if (i == 30) { i++; post++; i = 0; } TcpYclj = new TcpClient(ycip, post); NetworkStream NetStrm; byte[] data = PhotoImageInsert(image); data = Compress(data); NetStrm = TcpYclj.GetStream(); NetStrm.Write(data, 0, data.Length); data = Encoding.UTF8.GetBytes("完成".ToCharArray()); NetStrm.Write(data, 0, data.Length); NetStrm.Close(); TcpYclj.Close(); i++; //MessageBox.Show("发送成功!"); } }
/**/ ////根据图片文件的路径使用文件流打开,并保存为byte[] FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法 byte[] byData = new byte[fs.Length]; fs.Read(byData, 0, byData.Length); fs.Close(); return(byData); } public void SendImgFiles(string FilesPath) { TcpClient Tcp = new TcpClient(comboBox1.Text, 12347); byte[] data = GetPictureData(FilesPath); NetworkStream NetStrm; NetStrm = Tcp.GetStream(); NetStrm.Write(data, 0, data.Length); data = Encoding.UTF8.GetBytes("结束".ToCharArray()); NetStrm.Write(data, 0, data.Length); NetStrm.Close(); Tcp.Close(); Tcp = null; } ArrayList Cg = new ArrayList(); private void Sleep(object o) { string TcpIP = (string)o; TcpClient Tcp1 = new TcpClient(); try { Tcp1 = new TcpClient(TcpIP, po); textBox1.Invoke(my_TextBoxApText, TcpIP + ":12346:ok", textBox1); Cg.Add(TcpIP); comboBox1.Invoke(my_ComboBoxAdd, TcpIP, comboBox1); NetworkStream NetStrm; NetStrm = Tcp1.GetStream(); byte[] data = Encoding.ASCII.GetBytes(wb.ToCharArray()); NetStrm.Write(data, 0, data.Length); NetStrm.Close(); Tcp1.Close(); Tcp1 = null; } catch { textBox1.Invoke(my_TextBoxApText, TcpIP + ":12346:no", textBox1); Tcp1.Close(); } } public class TcpBegin { private string ip1; public TcpBegin(string ip2) { ip1 = ip2; } public void ConnectBegin() { string ip = ip1; Thread thread = new Thread(new ParameterizedThreadStart(Sleep)); thread.Start((object)ip); } private void Sleep(object o) { } } private void button1_Click(object sender, EventArgs e) { SendImgFiles(@textBox2.Text); } /* * 12346是打招呼的端口 * 12347是发文件的端口 * 12345 * */ private void button2_Click(object sender, EventArgs e) { TcpClient tcp = new TcpClient(comboBox1.Text, 12345); NetworkStream NetStrm; NetStrm = tcp.GetStream(); string nr = "远程请求"; byte[] data = Encoding.UTF8.GetBytes(nr.ToCharArray()); NetStrm.Write(data, 0, data.Length); NetStrm.Close(); tcp.Close(); ycip = comboBox1.Text; Thread t = new Thread(new ThreadStart(SenBimap)); t.IsBackground = true; t.Start(); button2.Enabled = false; }