原文 http://hi.baidu.com/wcsjsdn/item/b2af6de7fefa8a334ddcafb9
看界面:
这 个主要是利用微软提供的库文件实现。(首先要在本机上安装解码器:WMEncoder_cn.exe。注:这个是xp系统的解码器,如有需要,可以下载安 装window7的解码器 ),然后添加引用:Interop.WMEncoderLib.dll,在项目中添加命名空间:using WMEncoderLib;
这也没什么好说的,直接贴上代码:
首先定义一个WMEncoderManage类
public class WMEncoderManage
{
private static WMEncoder _encoder = null;
public static WMEncoder GetWMEncoder
{
get
{
if (_encoder == null)
{
_encoder = new WMEncoder();
}
return _encoder;
}
}
private static string _defaultFormat = "屏幕视频/音频 - 高(CBR)";
public static string DefaultFormat
{
get
{
return _defaultFormat;
}
set { _defaultFormat = value; }
}
}
然后下面开始:
/// <summary>
/// 视频录制方法
/// </summary>
private void Coder()
{
try
{
WMEncoder Encoder = WMEncoderManage.GetWMEncoder;
IWMEncSourceGroup SrcGrp;
IWMEncSourceGroupCollection SrcGrpColl;
SrcGrpColl = Encoder.SourceGroupCollection;
SrcGrp = SrcGrpColl.Add("SG_1");
IWMEncSource SrcVid;
IWMEncSource SrcAud;
SrcVid = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
SrcAud = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
SrcAud.SetInput("Default_Audio_Device", "Device", "");
SrcVid.SetInput("ScreenCapture1", "ScreenCap", "");
IWMEncProfileCollection ProColl;
IWMEncProfile Pro;
int i;
long lLength;
ProColl = Encoder.ProfileCollection;
lLength = ProColl.Count;
for (i = 0; i < lLength - 1; i++)
{
Pro = ProColl.Item(i);
if (Pro.Name == WMEncoderManage.DefaultFormat)
{
SrcGrp.set_Profile(Pro);
break;
}
}
IWMEncDisplayInfo Descr;
Descr = Encoder.DisplayInfo;
Descr.Author = "wangchao";
Descr.Copyright = "wangchao";
Descr.Description = "wangchao";
Descr.Rating = "wangchao";
Descr.Title = "wangchao";
IWMEncAttributes Attr;
Attr = Encoder.Attributes;
Attr.Add("URL", "www.920wc.cn");
IWMEncFile File;
File = Encoder.File;
File.LocalFileName = this.saveFileDialog1.FileName;
Encoder.Start();
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 浏览
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (DialogResult.OK == this.saveFileDialog1.ShowDialog())
{
this.tbFileSave.Text = saveFileDialog1.FileName;
}
}
在"开始录制"按钮事件中:
private void button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.tbFileSave.Text))
{
MessageBox.Show("请先选择视频文件保存路径");
return;
}
Coder();
label3.Text = "录制中...";
}
/// <summary>
/// 停止录制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
WMEncoderManage.GetWMEncoder.Stop();
}
/// <summary>
/// 暂停录制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(tbFileSave.Text))
{
MessageBox.Show("请先开始录制后暂停");
return;
}
if (this.button4.Text == "暂停录制")
{
WMEncoderManage.GetWMEncoder.Pause();
this.button4.Text = "继续录制";
label3.Text = "录制暂时...";
}
else
{
WMEncoderManage.GetWMEncoder.Start();
this.button4.Text = "暂停录制";
label3.Text = "录制中...";
}
}
}
好了,启动程序,选择保存路径及格式,这样就可以直接录制屏幕了。试一下,效果不错。