【WinForm】自己写一个截图软件--窗口3(最后的选择窗口)

这是截图软件的最终显示窗口,在第二步选择的画面会显示在这个窗口中

 public partial class Select : Form
    {
        private bool canMove = false;
        private Point start;
        private bool small;
        private Size sizeRecord = Size.Empty;
        private Image imgRecord;
        private Point pointRecord;
        private string shotName;
        private Select()
        {
            InitializeComponent();
        }

        public Select(Image img)
        {
            InitializeComponent();
            picScreenShot.Image = img;
            Size = img.Size;
            shotName = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
        }
        private void picScreenShot_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics,picScreenShot.ClientRectangle,
                Color.White, 1, ButtonBorderStyle.Solid, 
                Color.White, 1, ButtonBorderStyle.Solid, 
                Color.Black, 1, ButtonBorderStyle.Solid, 
                Color.Black, 2, ButtonBorderStyle.Solid);
        }

        private void picScreenShot_MouseDown(object sender, MouseEventArgs e)
        {
            canMove = true;
            start = e.Location;
        }

        private void picScreenShot_MouseMove(object sender, MouseEventArgs e)
        {
            if (canMove)
            {
                Location += (Size)Point.Subtract(e.Location , (Size)start);
            }
        }

        private void picScreenShot_MouseUp(object sender, MouseEventArgs e)
        {
            canMove = false;
        }

        private void Save_Click(object sender, EventArgs e)
        {
            FileDialogUtil.SaveScreenShot(picScreenShot.Image, shotName);
        }

        private void SaveAs_Click(object sender, EventArgs e)
        {
            FileDialogUtil.SaveAs(picScreenShot.Image, shotName);
        }

        private void Copy_Click(object sender, EventArgs e)
        {
            Clipboard.SetImage(picScreenShot.Image);
        }

        private void Close_Click(object sender, EventArgs e)
        {
            Visible = false;
            Close();
            Dispose();
        }


        private void picScreenShot_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (small)
            {
                picScreenShot.Image = imgRecord;
                Size = sizeRecord;
                Location = pointRecord;
                small = false;
            }
            else
            {
                sizeRecord = Size;
                imgRecord = picScreenShot.Image;
                pointRecord = Location;
                picScreenShot.Image = GetSmallImage(picScreenShot.Image, picScreenShot.Location, e.Location);
                Size = picScreenShot.Image.Size;
                Thread.Sleep(20); //防止缩小操作时的闪烁现象
                Location = Location + (Size)e.Location - new Size(Size.Width / 2, Size.Height / 2);
                small = true;
            }
        }
//获取截图缩小成50*50的大小的部分
        private Image GetSmallImage(Image source,Point position,Point point)
        {
            if (source.Width <= 50)
            {
                if (source.Height <= 50) //宽高都小于等于samll尺寸,按原大小返回
                {
                    return source;
                }
                if (point.Y < position.Y + 25) //鼠标距上边界不足25
                {
                    point.Y = position.Y + 25;
                }
                else if (point.Y > position.Y + source.Height - 25) //鼠标距下边界不足25
                {
                    point.Y = position.Y + source.Height - 25;
                }
                return ImageUtil.GetBitmapFromImage(source,new Rectangle(position.X,point.Y-25,source.Width,50));
            }
            //宽大于50的情况
            if (source.Height <= 50) //高小于等于50
            {
                if ( point.X < position.X + 25) //鼠标距左边不足25
                {
                    point.X = position.X + 25;
                }
                else if (point.X > position.X + source.Width - 25) //鼠标距右边不足25
                {
                    point.X = position.X + source.Width - 25;
                }
                return ImageUtil.GetBitmapFromImage(source, new Rectangle(point.X - 25, position.Y, 50, source.Height));
            }
            else //高大于50
            {
                if (point.X < position.X + 25)
                {
                    point.X = position.X + 25;
                }else if(point.X > position.X + source.Width - 25)
                {
                    point.X = position.X + source.Width - 25;
                }
                if (point.Y < position.Y + 25)
                {
                    point.Y = position.Y + 25;
                }else if (point.Y > position.Y + source.Height -25)
                {
                    point.Y = position.Y + source.Height - 25;
                }
                return ImageUtil.GetBitmapFromImage(source, new Rectangle(point.X - 25, point.Y - 25, 50, 50));
            }
        }
        /// <summary>
        /// 快捷键
        /// </summary>
        private void Form3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control)
            {
                if (e.KeyCode == Keys.S)
                {
                    Save_Click(sender,e);
                }else if (e.KeyCode == Keys.C)
                {
                    Copy_Click(sender,e);
                }
            }
            else if (e.Modifiers == (Keys.Control|Keys.Shift) && e.KeyCode == Keys.S)
            {
                SaveAs_Click(sender, e);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                Close_Click(sender,e);
            }
        }

    }

猜你喜欢

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