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

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值