C#实现循环发送电脑屏幕截图
本文实例为大家分享了C#实现循环发送电脑屏幕截图的具体代码,供大家参考,具体内容如下
写的一个demo,建立Socket连接之后,循环发送电脑屏幕截图
服务器端开启之后监听端口号2000,为新建连接创建新的Socket。之后从客户端接收截图,判断一张图片接收结束之后。将图片显示于pictureBox控件上。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; namespace server{public partial class Form1 : Form{public Thread mythread; public int shu = 0; public Form1(){InitializeComponent(); //CheckForIllegalCrossThreadCalls = false; 不需要}public Socket nect(int port, string host){IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, port); Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建一个Socket类s.Bind(ipe); //绑定2000端口s.Listen(0); //开始监听 Socket temp = s.Accept(); //为新建连接创建新的Socket。 return temp; }public void look(object s){Socket temp = (Socket)s; byte[] buffer = new byte[1024]; while (true){try{MemoryStream ms = new MemoryStream(); // Thread.Sleep(1000); int end = 1; while (end != 0){shu = temp.Receive(buffer, buffer.Length, 0); //从客户端接受信息if (shu == 5)end = 0; elsems.Write(buffer, 0, 1024); }pictureBox1.Image = Bitmap.FromStream(ms); ms.Dispose(); }catch(System.ArgumentExceptione){}} }//开启private void button1_Click(object sender, EventArgs e){int port = 2000; string host = "127.0.0.1"; Socket temp = nect(port, host); mythread = new Thread(new ParameterizedThreadStart(look)); mythread.Start(temp); } }}
客户端连接到服务器后,获取屏幕截图之后,设置图片的大小和清晰度,并循环发送截图。
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Net; using System.Net.Sockets; using System.Drawing; using System.Windows.Forms; using System.CodeDom; using System.Drawing.Imaging; using System.Drawing.Design; using System.Text; using System.Threading; namespace client{class Program{static void Main(string[] args){int port = 2000; string host = "127.0.0.1"; Socket c=connect(port, host); Bitmap bt = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics g= Graphics.FromImage(bt); while(true)//循环发送截图{ MemoryStream ms = new MemoryStream(); g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.PrimaryScreen.Bounds.Size); //获取屏幕截图Image mm = SaveJpg(bt,10); //设置图片清晰度mm = GetWebImage(mm,360,240); //改变截屏图片大小mm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] buffer = new byte[1024]; ms.Position = 0; int end = 1; while (end != 0){end= ms.Read(buffer, 0, 1024); //end为零表示读取完毕c.Send(buffer, buffer.Length, 0); //每次发送1024个字节}string sendStr = "over!"; //结束信息byte[] bs = Encoding.ASCII.GetBytes(sendStr); c.Send(bs, bs.Length, 0); //发送测试信息ms.Dispose(); }c.Close(); Console.ReadLine(); }public static Socket connect(int port, string host){IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, port); //把ip和端口转化为IPEndPoint实例 Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建一个SocketConsole.WriteLine("Conneting..."); try{c.Connect(ipe); //连接到服务器 return c; }catch{Thread.Sleep(1000); connect(port, host); return c; } }public static Image SaveJpg(Image image, long value)//设置图像质量1—100{ImageCodecInfo icInfo = null; ImageCodecInfo[] infos = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo info in infos){if (info.MimeType == "image/jpeg"){icInfo = info; break; }}EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, value); //质量,定义图片的清晰度ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW); //压缩,似乎无效果return image; }public static Image GetWebImage(Image img, int p_Width, int p_Height)//改变图片大小{Bitmap _Bitmap = new Bitmap(p_Width, p_Height); Graphics _Graphcis = Graphics.FromImage(_Bitmap); _Graphcis.DrawImage(img, 0, 0, p_Width, p_Height); _Graphcis.Dispose(); return _Bitmap; } }}
【C#实现循环发送电脑屏幕截图】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- java中如何实现重建二叉树
- 人脸识别|【人脸识别系列】| 实现自动化妆
- paddle|动手从头实现LSTM
- pytorch|使用pytorch从头实现多层LSTM