forked from Trevor3000/RemoteControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrmCaptureVideo.cs
More file actions
180 lines (167 loc) · 6.55 KB
/
FrmCaptureVideo.cs
File metadata and controls
180 lines (167 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RemoteControl.Protocals;
using RemoteControl.Audio;
using RemoteControl.Server.Utils;
namespace RemoteControl.Server
{
public partial class FrmCaptureVideo : FrmBase
{
private SocketSession oSession;
private bool saveInRealTime = false;
private int _fps = 2;
private bool _captureAudio = false;
public FrmCaptureVideo(SocketSession session)
{
InitializeComponent();
this.oSession = session;
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
ToolStripButton button = sender as ToolStripButton;
button.Checked = !button.Checked;
if (button.Checked)
{
this.toolStripSplitButton2.Enabled = false;
RequestStartCaptureVideo req = new RequestStartCaptureVideo();
req.Fps = _fps;
oSession.Send(ePacketType.PACKET_START_CAPTURE_VIDEO_REQUEST, req);
}
else
{
this.toolStripSplitButton2.Enabled = true;
oSession.Send(ePacketType.PACKET_STOP_CAPTURE_VIDEO_REQUEST, null);
}
}
public void HandleScreen(ResponseStartCaptureVideo resp)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<ResponseStartCaptureVideo>(HandleScreen), resp);
return;
}
//MsgBox.ShowInfo(resp.ImageData.Length + "");
this.pictureBox1.Image = resp.GetImage();
this.toolStripStatusLabel1.Text = "图像采集时间:" + resp.CollectTime;
this.toolStripStatusLabel2.Text = "图像返回时间:" + DateTime.Now;
// 实时保存
if (this.saveInRealTime)
{
try
{
if (!System.IO.Directory.Exists("CaptureVideo"))
{
System.IO.Directory.CreateDirectory("CaptureVideo");
}
string dir = Application.StartupPath + "\\CaptureVideo\\" + oSession.SocketId.Replace(":","-") + "\\";
if (!System.IO.Directory.Exists(dir))
System.IO.Directory.CreateDirectory(dir);
string filename = dir + resp.CollectTime.ToString("yyyyMMddHHmmssfff") + ".jpg";
System.IO.File.WriteAllBytes(filename, resp.ImageData);
}
catch (Exception ex)
{
}
}
}
private void FrmCaptureScreen_Load(object sender, EventArgs e)
{
// Panel增加滚动条
this.panel1.AutoScroll = true;
// 根据图像大小,自动调节控件和Image的尺寸
this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
}
private void toolStripButtonSave_ButtonClick(object sender, EventArgs e)
{
if (this.pictureBox1.Image != null)
{
string fileName = "";
// 直接从picturebox中调用save()的话,容易出现“GDI+ 发生一般性错误”。
// 此处用bitmap对象中专一次
using (Bitmap bmp = new Bitmap(this.pictureBox1.Image))
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "*.bmp|*.bmp|*.jpg;*.jpeg|*.jpg;*.jpeg|*.*|*.*";
dialog.FilterIndex = 1;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileName = dialog.FileName;
try
{
bmp.Save(fileName);
MsgBox.Info("保存成功!");
}
catch (Exception ex)
{
MsgBox.Info("保存失败," + ex.Message);
}
}
}
}
else
{
MsgBox.Info("暂无图像,无法保存!");
}
}
private void 实时保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.saveInRealTime = !this.saveInRealTime;
this.实时保存ToolStripMenuItem.Checked = !this.实时保存ToolStripMenuItem.Checked;
}
/// <summary>
/// 不同的帧率的点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItemFPS_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = sender as ToolStripMenuItem;
if (item != null && item.Tag != null)
{
var parent = this.toolStripSplitButton2;
for (int i = 0; i < parent.DropDownItems.Count; i++)
{
var mItem = parent.DropDownItems[i] as ToolStripMenuItem;
if (mItem != null)
{
mItem.Checked = false;
}
}
_fps = Convert.ToInt32(item.Tag);
item.Checked = true;
}
}
private void toolStripMenuItemCaptureAudio_Click(object sender, EventArgs e)
{
toolStripMenuItemCaptureAudio.Checked = !toolStripMenuItemCaptureAudio.Checked;
_captureAudio = toolStripMenuItemCaptureAudio.Checked;
if (_captureAudio)
{
RequestStartCaptureAudio req = new RequestStartCaptureAudio();
this.oSession.Send(ePacketType.PACKET_START_CAPTURE_AUDIO_REQUEST, req);
}
else
{
this.oSession.Send(ePacketType.PACKET_STOP_CAPTURE_AUDIO_REQUEST, null);
}
}
private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
{
ToolStripSplitButton btn = sender as ToolStripSplitButton;
if (btn != null)
{
btn.ShowDropDown();
}
}
private void FrmCaptureVideo_FormClosing(object sender, FormClosingEventArgs e)
{
oSession.Send(ePacketType.PACKET_STOP_CAPTURE_VIDEO_REQUEST, null);
oSession.Send(ePacketType.PACKET_STOP_CAPTURE_AUDIO_REQUEST, null);
}
}
}