C#实现对任意区域任意大小的截图

1,目的:

  • 实现类似系统截图工具那样对屏幕任何区域自定义大小的截图。

2,效果展示:

  • 点击截图
  • 选择需要截图的区域:
  • 区域选择完成后,单击右键完成截图:
  • 在合适的载体上粘贴截图:

3,代码:

cs 复制代码
 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            //获取屏幕截屏
            Bitmap bcgImg = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            using (Graphics g= Graphics.FromImage(bcgImg))
            {
                g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(bcgImg.Width, bcgImg.Height));

            }
            //将图片传给截图窗口
            CaptureFrm frm = new CaptureFrm(bcgImg);
            frm.TopMost = true;
           if( frm.ShowDialog()== DialogResult.OK)
            {
                MessageBox.Show("截图已保存至剪贴板,请选择合适的载体进行粘贴!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("取消截图","提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            this.WindowState = FormWindowState.Normal;
        }
    }
cs 复制代码
public partial class CaptureFrm : Form
    {
        Bitmap bcgImg;
        bool drawingFlag=false;
        Point startPoint;
        Point endPoint;
        Graphics main_g;
        bool isCapture=false;
        public CaptureFrm(Bitmap img)
        {
            InitializeComponent();
            bcgImg = img;
        }

        private void CaptureFrm_Load(object sender, EventArgs e)
        {
            //背景
            this.BackgroundImage = bcgImg;
            this.BackgroundImageLayout = ImageLayout.Stretch;

        }

        private void CaptureFrm_KeyDown(object sender, KeyEventArgs e)
        {
            //如果按下ESC键则退出
            if (e.KeyCode == Keys.Escape)
            {
                //  this.Close();
                DialogResult = DialogResult.Cancel;
            }
        }

        private void CaptureFrm_MouseDown(object sender, MouseEventArgs e)
        {
          if(e.Button== MouseButtons.Left)
            {
                drawingFlag = true;
                startPoint = e.Location;
                main_g = this.CreateGraphics();
            }
          if(e.Button== MouseButtons.Right)
            {
                if (isCapture)
                {
                    Bitmap map = new Bitmap(width, height);
                    Bitmap source = new Bitmap(bcgImg, new Size(this.Width, this.Height));
                    using (Graphics g = Graphics.FromImage(map))
                    {
                        g.DrawImage(source, new Rectangle(0, 0, map.Width, map.Height), new Rectangle(recX, recY, width, height), GraphicsUnit.Pixel);
                    }
                    Clipboard.SetImage(map);
                    main_g.Dispose();
                    DialogResult = DialogResult.OK;
                    isCapture = false;
                }
            }
            
            
        }

        private void CaptureFrm_MouseUp(object sender, MouseEventArgs e)
        {
            if(e.Button== MouseButtons.Left)
            {
                //进行最终边界确认
                endPoint = e.Location;
                drawingFlag = false;
                isCapture = true;
            }
           
        }
        int recX, recY, width, height;
        private void CaptureFrm_MouseMove(object sender, MouseEventArgs e)
        {
            if (!drawingFlag ||main_g==null) return;
            
            width = Math.Abs(startPoint.X - e.X);
            height = Math.Abs(startPoint.Y - e.Y);
            if (startPoint.X < e.X)
            {
                recX = startPoint.X;
            }
            else
            {
                recX = startPoint.X-width;
            }
            if (startPoint.Y < e.Y)
            {
                recY = startPoint.Y;
            }
            else
            {
                recY = startPoint.Y-height;
            }
            CaptureFrm_Paint(null, null);
            Pen pen = new Pen(Color.Green, 2);
            pen.DashPattern = new float[] { 1, 2 };
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            main_g.DrawRectangle(pen, recX, recY, width, height);
            string ss = $"X:{recX},Y:{recY}\n width:{width},height:{height}";
            main_g.DrawString(ss, new Font("宋体", 12,FontStyle.Bold), Brushes.Red, new Point(10, 10));
        }

        private void CaptureFrm_Paint(object sender, PaintEventArgs e)
        {
            if (main_g == null) return;
          //  main_g.Clear(Color.WhiteSmoke);
            main_g.DrawImage(bcgImg, new Rectangle(0, 0, this.Width, this.Height), new Rectangle(0, 0, bcgImg.Width, bcgImg.Height), GraphicsUnit.Pixel);
           
        }

        private void CaptureFrm_DoubleClick(object sender, EventArgs e)
        {
           
        }
    }
相关推荐
lpfasd1231 小时前
编程语言榜单变迁分析
开发语言·后端·scala
名字还没想好☜2 小时前
Go 用 bufio.Scanner 读大文件踩坑:默认 64KB 行上限、Buffer 扩容与按 Token 切分
开发语言·后端·golang·go
Byron Loong3 小时前
【C++】重定向是什么
开发语言·c++
噗噗夹的TA之旅3 小时前
Shader 学习 21:自定义 Render Feature
学习·游戏·unity·c#·游戏引擎·图形渲染
techdashen3 小时前
Go设计取舍之六: sync.Mutex正常模式与饥饿模式
开发语言·后端·golang
TunerT_TQ4 小时前
【智能体安全治理|专栏第9期】从“一堆规则”到“数字宪法”:智能体治理的下一个阶段
java·开发语言·安全·开源治理·大模型安全·ai基础设施·智能体安全
Aurorar0rua4 小时前
CS50 x 2024 Notes Algorithms - 07
c语言·开发语言·学习方法
程序员-Benothing4 小时前
Java ForkJoinPool 详解:从分治思想到高性能并行计算
java·开发语言·后端·面试·职场和发展
脚踏实地皮皮晨4 小时前
003006001_WrapPanel控件
开发语言·c#·.net·wpf·visual studio
CODER03044 小时前
封装忽律底层硬件差异的自定义系统
windows·microsoft