• 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
            
        }
  • 相关阅读:
    Python基础之内存管理与垃圾回收机制
    Git常用命令
    Git分支操作
    码云配置SSH公钥
    Git基本操作
    Git基本理论
    版本控制
    Python基础之Python语法
    成为一名JAVA高级工程师你需要学什么【转】
    一个java高级工程师的进阶之路【转】
  • 原文地址:https://www.cnblogs.com/xiaoyu369/p/4159652.html
Copyright © 2020-2023  润新知