2015/10/26

C#.Net Expression Encoder 4.0 擷取螢幕並輸出影音

使用前請先安裝Microsoft Expression Encoder 4 with Service Pack 2 (SP2)
版本有分試用版跟正版,關於差異就請各位客觀自己看「About Expression Encoder
不過Expression Encoder 4 目前也不提供購買了
預設儲存的檔案格式為xesc如果要轉成其他的格式可以透過MediaItemJob來做轉換



Code:
using Microsoft.Expression.Encoder.ScreenCapture;
using Microsoft.Expression.Encoder.Devices;
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.ObjectModel;


namespace TestProject
{
    public partial class Form1 : Form
    {

        private ScreenCaptureJob _screenJob = new ScreenCaptureJob();
        private Boolean _isLock = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void Start()
        {
            //輸出資料夾
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {

                //檔案路徑
                String path = dialog.SelectedPath;

                //檔案名稱
                String fileName = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");

                //取得音源裝置
                //Collection<EncoderDevice> audioDevices = EncoderDevices.FindDevices(EncoderDeviceType.Audio);
                //設定音源來源
                //_screenJob.AddAudioDeviceSource(audioDevices.ElementAt(Device_index))

                //輸出檔案路徑
                _screenJob.OutputScreenCaptureFileName = String.Format(@"{0}\{1}.xesc", path, fileName);

                //主螢幕尺寸
                var size = Screen.PrimaryScreen.Bounds;

                //擷取整個螢幕
                _screenJob.CaptureRectangle = new Rectangle(0, 0, size.Width, size.Height);

                //畫質
                _screenJob.ScreenCaptureVideoProfile.Quality = 100;

                //張數
                _screenJob.ScreenCaptureVideoProfile.FrameRate = 30;

                //擷取滑鼠
                _screenJob.CaptureMouseCursor = true;

                //開始擷取
                _screenJob.Start();

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!_isLock)
            {
                _isLock = true;
                Start();
            }
            else
            {
                _isLock = false;

                //停止擷取
                _screenJob.Stop();
            }
        }

    }
}


參考資料:
https://msdn.microsoft.com/en-us/library/microsoft.expression.encoder(v=expression.40).aspx
https://msdn.microsoft.com/en-us/library/microsoft.expression.encoder.presets_members(v=expression.40).aspx
https://msdn.microsoft.com/en-us/library/microsoft.expression.encoder.profiles(v=expression.40).aspx