C#實(shí)現(xiàn)循環(huán)發(fā)送電腦屏幕截圖
本文實(shí)例為大家分享了C#實(shí)現(xiàn)循環(huán)發(fā)送電腦屏幕截圖的具體代碼,供大家參考,具體內(nèi)容如下
寫(xiě)的一個(gè)demo,建立Socket連接之后,循環(huán)發(fā)送電腦屏幕截圖
服務(wù)器端開(kāi)啟之后監(jiān)聽(tīng)端口號(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類(lèi)
s.Bind(ipe);//綁定2000端口
s.Listen(0);//開(kāi)始監(jiān)聽(tīng)
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)
{
}
}
}
//開(kāi)啟
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);//壓縮,似乎無(wú)效果
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#備忘錄人生存檔的設(shè)計(jì)模式實(shí)例
這篇文章主要為大家介紹了C#設(shè)計(jì)模式中備忘錄模式的實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘二
上文對(duì)數(shù)據(jù)結(jié)構(gòu)與算法,有了一個(gè)簡(jiǎn)單的概述與介紹,這篇文章,我們介紹一中典型數(shù)據(jù)結(jié)構(gòu)——線性結(jié)構(gòu)2012-10-10
c#中多線程訪問(wèn)winform控件的若干問(wèn)題小結(jié)
大部分情況下都會(huì)碰到使用多線程控制界面上控件信息的問(wèn)題。然而我們并不能用傳統(tǒng)方法來(lái)解決這個(gè)問(wèn)題,下面我將詳細(xì)的介紹2013-10-10
在C#中g(shù)lobal關(guān)鍵字的作用及其用法
global 是 C# 2.0 中新增的關(guān)鍵字,理論上說(shuō),如果代碼寫(xiě)得好的話,根本不需要用到它,但是不排除一些特別的情況,比如修改別人的代碼,本文僅舉例說(shuō)明。2016-03-03
MVVM簡(jiǎn)化的Messager類(lèi)實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于MVVM簡(jiǎn)化的Messager類(lèi)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
C#實(shí)現(xiàn)公式計(jì)算驗(yàn)證碼的示例詳解
現(xiàn)在很多的平臺(tái)已經(jīng)不使用普通的數(shù)字、字母等驗(yàn)證碼了,取而代之的是拼圖類(lèi)、選圖類(lèi)、旋轉(zhuǎn)類(lèi)或者計(jì)算類(lèi)的驗(yàn)證碼。本文將利用C#實(shí)現(xiàn)一個(gè)公式計(jì)算驗(yàn)證碼,感興趣的可以了解一下2022-10-10
深入淺析C#?11?對(duì)?ref?和?struct?的改進(jìn)
這篇文章主要介紹了C#?11?對(duì)?ref?和?struct?的改進(jìn),有了這些基礎(chǔ)設(shè)施,開(kāi)發(fā)者們將能輕松使用安全的方式來(lái)編寫(xiě)沒(méi)有任何堆內(nèi)存開(kāi)銷(xiāo)的高性能代碼,需要的朋友可以參考下2022-04-04
WPF仿Tabcontrol實(shí)現(xiàn)切換多個(gè)不同View
這篇文章主要為大家詳細(xì)介紹了WPF如何模仿Tabcontrol實(shí)現(xiàn)切換多個(gè)不同View,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
unity實(shí)現(xiàn)場(chǎng)景跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)場(chǎng)景跳轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
C#中ftp檢測(cè)目錄是否存在和創(chuàng)建文件夾的實(shí)現(xiàn)
本文主要介紹了C#中ftp檢測(cè)目錄是否存在和創(chuàng)建文件夾的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

