欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)循環(huán)發(fā)送電腦屏幕截圖

 更新時(shí)間:2021年07月26日 10:21:24   作者:ccessl  
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)循環(huán)發(fā)送電腦屏幕截圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#實(shí)現(xiàn)循環(huán)發(fā)送電腦屏幕截圖的具體代碼,供大家參考,具體內(nèi)容如下

寫的一個(gè)demo,建立Socket連接之后,循環(huán)發(fā)送電腦屏幕截圖

服務(wù)器端開啟之后監(jiān)聽端口號(hào)2000,為新建連接創(chuàng)建新的Socket。之后從客戶端接收截圖,判斷一張圖片接收結(jié)束之后。將圖片顯示于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);//創(chuàng)建一個(gè)Socket類    
            s.Bind(ipe);//綁定2000端口     
            s.Listen(0);//開始監(jiān)聽 
            Socket temp = s.Accept();//為新建連接創(chuàng)建新的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;
                    else
                        ms.Write(buffer, 0, 1024);
                }
                pictureBox1.Image = Bitmap.FromStream(ms);
                ms.Dispose();
            }
            catch(System.ArgumentException  e)
            {
                
            }
        } 
        }
        //開啟
        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);
    
        }
 
    }
}

客戶端連接到服務(wù)器后,獲取屏幕截圖之后,設(shè)置圖片的大小和清晰度,并循環(huán)發(fā)送截圖。

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)//循環(huán)發(fā)送截圖
            {
 
            MemoryStream ms = new MemoryStream();
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.PrimaryScreen.Bounds.Size);//獲取屏幕截圖
            Image mm = SaveJpg(bt,10);//設(shè)置圖片清晰度
            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);//每次發(fā)送1024個(gè)字節(jié)
            }
            string sendStr = "over!";//結(jié)束信息
            byte[] bs = Encoding.ASCII.GetBytes(sendStr); 
            c.Send(bs, bs.Length, 0);//發(fā)送測(cè)試信息
            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和端口轉(zhuǎn)化為IPEndPoint實(shí)例 
            Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//創(chuàng)建一個(gè)Socket    
            Console.WriteLine("Conneting...");
            try
            {
               
                c.Connect(ipe);//連接到服務(wù)器 
                return c;
            }
            catch
            {
                Thread.Sleep(1000);
                connect(port, host);
                return c;
            
            }
 
        }
        public static Image SaveJpg(Image image, long value)//設(shè)置圖像質(zhì)量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);//質(zhì)量,定義圖片的清晰度
            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;
        }
 
    }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論