您的位置:首页 > 编程语言 > C#

c#实现远程截取屏幕:发送端和接收端C/S----C/S

2008-12-04 14:37 405 查看
顺序:先点监听,再点连接,再点发送,再点接收

发送端

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Example023_屏幕捕获程序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

/// <summary>
/// 应用程序的主入口点。
/// </summary>

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, //目标设备的句柄
int nXDest, // 目标对象的左上角的X坐标
int nYDest, // 目标对象的左上角的X坐标
int nWidth, // 目标对象的矩形的宽度
int nHeight, // 目标对象的矩形的长度
IntPtr hdcSrc, // 源设备的句柄
int nXSrc, // 源对象的左上角的X坐标
int nYSrc, // 源对象的左上角的X坐标
System.Int32 dwRop // 光栅的操作值
);

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreateDC(
string lpszDriver, // 驱动名称
string lpszDevice, // 设备名称
string lpszOutput, // 无用,可以设定位"NULL"
IntPtr lpInitData // 任意的打印机数据
);

System.Net.Sockets.Socket sendsocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
//实例化socket
System.Net.IPEndPoint ipendpiont = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"),8888); //服务器的IP和端口//因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。

//连接
private void button2_Click(object sender, EventArgs e)
{

// sendsocket.Connect(ipendpiont);
try
{
sendsocket.Connect(ipendpiont);
MessageBox.Show("连接成功!");

}
catch (Exception se)
{
MessageBox.Show("连接错误" + se.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information);
return;
}
}

//发送

private void button1_Click(object sender, EventArgs e)
{
try
{
this.Hide();
IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);
//创建显示器的DC
Graphics g1 = Graphics.FromHdc(dc1);
//由一个指定设备的句柄创建一个新的Graphics对象
Bitmap MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);
//根据屏幕大小创建一个与之相同大小的Bitmap对象
Graphics g2 = Graphics.FromImage(MyImage);
//获得屏幕的句柄
IntPtr dc3 = g1.GetHdc();
//获得位图的句柄
IntPtr dc2 = g2.GetHdc();
//把当前屏幕捕获到位图对象中
BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc3, 0, 0, 13369376);
//把当前屏幕拷贝到位图中
g1.ReleaseHdc(dc3);
//释放屏幕句柄
g2.ReleaseHdc(dc2);
//释放位图句柄
MyImage.Save("d://新建文件夹//MyJpeg.jpg", ImageFormat.Jpeg); //当然你也可以根据自己的需要,把屏幕以其他图片的格式来保存,如果你想把图片保存为位图文件,可以把"ImageFormat.Jpeg"改换成"ImageFormat.Bmp";想把图片保存为Gif文件,就把"ImageFormat.Jpeg"改换成"ImageFormat.Gif"。你可以保存的文件类型大概有十多种,这里就不一一介绍了,当然你也要相应改变保存文件的后缀。
pictureBox1.Image = MyImage;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //本程序所用控件为1个按狃,一个panel,一个picturebox,其中panel的属性Autoscroll =true; picturebox的属性SizeMode =AutoSize; 然后只要改变pictureBox的大小,图象就会跟着改变大小,如果把这句去掉的话,就可以实现图象按panel滚动条移动而查看全图。

// MessageBox.Show("已经把当前屏幕保存到D://MyJpeg.jpg文件中!");
this.Show();

//***************************************************************************************开始使用socket发送文件*********************************************************//

//建立终结点
System.IO.MemoryStream Stream = new System.IO.MemoryStream();
pictureBox1.Image.Save(Stream, System.Drawing.Imaging.ImageFormat.Bmp);//存储为bmp图象
// pictureBox1.Image.Save(Stream, System.Drawing.Imaging.ImageFormat.Jpeg); //不想存储为bmp图象,想存储为jpeg图象
byte[] b = Stream.ToArray();
//连接远程计算机
sendsocket.Send(b);
//发送

Stream.Close();
sendsocket.Shutdown(System.Net.Sockets.SocketShutdown.Send);
//关闭发送连接
sendsocket.Close(); //关闭本

//***********************************************************************************************************end*******************************************************//

}
catch (Exception ex)
{
string s = ex.ToString();
return;
}
finally

{

sendsocket.Close(); //关闭本

}
}

//时间监控
private void timer1_Tick(object sender, EventArgs e)
{
if (sendsocket.Connected)
{
this.label1.Text= "连接成功";

}
}

}
}

发送端Fomr1.Designer.cs

namespace Example023_屏幕捕获程序
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel1 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(308, 520);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(123, 46);
this.button1.TabIndex = 0;
this.button1.Text = "抓屏发送";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(1, 1);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(665, 479);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(38, 13);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(668, 485);
this.panel1.TabIndex = 2;
//
// button2
//
this.button2.Location = new System.Drawing.Point(100, 520);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(133, 46);
this.button2.TabIndex = 2;
this.button2.Text = "连接";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(480, 553);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 4;
this.label1.Text = "label1";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(736, 608);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Timer timer1;
}
}

接收端

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.IO;
using System.Net.Sockets;
using System.Drawing.Imaging;
namespace Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

System.Net.Sockets.Socket receivesocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
//设置接收数据的地址
System.Net.IPEndPoint hostipendpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any,8888); //需要你用tcpclient访问的IP的端口正在被监听,否则就会显示积极拒绝,不是看他是否被占用,要看他是否在监听

//监听
private void btnConnect_Click(object sender, EventArgs e)
{
receivesocket.Bind(hostipendpoint);
//监听
receivesocket.Listen(2);
this.lblState.Text = "已经开始监听";
}

//接收
private void button1_Click(object sender, EventArgs e)
{
try
{
this.label2.Text = "图象正在传输中,请稍侯";
//设置接收数据缓冲区的大小
byte[] b = new byte[1024 * 4]; //1024* 4
System.Net.Sockets.Socket hostsocket = receivesocket.Accept();
//如何确定该数组大小
System.IO.MemoryStream fs = new System.IO.MemoryStream();

int got = 0;
int datalength = 0;

while (true)
{
got = hostsocket.Receive(b);
fs.Write(b, 0, got);
if (got > 0)
datalength = datalength + got;
else
break;
}
Bitmap Img = new Bitmap(fs);
pictureBox1.Image = Img;
Img.Save("e://新建文件夹//MyJpeg.jpg", ImageFormat.Jpeg);
// this.Image1.ImageUrl = @"e:/MyJpeg.jpg";
//关闭写文件流
fs.Close();
//关闭接收数据的Socket
hostsocket.Shutdown(System.Net.Sockets.SocketShutdown.Receive);
hostsocket.Close();
MessageBox.Show("传输成功!");

}
catch (Exception se)
{
MessageBox.Show("连接错误" + se.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information);
return;
}
}
}
}

接收端Form1.Designer.cs

namespace Client
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.btnConnect = new System.Windows.Forms.Button();
this.lblState = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label2 = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(94, 124);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "接收端";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// btnConnect
//
this.btnConnect.Location = new System.Drawing.Point(87, 60);
this.btnConnect.Name = "btnConnect";
this.btnConnect.Size = new System.Drawing.Size(82, 29);
this.btnConnect.TabIndex = 1;
this.btnConnect.Text = "开始监听";
this.btnConnect.UseVisualStyleBackColor = true;
this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
//
// lblState
//
this.lblState.AutoSize = true;
this.lblState.Location = new System.Drawing.Point(32, 200);
this.lblState.Name = "lblState";
this.lblState.Size = new System.Drawing.Size(41, 12);
this.lblState.TabIndex = 2;
this.lblState.Text = "label1";
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(3, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(592, 547);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex = 3;
this.pictureBox1.TabStop = false;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(46, 283);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 4;
this.label2.Text = "label1";
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(262, 22);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(598, 547);
this.panel1.TabIndex = 5;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(892, 608);
this.Controls.Add(this.label2);
this.Controls.Add(this.lblState);
this.Controls.Add(this.btnConnect);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "接收端";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.Label lblState;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Panel panel1;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: