1. 程式人生 > >c# picturebox圖片上畫框框或圈圈

c# picturebox圖片上畫框框或圈圈

picturebox裝載圖片後,然後在在這個圖片上用滑鼠按下,再拖動大小,鬆開後就畫了一個框,pictruebox尺寸和圖片本身尺寸有關係的,每次畫後都要重繪,效果圖

 

直接貼程式碼:

/// <summary>
        /// 滑鼠狀態
        /// </summary>
        private bool m_MouseIsDown = false;
        /// <summary>
        /// 繪製區域
        /// </summary>
        private Rectangle m_MouseRect = Rectangle.Empty;
        public delegate void SelectRectangel(object sneder, Rectangle e);
        public event SelectRectangel SetRectangel;

        int intwidth = 0;
        int intheigh = 0;

///開啟一個圖片
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() ==DialogResult.OK )
            {
                pictureBox1.ImageLocation = openFileDialog1.FileName;
                Image pic = Image.FromFile(openFileDialog1.FileName);
                intwidth = pic.Width;
                intheigh = pic.Height;
                pos = pictureBox1.PointToScreen(pictureBox1.Location);
            }
        }

//滑鼠按下

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            m_MouseIsDown = true;
            DrawStart(new Point(e.X, e.Y));         
        }
        ///滑鼠移動
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_MouseIsDown) ResizeToRectangle(new Point(e.X, e.Y));
                 
        }
      
       
        float w = 0;//寬度比例
        float h = 0;//高度比例
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)///滑鼠彈起
        {
           
              w = (float)intwidth /(float) pictureBox1.Width;
             h = (float)intheigh / (float)pictureBox1.Height;
            Cursor.Clip = Rectangle.Empty;
            m_MouseIsDown = false;
            DrawRectangle();
            if (m_MouseRect.X == 0 || m_MouseRect.Y == 0 || m_MouseRect.Width == 0 || m_MouseRect.Height == 0)
            {
                //如果區域沒0 就不執行委託
            }
            else
            {
                if (SetRectangel != null) SetRectangel(pictureBox1, m_MouseRect);
            }
          
            ControlPaint.DrawReversibleFrame(pictureBox1.RectangleToScreen(m_MouseRect), Color.Red, FrameStyle.Thick);
            System.Drawing.Bitmap bmp = new Bitmap(pictureBox1.Image);
            Graphics m_mouse = Graphics.FromImage(bmp);           
            Brush brush = new SolidBrush(Color.Red);
            Pen pen = new Pen(brush, 3);
           int  wd=(int)(w*m_MouseRect.Width);
           int hd = (int)(h * m_MouseRect.Height);
            m_mouse.DrawEllipse(pen, new Rectangle((int)(m_MouseRect.X*w),(int)(h* m_MouseRect.Y), wd, hd));
            m_mouse.DrawRectangle(pen, new Rectangle((int)(m_MouseRect.X*w),(int)(h* m_MouseRect.Y), wd, hd));           
            MemoryStream ms = new MemoryStream();
           // bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            bmp.Save("D://1.bmp");         
            pictureBox3.Image = bmp;

            //截圖可以實現效果
            Bitmap myImage = new Bitmap(pictureBox1.Width , pictureBox1.Height);
            Graphics g = Graphics.FromImage(myImage);        
            g.CopyFromScreen(pictureBox1.PointToScreen(pictureBox1.Location),new Point(0,0),pictureBox1.PreferredSize);
            IntPtr dc1 = g.GetHdc();
            g.ReleaseHdc(dc1);
            pictureBox2.Image=(Image)myImage;
           

        }

/// <summary>
        /// 重新整理繪製
        /// </summary>
        /// <param name="p"></param>
        private void ResizeToRectangle(Point p_Point)
        {
            DrawRectangle();
            m_MouseRect.Width = p_Point.X - m_MouseRect.Left;
            m_MouseRect.Height = p_Point.Y - m_MouseRect.Top;
            DrawRectangle();
        }


        /// <summary>
        /// 繪製區域
        /// </summary>
        private void DrawRectangle()
        {
            Rectangle _Rect = pictureBox1.RectangleToScreen(m_MouseRect);
            ControlPaint.DrawReversibleFrame(_Rect, Color.White, FrameStyle.Dashed);
           


        }

        /// <summary>
        /// 開始繪製 並且設定滑鼠區域
        /// </summary>
        /// <param name="StartPoint">開始位置</param>
        private void DrawStart(Point p_Point)
        {
            //pos.X -= panel1.Location.X;
            //pos.Y -= panel1.Location.Y;

            //pictureBox1.Capture = true;
            Cursor.Clip = pictureBox1.RectangleToScreen(new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            m_MouseRect = new Rectangle(p_Point.X, p_Point.Y, 0, 0);

        }

以上已經測試成功,圖片大小尺寸均對,起始裡面還有寫文字,但是文字區域無法定位,可以一起討論

補充:

又弄了個在CE上的,這個無法移動是產生虛線,只能手寫筆離開的時候才能出現圈圈