C#實(shí)現(xiàn)循環(huán)發(fā)送電腦屏幕截圖
本文實(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)文章
C#進(jìn)行圖像處理的常見方法(Bitmap,BitmapData,IntPtr)使用詳解
這篇文章主要為大家詳細(xì)介紹了C#進(jìn)行圖像處理的幾個(gè)常見方法(Bitmap,BitmapData,IntPtr)具體使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-01-01asp.net core mvc權(quán)限控制:在視圖中控制操作權(quán)限
本文主要介紹了asp.net core mvc權(quán)限控制:在視圖中控制操作權(quán)限。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02C#用websocket實(shí)現(xiàn)簡(jiǎn)易聊天功能(服務(wù)端)
這篇文章主要為大家詳細(xì)介紹了C#用websocket實(shí)現(xiàn)簡(jiǎn)易聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02漢字轉(zhuǎn)拼音縮寫示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音)
本篇文章主要介紹了漢字轉(zhuǎn)拼音縮寫示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音) 需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01C#實(shí)現(xiàn)Bitmap類型與Byte[]類型相互轉(zhuǎn)化的示例詳解
在C#編程中,Bitmap類型和Byte[]類型之間的相互轉(zhuǎn)化是圖像處理和數(shù)據(jù)傳輸中常見的需求,Bitmap類型表示一個(gè)位圖圖像,而Byte[]類型則是一個(gè)字節(jié)數(shù)組,本文將詳細(xì)介紹如何在這兩種類型之間進(jìn)行相互轉(zhuǎn)化,需要的朋友可以參考下2024-07-07