C#螢幕錄影控制項代碼

來源:互聯網
上載者:User

標籤:style   http   os   io   檔案   for   art   ar   

錄影控制項Demo 下載:
http://hocor.cn/sc.rar

下面是主要代碼,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WMEncoderLib;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Threading;
using System.IO;
using System.Drawing.Drawing2D;

namespace F.ScreenCamera
{
public partial class MainForm : Form
{

//////////有關係統托盤

//////////Win32

//////////成員

///////////////////////////////////////////////////////////////////////////////////////////////
////// 程式初始化
///private void AppInit()
{
enc = new WMEncoderClass();
LoadSeting();
}
////// 設定皮膚
//////private void SetSkin(string SkinName)
{
this.skin.SerialNumber = "";
this.skin.SkinFile = "Skin\\" + SkinName + ".ssk";
}
////// 初始壓縮選項列表
///private void InitCompression()
{
//
ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();
string Compression = appseting.ReadKeyValue("Compression");
//
enc = new WMEncoderClass();
wspim = enc.SourcePluginInfoManager;
wspim.Refresh();
IWMEncProfileCollection wpfc = enc.ProfileCollection;
IWMEncProfile wp;
this.CompressionOptionListBox.Items.Clear();
for (int i = 0; i < wpfc.Count; i++)
{
wp = wpfc.Item(i);
this.CompressionOptionListBox.Items.Add(wp.Name);
if (wp.Name == Compression)
this.CompressionOptionListBox.SelectedIndex = i;
}

}
////// 載入設定
///private void LoadSeting()
{
ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();
//是否錄製聲音
this.ChkSound.Checked =Convert.ToBoolean(appseting.ReadKeyValue("ChkSound"));
//是否隱藏主表單
this.ChkHideMainForm.Checked = Convert.ToBoolean(appseting.ReadKeyValue("ChkHideMainForm"));
//快速鍵設定
this.SkStartAndPause.Text = appseting.ReadKeyValue("SKey_StartAndPause");
this.SkStop.Text = appseting.ReadKeyValue("SKey_Stop");
this.SkShowAndHide.Text = appseting.ReadKeyValue("SKey_ShowAndHide");
//錄製地區
int rect = Convert.ToInt32(appseting.ReadKeyValue("CameraRect"));
if (rect == 1)
this.RWindow.Checked = true;
else
this.RScreen.Checked = true;
//
Size sz = MainForm.GetScreenSize();
this.rw.Text = sz.Width.ToString();
this.rh.Text = sz.Height.ToString();
}
////// 取得選擇的壓縮選項
//////private IWMEncProfile2 GetSelectCompressionOption()
{
IWMEncProfileCollection wpfc = enc.ProfileCollection;
IWMEncProfile wp;
IWMEncProfile2 wp2 = new WMEncProfile2Class();
if (this.CompressionOptionListBox.SelectedIndex == -1)
{
return null;
}
for (int i = 0; i < wpfc.Count; i++)
{
wp = wpfc.Item(i);

if (this.CompressionOptionListBox.SelectedItem.ToString() == wp.Name)
{
wp2.LoadFromIWMProfile(wp);
return wp2;
}
}
return null;
}
////// 開始錄製
///private void StartCamera()
{
IWMEncSourceGroupCollection SrcGrpColl;
IWMEncSourceGroup2 SrcGrp;
IWMEncAudioSource SrcAud;
IWMEncVideoSource2 SrcVid;
IWMEncProfile2 Pro;
enc = new WMEncoderClass();
//-------------------------------------------
try
{
SrcGrpColl = enc.SourceGroupCollection;
SrcGrp = (IWMEncSourceGroup2)SrcGrpColl.Add("SG_1");
SrcVid = (IWMEncVideoSource2)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);

//是否錄製聲音
if (this.ChkSound.Checked)
{
SrcAud = (IWMEncAudioSource)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
SrcAud.SetInput("Default_Audio_Device", "DEVICE", "");
}
SrcVid.SetInput("ScreenCapture1", "ScreenCap", "");

指定螢幕地區錄製

//確定壓縮方式
Pro = GetSelectCompressionOption();
if (Pro == null)
{
MessageBox.Show("錯誤!請選中 視頻壓縮模板!");
return;
}
SrcGrp.set_Profile(Pro);

//檢查輸出檔案名是否為空白
if (this.OutFilePathTextBox.Text.Length < 1)
{
MessageBox.Show("請指定儲存路徑!");
return;
}

輸出檔案名

enc.Start();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}

}
///////////////////////////////////////////////////////////////////////////////////////////////
////// 構造
///public MainForm()
{
InitializeComponent();
//Control.CheckForIllegalCrossThreadCalls = false;
//載入皮膚
ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();
string skin = appseting.ReadKeyValue("skin");
this.SetSkin(skin);
//
//初始托盤
NotificationIcon();
//
//表單大小固定
this.MinimumSize=this.Size;
this.MaximumSize = this.Size;
}
////// 主表單載入事件
/////////private void MainForm_Load(object sender, EventArgs e)
{
AppInit();
InitCompression();
}
////// 選擇輸出檔案儲存路徑
/////////private void SelectOutPathButton_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "WMV檔案(*.wmv)|*.wmv";
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
this.OutFilePathTextBox.Text = sfd.FileName;
if(File.Exists(sfd.FileName))
File.Delete(sfd.FileName);
}
}
////// 開始錄製
/////////private void StartButton_Click(object sender, EventArgs e)
{
if (this.CompressionOptionListBox.SelectedIndex > -1 && this.OutFilePathTextBox.Text.Trim() != "")
{

th = new Thread( new ThreadStart(start));
th.IsBackground = true;
th.Start();
//將開始按鈕不可用
this.StartButton.Enabled = false;
//暫停按鈕可用
this.PauseButton.Enabled = true;
//
//表單隱藏
if (this.ChkHideMainForm.Checked)
{
this.WindowState = FormWindowState.Minimized;
this.Visible = false;
}
}
else
{
if (this.OutFilePathTextBox.Text.Trim() == "")
MessageBox.Show("請選擇錄影檔案儲存位置");
if (this.CompressionOptionListBox.SelectedIndex == -1)
MessageBox.Show("請選擇一種壓縮方案");
}
}
private void start()
{
VoidDelegate dstart = new VoidDelegate(StartCamera);
this.Invoke(dstart);
}
////// 停止錄製按鈕
/////////private void StopButton_Click(object sender, EventArgs e)
{
th.Abort();
enc.Stop();
this.StartButton.Enabled = true;
this.PauseButton.Enabled = false;

}
////// 暫停錄製按鈕
/////////private void PauseButton_Click(object sender, EventArgs e)
{
if (this.PauseButton.Text != "繼續錄製")
{
enc.Pause();
this.PauseButton.Text = "繼續錄製";
}
else
{
enc.Start();
this.PauseButton.Text = "暫停錄製";
}
}
////// 單擊主表單關閉按鈕
/////////private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.ApplicationExitCall && e.CloseReason != CloseReason.WindowsShutDown)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.Visible= false;
}
else
{
//none
}
}
////// 壓縮選項更改
/////////private void CompressionOptionListBox_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox lb = (ListBox)sender;
ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();
appseting.UpdateKey("Compression", lb.Items[lb.SelectedIndex].ToString());
}
////// 壓縮選項更改
/////////private void CompressionOptionListBox_MouseClick(object sender, MouseEventArgs e)
{
ListBox lb = (ListBox)sender;
ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();
appseting.UpdateKey("Compression", lb.Items[lb.SelectedIndex].ToString());
}

快速鍵

錄製地區
////// 是否錄聲音
/////////private void ChkSound_CheckedChanged(object sender, EventArgs e)
{
ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();
appseting.UpdateKey("ChkSound", this.ChkSound.Checked.ToString());
}
//是否隱藏主表單
private void ChkHideMainForm_CheckedChanged(object sender, EventArgs e)
{
ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();
appseting.UpdateKey("ChkHideMainForm", this.ChkHideMainForm.Checked.ToString());
}

//////////////////////////////////////////////////////////////////////////////
////// 取得螢幕大小
//////public static Size GetScreenSize()
{
//獲得當前螢幕的解析度
Screen scr = Screen.PrimaryScreen;
Rectangle rc = scr.Bounds;
Size size = new Size(rc.Width, rc.Height);
return size;
}

 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.