C# 截屏

    /// <summary>
    /// 辅助类
    /// </summary>
    public class ScreenHelper
    {
        //文件保存
        private static string _filePath = string.Empty;

        #region 构造函数
        /// <summary>
        /// 私有构造函数
        /// </summary>
        private ScreenHelper()
        {

        }
        /// <summary>
        /// 静态构造函数
        /// </summary>
        static ScreenHelper()
        {
            _filePath = System.Environment.CurrentDirectory;

            string filePath = System.Configuration.ConfigurationManager.AppSettings["ScreenshotFilePath"];
            if (!string.IsNullOrEmpty(filePath))
            {
                _filePath = filePath;
            }
            
        }

        /// <summary>
        /// 截图
        /// </summary>
        /// <param name="fileName">文件名,不包含后缀</param>
        /// <param name="filePath">文件路径</param>
        public static string Screenshot(string fileName,string filePath)
        {
            try
            {
                string dtString = DateTime.Now.ToString("yyyy-MM-dd");
                string dir = filePath + "\Screenshot\" + dtString + "\";

                //判断文件夹是否存在,不存在则创建
                if (System.IO.Directory.Exists(dir) == false)
                {
                    System.IO.Directory.CreateDirectory(dir);
                }

                string fullPath = dir + fileName + "_" + Guid.NewGuid().ToString() + ".jpg";

                //获得当前屏幕的分辨率
                Screen scr = Screen.PrimaryScreen;
                Rectangle rc = scr.Bounds;
                int iWidth = rc.Width;
                int iHeight = rc.Height;
                //创建一个和屏幕一样大的Bitmap
                Image myImage = new Bitmap(iWidth, iHeight);
                //从一个继承自Image类的对象中创建Graphics对象
                Graphics g = Graphics.FromImage(myImage);
                //抓屏并拷贝到myimage里
                g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
                //保存为文件
                myImage.Save(fullPath);

                return fullPath;
            }
            catch (Exception ex)
            {
                Com.LogHelper.WriteLog(ex.Message, ex.StackTrace, Com.LogType.OtherError);

                return string.Empty;
            }
            
        }

        /// <summary>
        /// 截图
        /// </summary>
        /// <param name="fileName">文件名,不包含后缀名</param>
        public static string Screenshot(string fileName)
        {
            return Screenshot(fileName, _filePath);
        }
        #endregion
        
    }
原文地址:https://www.cnblogs.com/xiaoyu369/p/4159652.html