画边框

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using S10806Class;
using System.IO;

namespace GetImage
{
    public partial class GetScreenImgForm : Form
    {
         /// 图像操作类
         ImageWork imageWork = new ImageWork();
        /// 保存截取到的桌面图像
        Image screenImg = null;
         /// 保存用户截取的图像区域
        Rectangle imgRect = new Rectangle();
        /// 窗体的GDI
       Graphics g = null;
       /// 区域边框画笔
       Pen pen = new Pen(Color.Black, 1);
        /// 是否应该绘制区域
      bool isDraw = false;
        /// 用户是否选择了区域
         bool isOption = false;

       public GetScreenImgForm()
        {
            InitializeComponent();
     //获取当前桌面截图
            screenImg = imageWork.GetScreenImage();
            g = this.CreateGraphics();
        }
        private void GetScreenImgForm_Load(object sender, EventArgs e)
        {
            this.Size = new Size(screenImg.Width, screenImg.Height);
            this.BackgroundImage = screenImg;
        }
        private void GetScreenImgForm_MouseUp(object sender, MouseEventArgs e)
        {
            //没有截图并且单击右键 - 关闭本窗体
            if (e.Button == MouseButtons.Right && !this.isOption)
            {
                this.Close();
            }
            //截图了并且单击右键 - 取消此次截图
            else if (e.Button == MouseButtons.Right && this.isOption)
            {
                g.DrawImage(screenImg, new Point(0, 0));
                this.isOption = false;
                return;
            }
            //在截图情况下松开左键,选择区域结束,等待双击截取
            else if (e.Button == MouseButtons.Left && this.isOption && this.isDraw)
            {
                this.isDraw = false;
                imgRect.Width = e.X - imgRect.X;   //区域长度
                imgRect.Height = e.Y - imgRect.Y;  //区域高度
            }
        }
        private void GetScreenImgForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;
            if (this.isOption) return;
            imgRect.X = e.X;   //起始X轴
            imgRect.Y = e.Y;   //起始Y轴
            this.isDraw = true;//允许绘图
        }
        private void GetScreenImgForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDraw)
            {
                //刷新图像
                g.DrawImage(screenImg, new Point(0, 0));
                this.isOption = true;
                Point p1 = new Point(imgRect.X, imgRect.Y);
                Point p2 = new Point(e.X, imgRect.Y);
                Point p3 = new Point(imgRect.X, e.Y);
                Point p4 = new Point(e.X, e.Y);
                //画四条边框
                g.DrawLine(pen, p1, p2);
                g.DrawLine(pen, p2, p4);
                g.DrawLine(pen, p4, p3);
                g.DrawLine(pen, p3, p1);
            }
            else
            {
                //确定用户的选择区域
                int top = imgRect.Y;
                int down = imgRect.Y + imgRect.Height;
                int left = imgRect.X;
                int right = imgRect.X + imgRect.Width;
                //鼠标在选择区域中会变个模样
                if (e.X > left && e.X < right && e.Y > top && e.Y < down)
                {
                    this.Cursor = Cursors.SizeAll;
                }
                else
                {
                    this.Cursor = Cursors.Cross;
                }
            }
        }

        private void GetScreenImgForm_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                //用户双击选择区域时.....
                if (this.Cursor == Cursors.SizeAll)
                {
                    Bitmap b = new Bitmap(this.screenImg);
                    Bitmap img = b.Clone(this.imgRect, System.Drawing.Imaging.PixelFormat.DontCare);
                    SaveFileDialog dlg = new SaveFileDialog();
                    dlg.Filter = "(*.jpg)|*.jpg";
                    if (dlg.ShowDialog() != DialogResult.OK) return;
                    img.Save(dlg.FileName);
                    this.Close();
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值