侧边栏壁纸
博主头像
怪客のBlog 博主等级

行动起来,活在当下

  • 累计撰写 35 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录
C#

WinForm实现自定义屏幕截图工具

怪客
2022-04-01 / 0 评论 / 0 点赞 / 634 阅读 / 0 字

界面设计

主界面

主窗体,添加一个打开截图功能的按钮
image
添加事件,打开截图窗口

        private void btnScreen_Click(object sender, EventArgs e)
        {
            FrmScreen frmScreen = new FrmScreen();
            frmScreen.Show();
        }

截屏界面

一个截屏窗口(无边框窗体),包括坐标统计,尺寸统计显示标签以及相关操作按钮
image-1662360855288

        Point startPos;//截图开始位置的坐标
        Point currentPos;//截图结束位置的坐标
        bool drawing;//是否绘制
        Rectangle? rectangle = null;//绘制的矩形
        public FrmScreen()
        {
            InitializeComponent();
            //设置为顶级窗口
            TopMost = true;
            //设置窗体透明
            this.BackColor = Color.Blue;
            this.TransparencyKey = Color.Blue;
            //窗体最大化显示
            WindowState = FormWindowState.Maximized;
            //无边框
            FormBorderStyle = FormBorderStyle.None;
            //开始双缓冲,解决窗体闪烁
            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
            this.UpdateStyles();
            //默认不显示操作栏、尺寸信息、坐标信息
            plOper.Visible = false;
            lblSize.Visible = false;
            lblPosition.Visible = false;
        }

代码逻辑

辅助方法

        /// <summary>
        /// 获取当前截取的矩形
        /// </summary>
        /// <returns></returns>
        private Rectangle getRectangle()
        {
            return new Rectangle(
                Math.Min(startPos.X, currentPos.X),
                Math.Min(startPos.Y, currentPos.Y),
                Math.Abs(startPos.X - currentPos.X),
                Math.Abs(startPos.Y - currentPos.Y));
        }
        
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="image"></param>
        private void SaveImage(Image image)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = ".jpg|*.jpg|.png|*.png";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string fileName = saveFileDialog.FileName;
                image.Save(fileName, ImageFormat.Bmp);
            }
        }

窗体重绘事件(绘制截取的矩形框)

        private void FrmScreen_Paint(object sender, PaintEventArgs e)
        {
            var screen = Screen.PrimaryScreen.Bounds;
            lblSize.Text = $"{getRectangle().Width}*{getRectangle().Height}";
            lblPosition.Location = new Point(currentPos.X - lblPosition.Width - 2, currentPos.Y - lblPosition.Height - 2);
            lblPosition.Text = $"X:{currentPos.X},Y:{currentPos.Y}";
            Pen pen = new Pen(Color.Red);
            pen.Width = 2;
            if (rectangle != null)
                e.Graphics.DrawRectangle(pen, rectangle.Value);
            if (drawing)
                e.Graphics.DrawRectangle(pen, getRectangle());
        }

监听截屏窗体鼠标左键按下事件

  1. 记录开始坐标
  2. 开始绘制
  3. 显示尺寸、坐标统计信息
        private void FrmScreen_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (rectangle != null)
                    return;
                currentPos = startPos = e.Location;
                drawing = true;
                lblSize.Location = new Point(currentPos.X + 2, currentPos.Y + 2);
                lblSize.Visible = true;
                lblPosition.Visible = true;
            }
        }

监听鼠标按下后的移动事件

        private void FrmScreen_MouseMove(object sender, MouseEventArgs e)
        {
            currentPos = e.Location;
            if (drawing)//鼠标按下状态
                this.Invalidate();//重绘窗体,绘制图形
        }

鼠标左键释放,完成截图区域选取

        private void FrmScreen_MouseUp(object sender, MouseEventArgs e)
        {
            lblPosition.Visible = false;
            lblSize.Visible = false;
            if (drawing)~~
            {
                var screen = Screen.PrimaryScreen.Bounds;
                drawing = false;
                var rect = getRectangle();
                if (rect.Width > 0 && rect.Height > 0)
                    rectangle = rect;
                this.Invalidate();
                var r = rectangle.Value;
                plOper.Location = new Point(r.X, r.Bottom + 2);
                if (r.Bottom >= screen.Height - plOper.Height)
                    plOper.Location = new Point(r.X, screen.Height - plOper.Height - 2);
                plOper.Show();
            }
        }

点击取消按钮,退出截屏窗口

        private void btnCancel_Click(object sender, EventArgs e)
        {
            lblPosition.Visible = false;
            lblSize.Visible = false;
            this.Close();
        }

点击保存按钮,截图另存为文件

        private void btnSave_Click(object sender, EventArgs e)
        {
            var r = rectangle.Value;
            //创建图象,保存将来截取的图象
            Bitmap image = new Bitmap(r.Width, r.Height);
            Graphics imgGraphics = Graphics.FromImage(image);
            imgGraphics.SmoothingMode = SmoothingMode.HighQuality;
            imgGraphics.CompositingQuality = CompositingQuality.HighQuality;
            imgGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            //设置截屏区域
            imgGraphics.CopyFromScreen(r.X, r.Y, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
            SaveImage(image);
            this.Close();
        }

点击复制按钮,复制屏幕截图到粘贴板

        private void btnCopy_Click(object sender, EventArgs e)
        {
            var r = rectangle.Value;
            //创建图象,保存将来截取的图象
            Bitmap image = new Bitmap(r.Width, r.Height);
            Graphics imgGraphics = Graphics.FromImage(image);
            imgGraphics.SmoothingMode = SmoothingMode.HighQuality;
            imgGraphics.CompositingQuality = CompositingQuality.HighQuality;
            imgGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            //设置截屏区域 柯乐义
            imgGraphics.CopyFromScreen(r.X, r.Y, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
            Clipboard.SetData(DataFormats.Bitmap, image);
            this.Close();
        }

实现效果

点击开始截图,然后选择截图范围
image-1662361036760
松开鼠标显示操作按钮
image-1662361148533
点击保存,保存后的文件
image-1662361212909

0

评论区