簡體   English   中英

如何用鼠標在圖像上繪制多個矩形?

[英]How to draw multiple rectangles with mouse on an image?

我正在嘗試構建一個WinForm應用程序,該應用程序將從openfile對話框加載圖像文件並將其加載到面板或圖片框。 我希望能夠用鼠標左鍵繪制多個矩形並將它們留在圖像上。 我已經成功地一次在實際表單上獲得了一個矩形,但沒有在面板/圖片框內的圖像上獲得一個。 有人知道有什么資源可以幫助我理解如何實現這一目標嗎?

這是允許我在窗體上繪制一個可調整大小的矩形的代碼,但是當我將鼠標事件從form1_MouseEventpanel1_MouseEvent它什么也不做。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Temporary_Name_Utility
{
    public partial class Form1 : Form
    {
        Rectangle myRectangle;
        bool draw = false;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog myFile = new OpenFileDialog();
                myFile.Filter = "Image Files(*.img, *.bmp) |*.img; *.bmp;";
                myFile.InitialDirectory = "c:\\";

                if (myFile.ShowDialog() == DialogResult.OK)
                {
                    pictureBox1.BackgroundImage = Image.FromFile(myFile.FileName);
                }
            }

            catch (Exception error)
            {
                MessageBox.Show("Error loading the selected file.  Original error: " + error.Message);

            }

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            myRectangle = new Rectangle(e.X, e.Y, 0, 0);
            this.Invalidate();
        }


        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if(draw)
            {
                using(Pen myPen = new Pen(Color.Red, 2))
                {
                    e.Graphics.DrawRectangle(myPen, myRectangle);
                }
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                myRectangle = new Rectangle(myRectangle.Left, myRectangle.Top, Math.Min(e.X - myRectangle.Left, pictureBox1.ClientRectangle.Width - myRectangle.Left), Math.Min(e.Y - myRectangle.Top, pictureBox1.ClientRectangle.Height - myRectangle.Top));
            }
            this.Invalidate();
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {

        }

    }
}

最終得到一些工作代碼:這允許用戶使用鼠標在圖片框內繪制任意數量的矩形。

     using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rectangle_Utility
{
    public partial class Form1 : Form
    {
        Point startPos;
        Point currentPos;
        bool drawing;
        List<Rectangle> myRectangles = new List<Rectangle>();

        public Form1()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
        }

        #region Menu Tool Strip
        private void selectFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                //Initiate new OpenFileDialog
                //Filter for img and bmp files
                //Start looking in root of c:
                OpenFileDialog myFile = new OpenFileDialog();
                myFile.Filter = "Image Files(*.img, *.bmp) |*.img; *.bmp;";
                myFile.InitialDirectory = "c:\\";

                //Set background image of pictureBox to file selected through OpenFileDialog
                if (myFile.ShowDialog() == DialogResult.OK)
                {
                    pictureBox1.BackgroundImage = Image.FromFile(myFile.FileName);

                }
            }

            catch (Exception error)
            {
                MessageBox.Show("Error loading the selected file.  Original error: " + error.Message);

            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void clearAllToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void zoomToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void undoLastActionToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void redoLastActionToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region Rectangle

        private Rectangle getRectangle()
        {
            return new Rectangle(
                Math.Min(startPos.X, currentPos.X),
                Math.Min(startPos.Y, currentPos.Y),
                Math.Abs(startPos.X - currentPos.X),
                Math.Abs(startPos.Y - currentPos.Y));
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                currentPos = startPos = e.Location;
                drawing = true;
            }

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            currentPos = e.Location;
            if (drawing) pictureBox1.Invalidate();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (myRectangles.Count > 0) e.Graphics.DrawRectangles(Pens.Black, myRectangles.ToArray());
            if (drawing) e.Graphics.DrawRectangle(Pens.Red, getRectangle());
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (drawing)
            {
                drawing = false;
                var rect = getRectangle();
                if (rect.Width > 0 && rect.Height > 0) myRectangles.Add(rect);
                pictureBox1.Invalidate();
            }
        }

        #endregion

    }
}

您必須從圖像創建圖形,然后使用該圖形繪制矩形,以便在圖像中繪制矩形。

Graphics newGraphics = Graphics.FromImage(image);
newGraphics.FillRectangle(new SolidBrush(Color.Black), rectangles);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM