【WinForm】自己写一个截图软件--窗口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;
        }
    }

猜你喜欢

转载自blog.csdn.net/zoysia1314/article/details/86569640
今日推荐