C# 实现屏幕录像

来源:互联网 发布:js弹出框 编辑:程序博客网 时间:2024/05/16 07:24

下面是主要代码:(没有注释,不要骂人)

[c-sharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using WMEncoderLib;  
  9. using System.Runtime.InteropServices;  
  10. using Microsoft.Win32;  
  11. using System.Threading;  
  12. using System.IO;  
  13. using System.Drawing.Drawing2D;  
  14.   
  15. namespace F.ScreenCamera  
  16. {  
  17.     public partial class MainForm : Form  
  18.     {  
  19.   
  20.         有关系统托盘#region 有关系统托盘  
  21.         /**//// <summary>  
  22.         /// 托盘图标类实例  
  23.         /// </summary>  
  24.         private NotifyIcon notifyIcon;  
  25.         /**//// <summary>  
  26.         /// 托盘菜单   
  27.         /// </summary>  
  28.         private ContextMenu notificationMenu;  
  29.         /**//// <summary>  
  30.         /// 初始化托盘  
  31.         /// </summary>  
  32.         public void NotificationIcon()  
  33.         {  
  34.             notifyIcon = new NotifyIcon();  
  35.             notificationMenu = new ContextMenu(InitializeMenu());  
  36.             notifyIcon.DoubleClick += IconDoubleClick;  
  37.             notifyIcon.Icon = this.Icon;  
  38.             notifyIcon.ContextMenu = notificationMenu;  
  39.             notifyIcon.Visible = true;  
  40.         }  
  41.         /**//// <summary>  
  42.         /// 托盘菜单  
  43.         /// </summary>  
  44.         /// <returns></returns>  
  45.         private MenuItem[] InitializeMenu()  
  46.         {  
  47.             //加载主题菜单  
  48.             MenuItem SkinMenu = new MenuItem("主题");  
  49.             DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + "//Skin");  
  50.             foreach (FileInfo skinfile in dir.GetFiles())  
  51.             {  
  52.                 string skinname = skinfile.Name.Substring(0, (int)skinfile.Name.Length - 4);  
  53.                 SkinMenu.MenuItems.Add(new MenuItem(skinname, ChangeSkin));  
  54.             }  
  55.             //  
  56.             //  
  57.             MenuItem[] menu = new MenuItem[]  
  58.             {  
  59.                 new MenuItem("显示主窗口", IconDoubleClick),  
  60.                 new MenuItem("隐藏主窗口", HideMainForm),  
  61.                 SkinMenu,  
  62.     new MenuItem("关于", menuAboutClick),  
  63.     new MenuItem("退出", menuExitClick)  
  64.    };  
  65.             return menu;  
  66.         }  
  67.                     托盘事件#region 托盘事件  
  68.                     /**//// <summary>  
  69.                     /// 托盘关于菜单   
  70.                     /// </summary>  
  71.                     /// <param name="sender"></param>  
  72.                     /// <param name="e"></param>  
  73.                     private void menuAboutClick(object sender, EventArgs e)  
  74.                     {  
  75.   
  76.                         if (about == null || about.IsDisposed)  
  77.                             about = new About();  
  78.                         about.Show();  
  79.                     }  
  80.                     /**//// <summary>  
  81.                     /// 托盘退出菜单  
  82.                     /// </summary>  
  83.                     /// <param name="sender"></param>  
  84.                     /// <param name="e"></param>  
  85.                     private void menuExitClick(object sender, EventArgs e)  
  86.                     {  
  87.                         this.notifyIcon.Visible = false;  
  88.                         Application.Exit();  
  89.                     }  
  90.                     /**//// <summary>  
  91.                     /// 双击托盘图标显示主窗口  
  92.                     /// </summary>  
  93.                     /// <param name="sender"></param>  
  94.                     /// <param name="e"></param>  
  95.                     private void IconDoubleClick(object sender, EventArgs e)  
  96.                     {  
  97.                         this.Visible = true;  
  98.                         this.WindowState = FormWindowState.Normal;  
  99.                     }  
  100.                     /**//// <summary>  
  101.                     /// 隐藏主窗口  
  102.                     /// </summary>  
  103.                     /// <param name="sender"></param>  
  104.                     /// <param name="e"></param>  
  105.                     private void HideMainForm(object sender, EventArgs e)  
  106.                     {  
  107.                         this.WindowState = FormWindowState.Minimized;  
  108.                         this.Visible = false;  
  109.                     }  
  110.                     /**//// <summary>  
  111.                     /// 改变风格  
  112.                     /// </summary>  
  113.                     /// <param name="sender"></param>  
  114.                     /// <param name="e"></param>  
  115.                     private void ChangeSkin(object sender, EventArgs e)  
  116.                     {  
  117.                         string skinname = ((MenuItem)sender).Text;  
  118.                         SetSkin(skinname);  
  119.                         ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  120.                         appseting.UpdateKey("skin", skinname);  
  121.                     }  
  122.                     #endregion  
  123.         #endregion  
  124.   
  125.         Win32#region Win32  
  126.         [DllImport("user32")]  
  127.         static extern IntPtr SetCapture(IntPtr hwnd);  
  128.         [DllImport("user32")]  
  129.         static extern IntPtr ReleaseCapture();  
  130.         [DllImport("user32.dll")]  
  131.         static extern IntPtr WindowFromPoint(IntPtr xPoint, IntPtr yPoint);  
  132.         [DllImport("user32.dll")]  
  133.         static extern IntPtr GetDC(IntPtr hWnd);  
  134.   
  135.         [StructLayout(LayoutKind.Sequential)]  
  136.         public struct RECT  
  137.         {  
  138.             public int left;  
  139.             public int top;  
  140.             public int right;  
  141.             public int bottom;  
  142.             public int Width() { return right - left; }  
  143.             public int Height() { return bottom - top; }  
  144.         }  
  145.   
  146.         [DllImport("user32.dll")]  
  147.         // 注意,运行时知道如何列集一个矩形  
  148.         static extern IntPtr GetWindowRect(IntPtr hwnd, ref RECT rc);  
  149.         [DllImport("user32.dll")]  
  150.         static extern IntPtr PostMessage(IntPtr hWnd, IntPtr iMsg, IntPtr wParam, IntPtr lParam);  
  151.         #endregion  
  152.   
  153.         成员#region 成员  
  154.         private WMEncoder enc;  
  155.         IWMEncSourcePluginInfoManager wspim;  
  156.         Rectangle lastrect = new Rectangle(0, 0, 0, 0);  
  157.         private About about;  
  158.         public fScreen screen;  
  159.         private Thread th;  
  160.         private delegate void VoidDelegate();  
  161.         #endregion  
  162.   
  163.         /**////////////////////////////////////////////////////////////////////////////////////////////////  
  164.         /// <summary>  
  165.         /// 程序初始化  
  166.         /// </summary>  
  167.         private void AppInit()  
  168.         {  
  169.             enc = new WMEncoderClass();  
  170.             LoadSeting();  
  171.         }  
  172.         /**//// <summary>  
  173.         /// 设置皮肤  
  174.         /// </summary>  
  175.         /// <param name="SkinName"></param>  
  176.         private void SetSkin(string SkinName)  
  177.         {  
  178.             this.skin.SerialNumber = "";  
  179.             this.skin.SkinFile = "Skin//" + SkinName + ".ssk";  
  180.         }  
  181.         /**//// <summary>  
  182.         /// 初始压缩选项列表  
  183.         /// </summary>  
  184.         private void InitCompression()  
  185.         {  
  186.             //  
  187.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  188.             string Compression = appseting.ReadKeyValue("Compression");  
  189.             //  
  190.             enc = new WMEncoderClass();  
  191.             wspim = enc.SourcePluginInfoManager;  
  192.             wspim.Refresh();  
  193.             IWMEncProfileCollection wpfc = enc.ProfileCollection;  
  194.             IWMEncProfile wp;  
  195.             this.CompressionOptionListBox.Items.Clear();  
  196.             for (int i = 0; i < wpfc.Count; i++)  
  197.             {  
  198.                 wp = wpfc.Item(i);  
  199.                 this.CompressionOptionListBox.Items.Add(wp.Name);  
  200.                 if (wp.Name == Compression)  
  201.                     this.CompressionOptionListBox.SelectedIndex = i;  
  202.             }  
  203.   
  204.         }  
  205.         /**//// <summary>  
  206.         /// 加载设置  
  207.         /// </summary>  
  208.         private void LoadSeting()  
  209.         {  
  210.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  211.             //是否录制声音  
  212.            this.ChkSound.Checked =Convert.ToBoolean(appseting.ReadKeyValue("ChkSound"));  
  213.             //是否隐藏主窗体  
  214.            this.ChkHideMainForm.Checked = Convert.ToBoolean(appseting.ReadKeyValue("ChkHideMainForm"));  
  215.             //快捷键设置  
  216.            this.SkStartAndPause.Text = appseting.ReadKeyValue("SKey_StartAndPause");  
  217.            this.SkStop.Text = appseting.ReadKeyValue("SKey_Stop");  
  218.            this.SkShowAndHide.Text = appseting.ReadKeyValue("SKey_ShowAndHide");  
  219.             //录制区域  
  220.            int rect = Convert.ToInt32(appseting.ReadKeyValue("CameraRect"));  
  221.            if (rect == 1)  
  222.                this.RWindow.Checked = true;  
  223.            else  
  224.                this.RScreen.Checked = true;  
  225.             //  
  226.            Size sz = MainForm.GetScreenSize();  
  227.            this.rw.Text  = sz.Width.ToString();  
  228.            this.rh.Text = sz.Height.ToString();  
  229.         }  
  230.         /**//// <summary>  
  231.         /// 取得选择的压缩选项  
  232.         /// </summary>  
  233.         /// <returns></returns>  
  234.         private IWMEncProfile2 GetSelectCompressionOption()  
  235.         {  
  236.             IWMEncProfileCollection wpfc = enc.ProfileCollection;  
  237.             IWMEncProfile wp;  
  238.             IWMEncProfile2 wp2 = new WMEncProfile2Class();  
  239.             if (this.CompressionOptionListBox.SelectedIndex == -1)  
  240.             {  
  241.                 return null;  
  242.             }  
  243.             for (int i = 0; i < wpfc.Count; i++)  
  244.             {  
  245.                 wp = wpfc.Item(i);  
  246.   
  247.                 if (this.CompressionOptionListBox.SelectedItem.ToString() == wp.Name)  
  248.                 {  
  249.                     wp2.LoadFromIWMProfile(wp);  
  250.                     return wp2;  
  251.                 }  
  252.             }  
  253.             return null;  
  254.         }  
  255.         /**//// <summary>  
  256.         /// 开始录制  
  257.         /// </summary>  
  258.         private void StartCamera()  
  259.         {  
  260.             IWMEncSourceGroupCollection SrcGrpColl;  
  261.             IWMEncSourceGroup2 SrcGrp;  
  262.             IWMEncAudioSource SrcAud;  
  263.             IWMEncVideoSource2 SrcVid;  
  264.             IWMEncProfile2 Pro;  
  265.             enc = new WMEncoderClass();  
  266.             //-------------------------------------------  
  267.             try  
  268.             {  
  269.                 SrcGrpColl = enc.SourceGroupCollection;  
  270.                 SrcGrp = (IWMEncSourceGroup2)SrcGrpColl.Add("SG_1");  
  271.                 SrcVid = (IWMEncVideoSource2)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);  
  272.   
  273.                 //是否录制声音  
  274.                 if (this.ChkSound.Checked)  
  275.                 {  
  276.                     SrcAud = (IWMEncAudioSource)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);  
  277.                     SrcAud.SetInput("Default_Audio_Device""DEVICE""");  
  278.                 }  
  279.                 SrcVid.SetInput("ScreenCapture1""ScreenCap""");  
  280.   
  281.                 指定屏幕区域录制#region 指定屏幕区域录制  
  282.                 if (this.RectCustom.Checked)  
  283.                 {  
  284.                     Size priScreen = SystemInformation.PrimaryMonitorSize;  
  285.   
  286.                     float xx = (float)SrcVid.Height / (float)priScreen.Height;  
  287.                     float yy = (float)SrcVid.Width / (float)priScreen.Width;  
  288.   
  289.                     int ix = Convert.ToInt32(this.rx.Text);  
  290.                     int iy = Convert.ToInt32(this.ry.Text);  
  291.                     int ih = Convert.ToInt32(this.rh.Text);  
  292.                     int iw = Convert.ToInt32(this.rw.Text);  
  293.         
  294.                     SrcVid.CroppingLeftMargin = (int)((float)ix * yy);  
  295.                     SrcVid.CroppingTopMargin = (int)((float)iy * xx);  
  296.   
  297.                     if (SrcVid.CroppingLeftMargin >= priScreen.Width)  
  298.                     {  
  299.                         MessageBox.Show("屏幕区域外!");  
  300.                         return;  
  301.                     }  
  302.   
  303.                     if (SrcVid.CroppingTopMargin >= priScreen.Height)  
  304.                     {  
  305.                         MessageBox.Show("屏幕区域外!");  
  306.                         return;  
  307.                     }  
  308.   
  309.                     int ibm = priScreen.Height - ih - iy;  
  310.   
  311.                     if (ibm <= 0)  
  312.                     {  
  313.                         MessageBox.Show("屏幕区域外!");  
  314.                         return;  
  315.                     }  
  316.   
  317.                     int irm = priScreen.Width - iw - ix;  
  318.   
  319.                     if (irm <= 0)  
  320.                     {  
  321.                         MessageBox.Show("屏幕区域外!");  
  322.                         return;  
  323.                     }  
  324.                     SrcVid.CroppingRightMargin = (int)((float)irm * yy);  
  325.                     SrcVid.CroppingBottomMargin = (int)((float)ibm * xx);  
  326.                 }  
  327.  
  328.                 #endregion  
  329.   
  330.                 //确定压缩方式  
  331.                 Pro = GetSelectCompressionOption();  
  332.                 if (Pro == null)  
  333.                 {  
  334.                     MessageBox.Show("错误!请选中 视频压缩模板!");  
  335.                     return;  
  336.                 }  
  337.                 SrcGrp.set_Profile(Pro);  
  338.   
  339.                 //检查输出文件名是否为空  
  340.                 if (this.OutFilePathTextBox.Text.Length < 1)  
  341.                 {  
  342.                     MessageBox.Show("请指定保存路径!");  
  343.                     return;  
  344.                 }  
  345.   
  346.                 输出文件名#region 输出文件名  
  347.                 string sFile = this.OutFilePathTextBox.Text;  
  348.                 string sExt = sFile.Substring(sFile.LastIndexOf("."));  
  349.                 string[] sFileAr = sFile.Split(new char[] { //, / });  
  350.                 StringBuilder sb = new StringBuilder();  
  351.                 int i = 0;  
  352.                 for (i = 0; i < sFileAr.Length - 1; i++)  
  353.                 {  
  354.                     sb.Append(sFileAr[i]);  
  355.                     sb.Append("//");  
  356.                 }  
  357.                 string sPath = sb.ToString();  
  358.                 sFile = sFileAr[sFileAr.Length - 1];  
  359.                 sFile = sFile.Substring(0, sFile.LastIndexOf(.));  
  360.                 this.OutFilePathTextBox.Text = sPath + sFile + sExt;  
  361.   
  362.                 enc.File.LocalFileName = this.OutFilePathTextBox.Text;  
  363.                 #endregion  
  364.   
  365.                 enc.Start();  
  366.             }  
  367.             catch (Exception e1)  
  368.             {  
  369.                 MessageBox.Show(e1.Message);  
  370.             }  
  371.   
  372.         }  
  373.         /**////////////////////////////////////////////////////////////////////////////////////////////////  
  374.         /// <summary>  
  375.         /// 构造  
  376.         /// </summary>  
  377.         public MainForm()  
  378.         {  
  379.             InitializeComponent();  
  380.             //Control.CheckForIllegalCrossThreadCalls = false;  
  381.             //加载皮肤  
  382.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  383.             string skin = appseting.ReadKeyValue("skin");  
  384.             this.SetSkin(skin);  
  385.             //  
  386.             //初始托盘  
  387.             NotificationIcon();  
  388.             //  
  389.             //窗体大小固定  
  390.             this.MinimumSize=this.Size;  
  391.             this.MaximumSize = this.Size;  
  392.         }  
  393.         /**//// <summary>  
  394.         /// 主窗体加载事件  
  395.         /// </summary>  
  396.         /// <param name="sender"></param>  
  397.         /// <param name="e"></param>  
  398.         private void MainForm_Load(object sender, EventArgs e)  
  399.         {  
  400.             AppInit();  
  401.             InitCompression();  
  402.         }  
  403.         /**//// <summary>  
  404.         /// 选择输出文件保存路径  
  405.         /// </summary>  
  406.         /// <param name="sender"></param>  
  407.         /// <param name="e"></param>  
  408.         private void SelectOutPathButton_Click(object sender, EventArgs e)  
  409.         {  
  410.             SaveFileDialog sfd = new SaveFileDialog();  
  411.             sfd.Filter = "WMV文件(*.wmv)|*.wmv";  
  412.             sfd.RestoreDirectory = true;  
  413.             if (sfd.ShowDialog() == DialogResult.OK)  
  414.             {  
  415.                 this.OutFilePathTextBox.Text = sfd.FileName;  
  416.                 if(File.Exists(sfd.FileName))  
  417.                     File.Delete(sfd.FileName);  
  418.             }  
  419.         }  
  420.         /**//// <summary>  
  421.         /// 开始录制  
  422.         /// </summary>  
  423.         /// <param name="sender"></param>  
  424.         /// <param name="e"></param>  
  425.         private void StartButton_Click(object sender, EventArgs e)  
  426.         {  
  427.             if (this.CompressionOptionListBox.SelectedIndex > -1 && this.OutFilePathTextBox.Text.Trim() != "")  
  428.             {  
  429.                   
  430.                 th = new Thread( new ThreadStart(start));  
  431.                 th.IsBackground = true;  
  432.                 th.Start();  
  433.                 //将开始按钮不可用  
  434.                 this.StartButton.Enabled = false;  
  435.                 //暂停按钮可用  
  436.                 this.PauseButton.Enabled = true;  
  437.                 //  
  438.                 //窗体隐藏  
  439.                 if (this.ChkHideMainForm.Checked)  
  440.                 {  
  441.                     this.WindowState = FormWindowState.Minimized;  
  442.                     this.Visible = false;  
  443.                 }  
  444.             }  
  445.             else  
  446.             {  
  447.                 if (this.OutFilePathTextBox.Text.Trim() == "")  
  448.                     MessageBox.Show("请选择录像文件保存位置");  
  449.                 if (this.CompressionOptionListBox.SelectedIndex == -1)  
  450.                     MessageBox.Show("请选择一种压缩方案");  
  451.             }  
  452.         }  
  453.         private void start()  
  454.         {  
  455.             VoidDelegate dstart = new VoidDelegate(StartCamera);  
  456.             this.Invoke(dstart);  
  457.         }  
  458.         /**//// <summary>  
  459.         /// 停止录制按钮  
  460.         /// </summary>  
  461.         /// <param name="sender"></param>  
  462.         /// <param name="e"></param>  
  463.         private void StopButton_Click(object sender, EventArgs e)  
  464.         {  
  465.             th.Abort();  
  466.             enc.Stop();  
  467.             this.StartButton.Enabled = true;  
  468.             this.PauseButton.Enabled = false;  
  469.               
  470.         }  
  471.         /**//// <summary>  
  472.         /// 暂停录制按钮  
  473.         /// </summary>  
  474.         /// <param name="sender"></param>  
  475.         /// <param name="e"></param>  
  476.         private void PauseButton_Click(object sender, EventArgs e)  
  477.         {   
  478.             if (this.PauseButton.Text != "继续录制")  
  479.             {  
  480.                 enc.Pause();  
  481.                 this.PauseButton.Text = "继续录制";  
  482.             }  
  483.             else  
  484.             {  
  485.                 enc.Start();  
  486.                 this.PauseButton.Text = "暂停录制";  
  487.             }  
  488.         }  
  489.         /**//// <summary>  
  490.         /// 单击主窗体关闭按钮  
  491.         /// </summary>  
  492.         /// <param name="sender"></param>  
  493.         /// <param name="e"></param>  
  494.         private void MainForm_FormClosing(object sender, FormClosingEventArgs e)  
  495.         {  
  496.             if (e.CloseReason != CloseReason.ApplicationExitCall && e.CloseReason != CloseReason.WindowsShutDown)  
  497.             {  
  498.                 e.Cancel = true;  
  499.                 this.WindowState = FormWindowState.Minimized;  
  500.                 this.Visible= false;  
  501.             }  
  502.             else  
  503.             {  
  504.                 //none  
  505.             }  
  506.         }  
  507.         /**//// <summary>  
  508.         /// 压缩选项更改  
  509.         /// </summary>  
  510.         /// <param name="sender"></param>  
  511.         /// <param name="e"></param>  
  512.         private void CompressionOptionListBox_SelectedIndexChanged(object sender, EventArgs e)  
  513.         {  
  514.             ListBox lb = (ListBox)sender;  
  515.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  516.             appseting.UpdateKey("Compression", lb.Items[lb.SelectedIndex].ToString());  
  517.         }  
  518.         /**//// <summary>  
  519.         /// 压缩选项更改  
  520.         /// </summary>  
  521.         /// <param name="sender"></param>  
  522.         /// <param name="e"></param>  
  523.         private void CompressionOptionListBox_MouseClick(object sender, MouseEventArgs e)  
  524.         {  
  525.             ListBox lb = (ListBox)sender;  
  526.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  527.             appseting.UpdateKey("Compression", lb.Items[lb.SelectedIndex].ToString());  
  528.         }  
  529.   
  530.         快捷键#region 快捷键  
  531.         /**//// <summary>  
  532.         /// 快捷键  
  533.         /// </summary>  
  534.         /// <param name="sender"></param>  
  535.         /// <param name="e"></param>  
  536.         private void SkStartAndPause_TextChanged(object sender, EventArgs e)  
  537.         {  
  538.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  539.             appseting.UpdateKey("SKey_StartAndPause"this.SkStartAndPause.Text);  
  540.         }  
  541.         //  
  542.         private void SkStop_TextChanged(object sender, EventArgs e)  
  543.         {  
  544.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  545.             appseting.UpdateKey("SKey_Stop"this.SkStartAndPause.Text);  
  546.         }  
  547.         //  
  548.         private void SkShowAndHide_TextChanged(object sender, EventArgs e)  
  549.         {  
  550.   
  551.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  552.             appseting.UpdateKey("SKey_ShowAndHide"this.SkShowAndHide.Text);  
  553.         }  
  554.         #endregion  
  555.   
  556.         录制区域#region 录制区域  
  557.         private void RScreen_CheckedChanged(object sender, EventArgs e)  
  558.         {  
  559.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  560.             appseting.UpdateKey("CameraRect""0");  
  561.         }  
  562.         private void RWindow_CheckedChanged(object sender, EventArgs e)  
  563.         {  
  564.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  565.             appseting.UpdateKey("CameraRect""1");  
  566.         }  
  567.         /**//// <summary>  
  568.         /// 指定屏幕区域  
  569.         /// </summary>  
  570.         /// <param name="sender"></param>  
  571.         /// <param name="e"></param>  
  572.         private void RectCustom_Click(object sender, EventArgs e)  
  573.         {  
  574.             if (this.RectCustom.Checked)  
  575.             {  
  576.                 screen = new fScreen();  
  577.                 this.Hide();  
  578.                 this.WindowState = FormWindowState.Minimized;  
  579.                 System.Threading.Thread.Sleep(220);  
  580.   
  581.                 //创建一个和屏幕一样大的Bitmap  
  582.                 Size size = MainForm.GetScreenSize();  
  583.                 Image myImage = new Bitmap(size.Width, size.Height);  
  584.                 //从一个继承自Image类的对象中创建Graphics对象  
  585.                 Graphics gc = Graphics.FromImage(myImage);  
  586.                 gc.SmoothingMode = SmoothingMode.HighQuality;  
  587.                 //抓屏并拷贝到myimage里  
  588.                 gc.CopyFromScreen(new Point(0, 0), new Point(0, 0), size);  
  589.                 //保存为文件  
  590.                 screen.BackgroundImage = myImage;  
  591.                 screen.WindowState = FormWindowState.Maximized;  
  592.                 screen.TopMost = true;  
  593.                 screen.ShowDialog();  
  594.                 screen.Focus();  
  595.                 screen.TopMost = false;  
  596.                 screen.Refresh();  
  597.   
  598.                 myImage.Dispose();  
  599.                 gc.Dispose();  
  600.             }  
  601.             //  
  602.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  603.             appseting.UpdateKey("CameraRect""2");  
  604.         }  
  605.         #endregion  
  606.         /**//// <summary>  
  607.         /// 是否录声音  
  608.         /// </summary>  
  609.         /// <param name="sender"></param>  
  610.         /// <param name="e"></param>  
  611.         private void ChkSound_CheckedChanged(object sender, EventArgs e)  
  612.         {  
  613.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  614.             appseting.UpdateKey("ChkSound"this.ChkSound.Checked.ToString());  
  615.         }  
  616.         //是否隐藏主窗体  
  617.         private void ChkHideMainForm_CheckedChanged(object sender, EventArgs e)  
  618.         {  
  619.             ConfigManager.AppSetings appseting = new ConfigManager.AppSetings();  
  620.             appseting.UpdateKey("ChkHideMainForm"this.ChkHideMainForm.Checked.ToString());  
  621.         }  
  622.   
  623.         /**///////////////////////////////////////////////////////////////////////////////  
  624.         /// <summary>  
  625.         /// 取得屏幕大小  
  626.         /// </summary>  
  627.         /// <returns></returns>  
  628.         public static Size GetScreenSize()  
  629.         {  
  630.             //获得当前屏幕的分辨率  
  631.             Screen scr = Screen.PrimaryScreen;  
  632.             Rectangle rc = scr.Bounds;  
  633.             Size size = new Size(rc.Width, rc.Height);  
  634.             return size;  
  635.         }  

 原文链接:http://blog.csdn.net/gisfarmer/article/details/4382999