• 鼠标画出矩形


      public partial class ImageLoader : Form
        {

            private Point startPoint, endPoint;
            bool mark = false;
            Graphics bitG;
            Bitmap bits;
            public ImageLoader()
            {
                InitializeComponent();
                picBox1.Dock = DockStyle.Fill;
                bits = new Bitmap(picBox1.Width, picBox1.Height);
                bitG = Graphics.FromImage(bits);
                bitG.Clear(Color.White);
                picBox1.Image = bits;

                picBox1.MouseDown += new MouseEventHandler(picBox1_MouseDown);
                picBox1.MouseMove += new MouseEventHandler(picBox1_MouseMove);
                picBox1.MouseUp += new MouseEventHandler(picBox1_MouseUp);    
            }

            public Rectangle MakeRectangle(Point p1, Point p2)
            {
                int top, left, bottom, right;
                top = p1.X <= p2.X ? p1.X : p2.X;
                left = p1.Y <= p2.Y ? p1.Y : p2.Y;
                bottom = p1.X > p2.X ? p1.X : p2.X;
                right = p1.Y > p2.Y ? p1.Y : p2.Y;
                return (new Rectangle(top, left, bottom - top, right - left));
            }

            private void picBox1_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    Pen pen1 = new Pen(Color.Black, 1);
                    endPoint = e.Location;
                    Rectangle ret1 = MakeRectangle(startPoint, endPoint);
                    bitG.DrawRectangle(pen1, ret1);
                    picBox1.Image = bits;
                    mark = false;
                }
            }

            private void picBox1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    mark = true;
                    startPoint = e.Location;
                    endPoint = e.Location;
                }
               
            }

            private void picBox1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left && mark)
                {
                    Rectangle ret = MakeRectangle(startPoint, endPoint);
                    ret.Width += 2;
                    ret.Height += 2;
                    picBox1.Invalidate(ret);
                    picBox1.Update();
                    Graphics g = picBox1.CreateGraphics();
                    Pen pen = new Pen(Color.Black, 1);
                    endPoint = e.Location;
                    ret = MakeRectangle(startPoint, endPoint);
                    g.DrawRectangle(pen, ret);
                }
            }

            private void picBox1_Paint(object sender, PaintEventArgs e)
            {
               

            }

         
        }

  • 相关阅读:
    java web 入门实例servlet篇(显示后台数据库列表,删除某一条记录并显示)
    我的成长比价系列:java web开发过程中遇到的错误一:sql语句换行错误
    spring mvc + velocity 搭建实例程序maven版本并且使用的是tomcat容器而不是jetty(step by step)
    spring mvc学习笔记(一)web.xml文件配置的一点重要信息
    与数据库连接的页面增删改查 的easyui实现(主要是前端实现)
    oracle 11g 空表导出
    EMCA和EMCTL的简单用法
    vs2010补丁
    CAS 策略已被 .NET Framework 弃用
    sqlserver2008 链接服务器 2000
  • 原文地址:https://www.cnblogs.com/lmcblog/p/2596413.html
Copyright © 2020-2023  润新知