ActiveX网页截图插件

2021-04-13 19:26

阅读:647

标签:核心   mamicode   允许   rgb   copy   inf   下载地址   else   窗体   

技术图片

该插件目前只提供截图功能,你可以继续优化截图的编辑功能。

该插件使用js调用OpenScreen方法后启动截图功能,在截图功能启动后按键盘C键抓取屏幕,然后选中截取部分后双击选取将会完成截图,鼠标的右键为取消选中区域和取消已抓取的的屏幕。双击桌面右下角的文字可以关闭截图功能。

   /// 
    /// 截图核心
    /// 
    [Description("截图核心")]
    public class ScreenCore
    {
        #region
        /// 
        /// 截图ActiveX
        /// 
        public ActiveXScreen activeXScreen = null;
        /// 
        /// 关闭窗体
        /// 
        internal IcoForm icoForm = null;
        /// 
        /// 截屏窗体
        /// 
        internal ScreenForm screenForm = null;
        /// 
        /// 截图状态
        /// 
        public CaptureStatus captureStatus = CaptureStatus.Close;
        /// 
        /// 键盘钩
        /// 
        public HookUilt HookObject = null;

        /// 
        /// 要截取的屏幕
        /// 
        public Screen screen = null;
        /// 
        /// 原始截图
        /// 
        public Bitmap OriginalScreen = null;
        /// 
        /// 蒙版截图
        /// 
        public Bitmap MaskScreen = null;

        /// 
        /// 截图边框厚度
        /// 
        public int CatBoder = 1;
        /// 
        /// 截图边框颜色
        /// 
        public Color CatBodeColor = Color.YellowGreen;
        /// 
        /// 截图边框点的外边距
        /// 
        public int CatBoderDotMargin = 3;
        /// 
        /// 截图边框Rect
        /// 
        public ImageRectangle CatRect = null;

        /// 
        /// 鼠标的类型
        /// 
        public ScreenMoveTypes ScreenMoveType = ScreenMoveTypes.Normal;
        /// 
        /// 鼠标按下的坐标
        /// 
        public Point MoveDownPoint;
        /// 
        /// 鼠标移动时的上一次坐标
        /// 
        public Point MoveStartPoint;

        #endregion

        public ScreenCore(ActiveXScreen _activeXScreen)
        {
            this.activeXScreen = _activeXScreen;

            this.icoForm = new IcoForm(this);
            this.screenForm = new ScreenForm(this);

            this.HookObject = new HookUilt();
            this.HookObject.KeyDownHandler += new KeyEventHandler(this.Screen_KeyDown);
        }

        /// 
        /// 键盘监听
        /// 
        /// 
        /// 
        private void Screen_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.C://截图
                    {
                        if (this.captureStatus == CaptureStatus.Open)
                        {
                            this.CaptureScreen();
                        }
                        break;
                    }
                case Keys.Up://上移
                    {
                        if (this.captureStatus == CaptureStatus.Capture && this.IsCapture())
                        {
                            this.CatRect.Rect = new Rectangle(this.GetRectanglePoint(this.CatRect.Rect.X, this.CatRect.Rect.Y - 1), this.CatRect.Rect.Size);
                            this.InitializeRectangleDot();
                            this.screenForm.Invalidate();
                        }
                        break;
                    }
                case Keys.Down://下移
                    {
                        if (this.captureStatus == CaptureStatus.Capture && this.IsCapture())
                        {
                            this.CatRect.Rect = new Rectangle(this.GetRectanglePoint(this.CatRect.Rect.X, this.CatRect.Rect.Y + 1), this.CatRect.Rect.Size);
                            this.InitializeRectangleDot();
                            this.screenForm.Invalidate();
                        }
                        break;
                    }
                case Keys.Left://左移
                    {
                        if (this.captureStatus == CaptureStatus.Capture && this.IsCapture())
                        {
                            this.CatRect.Rect = new Rectangle(this.GetRectanglePoint(this.CatRect.Rect.X - 1, this.CatRect.Rect.Y), this.CatRect.Rect.Size);
                            this.InitializeRectangleDot();
                            this.screenForm.Invalidate();
                        }
                        break;
                    }
                case Keys.Right://右移
                    {
                        if (this.captureStatus == CaptureStatus.Capture && this.IsCapture())
                        {
                            this.CatRect.Rect = new Rectangle(this.GetRectanglePoint(this.CatRect.Rect.X + 1, this.CatRect.Rect.Y), this.CatRect.Rect.Size);
                            this.InitializeRectangleDot();
                            this.screenForm.Invalidate();
                        }
                        break;
                    }
            }
        }

        /// 
        /// 初始化截图边框信息
        /// 
        /// 截图边框开始坐标
        /// 截图边框结束坐标
        public void InitializeRectangle(Point moveStartPoint, Point moveEndPoint)
        {
            Point point = new Point(Math.Min(moveStartPoint.X, moveEndPoint.X), Math.Min(moveStartPoint.Y, moveEndPoint.Y));
            Size size = new Size(Math.Abs(moveStartPoint.X - moveEndPoint.X), Math.Abs(moveStartPoint.Y - moveEndPoint.Y));
            this.CatRect.Rect = new Rectangle(point, size);
        }
        /// 
        /// 初始化截图边框点信息
        /// 
        public void InitializeRectangleDot()
        {
            int dot_width = this.CatBoderDotMargin * 2;
            this.CatRect.TopLeftRect = new Rectangle(this.CatRect.Rect.X - this.CatBoderDotMargin, this.CatRect.Rect.Y - this.CatBoderDotMargin, dot_width, dot_width);
            this.CatRect.TopMiddleRect = new Rectangle(this.CatRect.Rect.X + this.CatRect.Rect.Width / 2 - this.CatBoderDotMargin, this.CatRect.Rect.Y - this.CatBoderDotMargin, dot_width, dot_width);
            this.CatRect.TopRightRect = new Rectangle(this.CatRect.Rect.Right - this.CatBoderDotMargin, this.CatRect.Rect.Y - this.CatBoderDotMargin, dot_width, dot_width);
            this.CatRect.BottomLeftRect = new Rectangle(this.CatRect.Rect.X - this.CatBoderDotMargin, this.CatRect.Rect.Bottom - this.CatBoderDotMargin, dot_width, dot_width);
            this.CatRect.BottomMiddleRect = new Rectangle(this.CatRect.Rect.X + this.CatRect.Rect.Width / 2 - this.CatBoderDotMargin, this.CatRect.Rect.Bottom - this.CatBoderDotMargin, dot_width, dot_width);
            this.CatRect.BottomRightRect = new Rectangle(this.CatRect.Rect.Right - this.CatBoderDotMargin, this.CatRect.Rect.Bottom - this.CatBoderDotMargin, dot_width, dot_width);
            this.CatRect.MiddleLeftRect = new Rectangle(this.CatRect.Rect.X - this.CatBoderDotMargin, this.CatRect.Rect.Y + this.CatRect.Rect.Height / 2 - this.CatBoderDotMargin, dot_width, dot_width);
            this.CatRect.MiddleRightRect = new Rectangle(this.CatRect.Rect.Right - this.CatBoderDotMargin, this.CatRect.Rect.Y + this.CatRect.Rect.Height / 2 - this.CatBoderDotMargin, dot_width, dot_width);
        }
        /// 
        /// 缩放截图边框
        /// 
        /// 缩放前鼠标坐标
        /// 缩放后鼠标坐标
        public void ZoonRectangle(Point moveStartPoint, Point moveEndPoint)
        {
            int x = moveEndPoint.X - moveStartPoint.X;
            int y = moveEndPoint.Y - moveStartPoint.Y;

            #region 更新鼠标类型
            if (this.ScreenMoveType == ScreenMoveTypes.DownZoonTopLeft)
            {
                this.CatRect.Rect = new Rectangle(this.CatRect.Rect.X + x, this.CatRect.Rect.Y + y, this.CatRect.Rect.Width - x, this.CatRect.Rect.Height - y);
                if (this.CatRect.Rect.Width 0 && this.CatRect.Rect.Height 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonBottomRight;
                }
                else if (this.CatRect.Rect.Width 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonTopRight;
                }
                else if (this.CatRect.Rect.Height 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonBottomLeft;
                }
            }
            else if (this.ScreenMoveType == ScreenMoveTypes.DownZoonTopMiddle)
            {
                this.CatRect.Rect = new Rectangle(this.CatRect.Rect.X, this.CatRect.Rect.Y + y, this.CatRect.Rect.Width, this.CatRect.Rect.Height - y);
                if (this.CatRect.Rect.Height 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonBottomMiddle;
                }
            }
            else if (this.ScreenMoveType == ScreenMoveTypes.DownZoonTopRight)
            {
                this.CatRect.Rect = new Rectangle(this.CatRect.Rect.X, this.CatRect.Rect.Y + y, this.CatRect.Rect.Width + x, this.CatRect.Rect.Height - y);
                if (this.CatRect.Rect.Width 0 && this.CatRect.Rect.Height 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonBottomLeft;
                }
                else if (this.CatRect.Rect.Width 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonTopLeft;
                }
                else if (this.CatRect.Rect.Height 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonBottomRight;
                }
            }
            else if (this.ScreenMoveType == ScreenMoveTypes.DownZoonBottomLeft)
            {
                this.CatRect.Rect = new Rectangle(this.CatRect.Rect.X + x, this.CatRect.Rect.Y, this.CatRect.Rect.Width - x, this.CatRect.Rect.Height + y);
                if (this.CatRect.Rect.Width 0 && this.CatRect.Rect.Height 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonTopRight;
                }
                else if (this.CatRect.Rect.Width 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonBottomRight;
                }
                else if (this.CatRect.Rect.Height 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonTopLeft;
                }
            }
            else if (this.ScreenMoveType == ScreenMoveTypes.DownZoonBottomMiddle)
            {
                this.CatRect.Rect = new Rectangle(this.CatRect.Rect.X, this.CatRect.Rect.Y, this.CatRect.Rect.Width, this.CatRect.Rect.Height + y);
                if (this.CatRect.Rect.Height 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonTopMiddle;
                }
            }
            else if (this.ScreenMoveType == ScreenMoveTypes.DownZoonBottomRight)
            {
                this.CatRect.Rect = new Rectangle(this.CatRect.Rect.X, this.CatRect.Rect.Y, this.CatRect.Rect.Width + x, this.CatRect.Rect.Height + y);
                if (this.CatRect.Rect.Width 0 && this.CatRect.Rect.Height 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonTopLeft;
                }
                else if (this.CatRect.Rect.Width 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonBottomLeft;
                }
                else if (this.CatRect.Rect.Height 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonTopRight;
                }
            }
            else if (this.ScreenMoveType == ScreenMoveTypes.DownZoonMiddleLeft)
            {
                this.CatRect.Rect = new Rectangle(this.CatRect.Rect.X + x, this.CatRect.Rect.Y, this.CatRect.Rect.Width - x, this.CatRect.Rect.Height);
                if (this.CatRect.Rect.Width 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonMiddleRight;
                }

            }
            else if (this.ScreenMoveType == ScreenMoveTypes.DownZoonMiddleRight)
            {
                this.CatRect.Rect = new Rectangle(this.CatRect.Rect.X, this.CatRect.Rect.Y, this.CatRect.Rect.Width + x, this.CatRect.Rect.Height);
                if (this.CatRect.Rect.Width 0)
                {
                    this.ScreenMoveType = ScreenMoveTypes.DownZoonMiddleLeft;
                }
            }
            #endregion

            #region 更新截图边框防止匡高出现负数
            int x_tmp = this.CatRect.Rect.X;
            int y_tmp = this.CatRect.Rect.Y;
            int width_tmp = this.CatRect.Rect.Width;
            int height_tmp = this.CatRect.Rect.Height;
            if (this.CatRect.Rect.Width 0)
            {
                width_tmp = -1 * this.CatRect.Rect.Width;
                x_tmp = this.CatRect.Rect.X - width_tmp;
            }
            if (this.CatRect.Rect.Height 0)
            {
                height_tmp = -1 * this.CatRect.Rect.Height;
                y_tmp = this.CatRect.Rect.Y - height_tmp;
            }
            this.CatRect.Rect = new Rectangle(x_tmp, y_tmp, width_tmp, height_tmp);
            #endregion

        }
        /// 
        /// 获取截图边框在屏幕的允许坐标中
        /// 
        /// 
        /// 
        /// 
        public Point GetRectanglePoint(int x, int y)
        {
            if (x this.screen.Bounds.X)
                x = this.screen.Bounds.X;
            if (x > this.screen.Bounds.Right - this.CatRect.Rect.Width)
                x = this.screen.Bounds.Right - this.CatRect.Rect.Width;

            if (y this.screen.Bounds.Y)
                y = this.screen.Bounds.Y;
            if (y > this.screen.Bounds.Bottom - this.CatRect.Rect.Height)
                y = this.screen.Bounds.Bottom - this.CatRect.Rect.Height;
            return new Point(x, y);
        }

        /// 
        /// 打开截屏
        /// 
        public void OpenScreen()
        {
            this.captureStatus = CaptureStatus.Open;
            this.icoForm.Show();
            this.screenForm.Hide();
            this.icoForm.LoadLocation();
            this.HookObject.Hook_Start();
        }
        /// 
        /// 关闭截屏
        /// 
        public void ColseScreen()
        {
            this.CatRect = null;
            this.DisposeImage(true, true);
            this.HookObject.Hook_Clear();
            this.icoForm.Hide();
            this.screenForm.Hide();
            this.captureStatus = CaptureStatus.Close;
        }
        /// 
        /// 截屏
        /// 
        private void CaptureScreen()
        {
            this.icoForm.Hide();
            this.screenForm.Hide();
            this.screen = Screen.FromControl(this.screenForm);
            this.screenForm.BackgroundImage = null;
            this.DisposeImage(true, true);

            //原图
            this.OriginalScreen = new Bitmap(this.screen.Bounds.Width, this.screen.Bounds.Height);
            Graphics g = Graphics.FromImage(this.OriginalScreen);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), this.screen.Bounds.Size);

            //蒙版图
            this.MaskScreen = (Bitmap)this.OriginalScreen.Clone();
            g = Graphics.FromImage(this.MaskScreen);
            SolidBrush mask_sb = new SolidBrush(Color.FromArgb(150, 1, 1, 1));
            g.FillRectangle(mask_sb, this.screen.Bounds);

            mask_sb.Dispose();
            g.Dispose();

            this.screenForm.BackgroundImage = (Image)this.MaskScreen;
            this.screenForm.Location = this.screen.Bounds.Location;
            this.screenForm.Size = this.screen.Bounds.Size;
            this.screenForm.Show();

            this.captureStatus = CaptureStatus.Capture;
        }

        /// 
        /// 释放截取的图片
        /// 
        /// 是否释放原图
        /// 是否释放蒙版图
        public void DisposeImage(bool Original, bool Mask)
        {
            if (Original && this.OriginalScreen != null)
            {
                this.OriginalScreen.Dispose();
                this.OriginalScreen = null;
            }
            if (Mask && this.MaskScreen != null)
            {
                this.MaskScreen.Dispose();
                this.MaskScreen = null;
            }
        }
        /// 
        /// 获取截取的图片
        /// 
        /// 
        public Bitmap GetCaptureImage()
        {
            Bitmap captureImage = new Bitmap(this.CatRect.Rect.Width, this.CatRect.Rect.Height);//选中截图
            using (Graphics g = Graphics.FromImage(captureImage))
            {
                g.DrawImage(this.OriginalScreen, 0, 0, this.CatRect.Rect, GraphicsUnit.Pixel);
                return captureImage;
            }
        }
        /// 
        /// 获取截取的图片64为字符串
        /// 
        /// 截取的图片
        /// 
        public string GetCaptureImageBase64String(Bitmap bmp)
        {
            string captureImageBase64 = "data:image/jpeg;base64,";
            using (MemoryStream ms = new MemoryStream())
            {
                bmp.Save(ms, ImageFormat.Jpeg);
                captureImageBase64 += Convert.ToBase64String(ms.ToArray());
            }
            return captureImageBase64;
        }

        /// 
        /// 是否有截图边框
        /// 
        /// 
        public bool IsCapture()
        {
            if (this.CatRect == null || this.CatRect.Rect.Width 1 || this.CatRect.Rect.Height 1)
            {
                return false;
            }
            return true;
        }
    }

源码下载地址:ActiveX网页截图插件.zip

ActiveX网页截图插件

标签:核心   mamicode   允许   rgb   copy   inf   下载地址   else   窗体   

原文地址:https://www.cnblogs.com/tlmbem/p/12384592.html

上一篇:vue.js

下一篇:beego客户端请求httplib


评论


亲,登录后才可以留言!