ActiveX网页截图插件

Posted tlmbem

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActiveX网页截图插件相关的知识,希望对你有一定的参考价值。

技术图片

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

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

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

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

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

        /// <summary>
        /// 鼠标的类型
        /// </summary>
        public ScreenMoveTypes ScreenMoveType = ScreenMoveTypes.Normal;
        /// <summary>
        /// 鼠标按下的坐标
        /// </summary>
        public Point MoveDownPoint;
        /// <summary>
        /// 鼠标移动时的上一次坐标
        /// </summary>
        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);
        }

        /// <summary>
        /// 键盘监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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;
                    }
            }
        }

        /// <summary>
        /// 初始化截图边框信息
        /// </summary>
        /// <param name="moveStartPoint">截图边框开始坐标</param>
        /// <param name="moveEndPoint">截图边框结束坐标</param>
        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);
        }
        /// <summary>
        /// 初始化截图边框点信息
        /// </summary>
        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);
        }
        /// <summary>
        /// 缩放截图边框
        /// </summary>
        /// <param name="moveStartPoint">缩放前鼠标坐标</param>
        /// <param name="moveEndPoint">缩放后鼠标坐标</param>
        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

        }
        /// <summary>
        /// 获取截图边框在屏幕的允许坐标中
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        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);
        }

        /// <summary>
        /// 打开截屏
        /// </summary>
        public void OpenScreen()
        {
            this.captureStatus = CaptureStatus.Open;
            this.icoForm.Show();
            this.screenForm.Hide();
            this.icoForm.LoadLocation();
            this.HookObject.Hook_Start();
        }
        /// <summary>
        /// 关闭截屏
        /// </summary>
        public void ColseScreen()
        {
            this.CatRect = null;
            this.DisposeImage(true, true);
            this.HookObject.Hook_Clear();
            this.icoForm.Hide();
            this.screenForm.Hide();
            this.captureStatus = CaptureStatus.Close;
        }
        /// <summary>
        /// 截屏
        /// </summary>
        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;
        }

        /// <summary>
        /// 释放截取的图片
        /// </summary>
        /// <param name="Original">是否释放原图</param>
        /// <param name="Mask">是否释放蒙版图</param>
        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;
            }
        }
        /// <summary>
        /// 获取截取的图片
        /// </summary>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 获取截取的图片64为字符串
        /// </summary>
        /// <param name="bmp">截取的图片</param>
        /// <returns></returns>
        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;
        }

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

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

以上是关于ActiveX网页截图插件的主要内容,如果未能解决你的问题,请参考以下文章

使用VLC Activex插件做网页版视频播放器

ActiveX网页信息加密

如何将做好的delphi放入qq空间或网页中得到应用???急

C# ActiveX 网页打包验证自动升级

C# ActiveX 网页打包验证自动升级

龙芯推出兼容IE的龙芯浏览器解决方案,全面支持ActiveX插件等应用类型