zoukankan      html  css  js  c++  java
  • 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
            
        }
  • 相关阅读:
    MRC与ARC两种模式的相互兼容
    解决获取的手机序列号卸载应用重新安装时序列号变动的问题
    屏幕截图
    图片右下角显示水印
    在一个工程中建两个端口(司机端和货主端)
    左右侧边栏抽屉效果
    第二阶段第一天站立会议总结
    第十天站立会议总结
    第九天站立会议总结
    第八天站立会议总结
  • 原文地址:https://www.cnblogs.com/xiaoyu369/p/4159652.html
Copyright © 2011-2022 走看看