📄 mainform.cs
字号:
/******************************************************
DirectShow .NET
netmaster@swissonline.ch
*******************************************************/
// SampleGrabber MainForm
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;
using System.Net.Sockets ;
using System.Net;
//using System.IO;
using DShowNET;
using DShowNET.Device;
namespace SampleGrabberNET
{
/// <summary> Summary description for MainForm. </summary>
public class MainForm : System.Windows.Forms.Form, ISampleGrabberCB
{
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.ToolBar toolBar;
private System.Windows.Forms.Panel videoPanel;
private System.Windows.Forms.Panel stillPanel;
private System.Windows.Forms.PictureBox pictureBox;
private System.Windows.Forms.ToolBarButton toolBarBtnTune;
private System.Windows.Forms.ToolBarButton toolBarBtnGrab;
private System.Windows.Forms.ToolBarButton toolBarBtnSave;
private System.Windows.Forms.ToolBarButton toolBarBtnSep;
private System.Windows.Forms.ImageList imgListToolBar;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem4;
private System.ComponentModel.IContainer components;
public MainForm()
{
// Required for Windows Form Designer support
InitializeComponent();
}
/// <summary> Clean up any resources being used. </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
CloseInterfaces();
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
this.toolBar = new System.Windows.Forms.ToolBar();
this.toolBarBtnTune = new System.Windows.Forms.ToolBarButton();
this.toolBarBtnGrab = new System.Windows.Forms.ToolBarButton();
this.toolBarBtnSep = new System.Windows.Forms.ToolBarButton();
this.toolBarBtnSave = new System.Windows.Forms.ToolBarButton();
this.imgListToolBar = new System.Windows.Forms.ImageList(this.components);
this.videoPanel = new System.Windows.Forms.Panel();
this.splitter1 = new System.Windows.Forms.Splitter();
this.stillPanel = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.stillPanel.SuspendLayout();
this.SuspendLayout();
//
// toolBar
//
this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarBtnTune,
this.toolBarBtnGrab,
this.toolBarBtnSep,
this.toolBarBtnSave});
this.toolBar.Cursor = System.Windows.Forms.Cursors.Hand;
this.toolBar.DropDownArrows = true;
this.toolBar.ImageList = this.imgListToolBar;
this.toolBar.Location = new System.Drawing.Point(0, 0);
this.toolBar.Name = "toolBar";
this.toolBar.ShowToolTips = true;
this.toolBar.Size = new System.Drawing.Size(610, 42);
this.toolBar.TabIndex = 0;
this.toolBar.Visible = false;
this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
//
// toolBarBtnTune
//
this.toolBarBtnTune.Enabled = false;
this.toolBarBtnTune.ImageIndex = 0;
this.toolBarBtnTune.Text = "Tune";
this.toolBarBtnTune.ToolTipText = "TV tuner dialog";
//
// toolBarBtnGrab
//
this.toolBarBtnGrab.ImageIndex = 1;
this.toolBarBtnGrab.Text = "Grab";
this.toolBarBtnGrab.ToolTipText = "Grab picture from stream";
//
// toolBarBtnSep
//
this.toolBarBtnSep.Enabled = false;
this.toolBarBtnSep.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// toolBarBtnSave
//
this.toolBarBtnSave.Enabled = false;
this.toolBarBtnSave.ImageIndex = 2;
this.toolBarBtnSave.Text = "Save";
this.toolBarBtnSave.ToolTipText = "Save image to file";
//
// imgListToolBar
//
this.imgListToolBar.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
this.imgListToolBar.ImageSize = new System.Drawing.Size(16, 16);
this.imgListToolBar.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgListToolBar.ImageStream")));
this.imgListToolBar.TransparentColor = System.Drawing.Color.Transparent;
//
// videoPanel
//
this.videoPanel.BackColor = System.Drawing.Color.Black;
this.videoPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.videoPanel.Dock = System.Windows.Forms.DockStyle.Left;
this.videoPanel.Location = new System.Drawing.Point(0, 42);
this.videoPanel.Name = "videoPanel";
this.videoPanel.Size = new System.Drawing.Size(328, 241);
this.videoPanel.TabIndex = 1;
this.videoPanel.Resize += new System.EventHandler(this.videoPanel_Resize);
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(328, 42);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(5, 241);
this.splitter1.TabIndex = 2;
this.splitter1.TabStop = false;
//
// stillPanel
//
this.stillPanel.AutoScroll = true;
this.stillPanel.AutoScrollMargin = new System.Drawing.Size(8, 8);
this.stillPanel.AutoScrollMinSize = new System.Drawing.Size(32, 32);
this.stillPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.stillPanel.Controls.Add(this.label2);
this.stillPanel.Controls.Add(this.pictureBox);
this.stillPanel.Controls.Add(this.textBox1);
this.stillPanel.Controls.Add(this.label1);
this.stillPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.stillPanel.Location = new System.Drawing.Point(333, 42);
this.stillPanel.Name = "stillPanel";
this.stillPanel.Size = new System.Drawing.Size(277, 241);
this.stillPanel.TabIndex = 3;
//
// label2
//
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
this.label2.Location = new System.Drawing.Point(16, 120);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(224, 48);
this.label2.TabIndex = 6;
this.label2.Text = "Paused";
//
// pictureBox
//
this.pictureBox.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox.Image")));
this.pictureBox.Location = new System.Drawing.Point(-16, 48);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(627, 395);
this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
this.pictureBox.Visible = false;
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.Color.White;
this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.textBox1.Location = new System.Drawing.Point(0, 23);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(619, 20);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "224.100.0.1";
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
//
// label1
//
this.label1.Dock = System.Windows.Forms.DockStyle.Top;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(619, 23);
this.label1.TabIndex = 5;
this.label1.Text = "Group";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "Start";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.Enabled = false;
this.menuItem2.Index = 1;
this.menuItem2.Text = "Stop";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "Hide";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// timer1
//
this.timer1.Interval = 300;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// notifyIcon1
//
this.notifyIcon1.ContextMenu = this.contextMenu1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "Multicast Conference System";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4});
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Text = "Show";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(610, 283);
this.Controls.Add(this.stillPanel);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.videoPanel);
this.Controls.Add(this.toolBar);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Video Conference";
this.TopMost = true;
this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
this.Load += new System.EventHandler(this.MainForm_Load);
this.Activated += new System.EventHandler(this.MainForm_Activated);
this.stillPanel.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
// static void Main()
// {
// Application.Run(new MainForm());
// }
private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.Hide ();
e.Cancel = true;
// this.Hide();
// CloseInterfaces();
}
/// <summary> detect first form appearance, start grabber. </summary>
private void MainForm_Activated(object sender, System.EventArgs e)
{
if( firstActive )
return;
firstActive = true;
if( ! DsUtils.IsCorrectDirectXVersion() )
{
MessageBox.Show( this, "DirectX 8.1 NOT installed!", "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop );
this.Close(); return;
}
if( ! DsDev.GetDevicesOfCat( FilterCategory.VideoInputDevice, out capDevices ) )
{
MessageBox.Show( this, "No video capture devices found!", "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop );
this.Close(); return;
}
DsDevice dev = null;
if( capDevices.Count == 1 )
dev = capDevices[0] as DsDevice;
else
{
DeviceSelector selector = new DeviceSelector( capDevices );
selector.ShowDialog( this );
dev = selector.SelectedDevice;
}
if( dev == null )
{
this.Close(); return;
}
if( ! StartupVideo( dev.Mon ) )
this.Close();
}
private void videoPanel_Resize(object sender, System.EventArgs e)
{
ResizeVideoWindow();
}
/// <summary> handler for toolbar button clicks. </summary>
private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
try
{
Trace.WriteLine( "!!BTN: toolBar_ButtonClick" );
int hr;
if( sampGrabber == null )
return;
Trace.WriteLine( "!!BTN: toolBarBtnGrab" );
if( savedArray == null )
{
int size = videoInfoHeader.BmiHeader.ImageSize;
if( (size < 1000) || (size > 16000000) )
return;
savedArray = new byte[ size + 64000 ];
}
toolBarBtnSave.Enabled = false;
Image old = pictureBox.Image;
//pictureBox.Image = null;
if( old != null )
old.Dispose();
toolBarBtnGrab.Enabled = false;
captured = false;
hr = sampGrabber.SetCallback( this, 1 );
Trace.WriteLine( "!!BTN: toolBarBtnSave" );
if( capGraph != null )
DsUtils.ShowTunerPinDialog( capGraph, capFilter, this.Handle );
}
catch (Exception){}
}
/// <summary> capture event, triggered by buffer callback. </summary>
void OnCaptureDone()
{
try {
Trace.WriteLine( "!!DLG: OnCaptureDone" );
toolBarBtnGrab.Enabled = true;
int hr;
if( sampGrabber == null )
return;
hr = sampGrabber.SetCallback( null, 0 );
int w = videoInfoHeader.BmiHeader.Width;
int h = videoInfoHeader.BmiHeader.Height;
if( ((w & 0x03) != 0) || (w < 32) || (w > 4096) || (h < 32) || (h > 4096) )
return;
int stride = w * 3;
GCHandle handle = GCHandle.Alloc( savedArray, GCHandleType.Pinned );
int scan0 = (int) handle.AddrOfPinnedObject();
scan0 += (h - 1) * stride;
Bitmap b = new Bitmap( w, h, -stride, PixelFormat.Format24bppRgb, (IntPtr) scan0 );
handle.Free();
savedArray = null;
Image old = pictureBox.Image;
pictureBox.Image = b;
if( old != null )
old.Dispose();
toolBarBtnSave.Enabled = true;
}
catch( Exception)
{
}
}
/// <summary> start all the interfaces, graphs and preview window. </summary>
bool StartupVideo( UCOMIMoniker mon )
{
int hr;
try {
if( ! CreateCaptureDevice( mon ) )
return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -