【WinForm】自己写一个截图软件5 --窗口2(全屏截图)

这个窗口在显示的时候会触发全屏截图,并且窗口设置成显示在所有的前面

 public partial class TempScreen : Form
    {
        private Point mouseDown;
        private Point mouseUp;
        private bool canDraw;
        private Graphics g;
        public TempScreen()
        {
            InitializeComponent();
        }
        
        private void Form2_Load(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Maximized; //最大化
        }

        private void Form2_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
            {
            //截取全屏图像
                Size size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                Bitmap bm = new Bitmap(size.Width, size.Height);
                Graphics g = Graphics.FromImage(bm);
                g.CopyFromScreen(0, 0, 0, 0, size);
                picScreen.Image = bm;
            }
        }
//鼠标选取区域
        private void picScreen_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = e.Location;
            canDraw = true;
        }

        private void picScreen_MouseMove(object sender, MouseEventArgs e)
        {
            if (!canDraw)
            {
                return;
            }
            picScreen.Refresh();
            mouseUp = e.Location;
            
            Pen p = new Pen(Color.Red,1);
            g = picScreen.CreateGraphics();
            Rectangle rect = GetRect(mouseDown,mouseUp);
            if (rect == Rectangle.Empty)
            {
                return;
            }
            g.DrawRectangle(p, rect);
        }
//当鼠标选取结束时,获取选取区域的像素
        private void picScreen_MouseUp(object sender, MouseEventArgs e)
        {
            mouseUp = e.Location;
            canDraw = false;

            Rectangle rect = GetRect(mouseDown, mouseUp);
            if (rect == Rectangle.Empty)
            {
                return;
            }
            //读取像素
            FillBitMap(rect);
        }
        private Rectangle GetRect(Point start,Point end)
        {
            Rectangle rect = new Rectangle();

            //获取两点的X,Y距离
            rect.Width = Math.Abs(start.X - end.X);
            rect.Height = Math.Abs(start.Y - end.Y);

            if (rect.Width == 0 || rect.Height == 0)
            {
                return Rectangle.Empty;
            }
            //根据两个点确定矩形的左上角点Location
            if (start.X > end.X && start.Y < end.Y)
            {
                rect.Location = new Point(end.X, start.Y);
            }
            else if (start.X < end.X && start.Y > end.Y)
            {
                rect.Location = new Point(start.X, end.Y);
            }
            else if (start.X > end.X && start.Y > end.Y)
            {
                rect.Location = end;
            }
            else
            {
                rect.Location = start;
            }
            return rect;
        }
        //绘制选取区域截图,打开新的窗口
        void FillBitMap(Rectangle rect)
        {
            Bitmap bm = ImageUtil.GetBitmapFromImage(picScreen.Image, rect);
            Select pic = new Select(bm);
            pic.Show();
            pic.Location = rect.Location;
            Visible = false;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值