C#GDI+视屏广播接收端
来源:互联网 发布:淘宝助理发货网点设置 编辑:程序博客网 时间:2024/05/16 08:41
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.Threading;
using System.IO;
using System.Drawing.Imaging;
using System.Net;
using System.Net.Sockets;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
Form.CheckForIllegalCrossThreadCalls = false;//这里是为了让我们跨线程访问空间的时候不会报错
}
private void Form2_Load(object sender, EventArgs e)
{
//接收广播***********
//新建一个线程来接收广播
Thread th = new Thread(new ThreadStart(RecviceData));
th.IsBackground = true;
th.Start();
}
int port = 2000;//端口
public void RecviceData()
{
//要接收的ip
IPAddress ip = IPAddress.Any;
//创建接收对象
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//绑定接收对象
EndPoint iep=new IPEndPoint(ip,port);
server.Bind(iep);
//准备接受的容器
byte[] buffer=new byte[600000];
//int width=Screen.PrimaryScreen.Bounds.Width;
//int height=Screen.PrimaryScreen.Bounds.Height;
while (true)//循环接收
{
//Bitmap bmp = new Bitmap(width, height);
MemoryStream ms = new MemoryStream();
int len = server.Receive(buffer);
ms.Write(buffer, 0, len);
while (true)//
{
if (len==1&&buffer[0]==100)//如果收到对方发送的标识(只有一个字节,并且第一个字节为100)则跳出循环
{
break;
}
len = server.Receive(buffer);
ms.Write(buffer, 0, len);
}
Bitmap bmp=new Bitmap(ms);//将流转化为图片
pictureBox1.Image = bmp;//将图片显示在pictureBox上
pictureBox1.Refresh();//刷新控件
}
}
private void 全屏ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;//设置窗体的边框样式为无边框
this.WindowState = FormWindowState.Maximized;//默认窗体的显示尺寸为最大
this.TopMost = true;//设置窗体默认显示在最前面
}
private void 窗口ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.Sizable;
this.WindowState = FormWindowState.Normal;
}
private void 居中ToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void 平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
private void 拉伸ToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
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.Threading;
using System.IO;
using System.Drawing.Imaging;
using System.Net;
using System.Net.Sockets;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
Form.CheckForIllegalCrossThreadCalls = false;//这里是为了让我们跨线程访问空间的时候不会报错
}
private void Form2_Load(object sender, EventArgs e)
{
//接收广播***********
//新建一个线程来接收广播
Thread th = new Thread(new ThreadStart(RecviceData));
th.IsBackground = true;
th.Start();
}
int port = 2000;//端口
public void RecviceData()
{
//要接收的ip
IPAddress ip = IPAddress.Any;
//创建接收对象
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//绑定接收对象
EndPoint iep=new IPEndPoint(ip,port);
server.Bind(iep);
//准备接受的容器
byte[] buffer=new byte[600000];
//int width=Screen.PrimaryScreen.Bounds.Width;
//int height=Screen.PrimaryScreen.Bounds.Height;
while (true)//循环接收
{
//Bitmap bmp = new Bitmap(width, height);
MemoryStream ms = new MemoryStream();
int len = server.Receive(buffer);
ms.Write(buffer, 0, len);
while (true)//
{
if (len==1&&buffer[0]==100)//如果收到对方发送的标识(只有一个字节,并且第一个字节为100)则跳出循环
{
break;
}
len = server.Receive(buffer);
ms.Write(buffer, 0, len);
}
Bitmap bmp=new Bitmap(ms);//将流转化为图片
pictureBox1.Image = bmp;//将图片显示在pictureBox上
pictureBox1.Refresh();//刷新控件
}
}
private void 全屏ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;//设置窗体的边框样式为无边框
this.WindowState = FormWindowState.Maximized;//默认窗体的显示尺寸为最大
this.TopMost = true;//设置窗体默认显示在最前面
}
private void 窗口ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.Sizable;
this.WindowState = FormWindowState.Normal;
}
private void 居中ToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void 平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
private void 拉伸ToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
}
原理:
将从网络上接受到的字节流转化为图片,并且显示在picturebox控件上,利用线程不停的接受图片并且显示在控件上,就可以实现动态效果。
缺点:
因为是从桌面上截的图,所以在广播中是看不到光标和鼠标的,而且图片的传输很耗费网络资源,整个程序的运行效果可能不是很流畅。
- C#GDI+视屏广播接收端
- C#GDI+实现简单视屏广播功能
- 广播接收
- 接收广播
- Android客户端接收PC端UDP广播
- C语言socket UDP广播的发送和接收示例
- Linux里UDP协议 广播和接收测试 C语言
- 发送广播和接收广播
- 广播接收销毁
- Intent 广播 发送 接收
- 接收和发送广播
- 短信接收广播
- 接收和发送广播
- 广播数据包的接收
- 广播发送与接收
- BroadcastReceiver(接收广播)
- 28广播接收
- EventBus实现广播接收
- 生活,温暖如花
- 重拾Android(1)----关于手机的认识
- HBASE 表结构设计
- Android TextView文字横向自动滚动(跑马灯)
- 初装IIS常见错误及解决办法 自用
- C#GDI+视屏广播接收端
- CMD(网络命令大全)
- Ubuntu12.04 死机 Freeze 问题 解决方案
- 一个独立游戏人的创业自白:最大的敌人是自己
- mysql的字符集设置
- MyEclipse中SVN的使用方法
- 3.windbg script----DML简介
- 求指点创业迷途
- Linux系统内存管理之伙伴系统分析