C# 绘制一个用户定义的矩形

C# 绘制一个用户定义的矩形,c#,graphics,drag-and-drop,drawrectangle,C#,Graphics,Drag And Drop,Drawrectangle,我当前的代码允许我从用户定义的点绘制矩形,但不是以我希望的方式。我需要它就像你在绘画中一样,这是我当前的代码: 命名空间SimpleDraw2 { /// ///主窗体的描述。 /// 公共部分类主窗体:窗体 { bool IsMouseDown=false; 点鼠标定位; int DrawShape=0; 位图存储图像 public MainForm() { // // The InitializeComponent() call is re

我当前的代码允许我从用户定义的点绘制矩形,但不是以我希望的方式。我需要它就像你在绘画中一样,这是我当前的代码:

命名空间SimpleDraw2 { /// ///主窗体的描述。 /// 公共部分类主窗体:窗体 { bool IsMouseDown=false; 点鼠标定位; int DrawShape=0; 位图存储图像

    public MainForm()
    {
        //
        // The InitializeComponent() call is required for Windows Forms designer support.
        //
        InitializeComponent();

        //
        // TODO: Add constructor code after the InitializeComponent() call.
        //
        pictureBox1.Image = new Bitmap    (pictureBox1.Width,pictureBox1.Height);                      
        StoredImage =  new Bitmap(pictureBox1.Width,pictureBox1.Height);
    }

    void PictureBox1MouseDown(object sender, MouseEventArgs e)
    {
        IsMouseDown = true;
        MousePosition = e.Location;
        Graphics gStored = Graphics.FromImage(StoredImage);
        gStored.Clear(Color.Transparent);
        gStored.DrawImage(pictureBox1.Image, 0, 0);
    }

    void PictureBox1MouseUp(object sender, MouseEventArgs e)
    {
        IsMouseDown = false;
    }

    void PictureBox1MouseMove(object sender, MouseEventArgs e)
    {
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        if (DrawShape == 0) 
        {
            Pen p = new Pen(Color.Red, 10);
            if (IsMouseDown) 
            {
                g.DrawLine(p,MousePosition,e.Location);
                MousePosition = e.Location;
            }
        }
        if (DrawShape == 1) 
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage,0,0);
            g.DrawRectangle(Pens.Green,MousePosition.X,MousePosition.Y,e.X,e.Y);

        }
        if (DrawShape == 2)
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage, 0, 0);
            g.DrawEllipse(Pens.HotPink, MousePosition.X, MousePosition.Y, e.X, e.Y);
        }
        if (DrawShape == 3)
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage, 0, 0);
            g.DrawArc(Pens.Indigo,pictureBox1.Bounds, e.Y, e.X);
        }
        //if (DrawShape == 4)
        //{
        //    g.Clear(Color.Transparent);
        //    g.DrawImage(StoredImage, 0, 0);
        //    g.DrawPolygon(Pens.Indigo, Point[] e.X);
        //}

        this.Refresh();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            axWindowsMediaPlayer1.URL = ofd.FileName;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.Ctlcontrols.pause();
        Bitmap bmp = new Bitmap(axWindowsMediaPlayer1.Width, axWindowsMediaPlayer1.Height);
        Graphics gfx = Graphics.FromImage(bmp);
        gfx.CopyFromScreen(PointToScreen(axWindowsMediaPlayer1.Location), new Point(0, 0), axWindowsMediaPlayer1.Bounds.Size, CopyPixelOperation.SourceCopy);
        pictureBox1.BackgroundImage = bmp;
        //axWindowsMediaPlayer1.Visible = false;
        //pictureBox1.Visible = true;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Graphics gg = Graphics.FromImage(pictureBox1.BackgroundImage);
        gg.Clear(Color.Transparent);
        Graphics gStored = Graphics.FromImage(StoredImage);
        gStored.Clear(Color.Transparent);
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        g.Clear(Color.Transparent);


    }

    private void button4_Click(object sender, EventArgs e)
    {
        DrawShape = 1;

    }

    private void button6_Click(object sender, EventArgs e)
    {
        DrawShape = 2;
    }

    private void button8_Click(object sender, EventArgs e)
    {
       DrawShape = 3;
    }

    private void button7_Click(object sender, EventArgs e)
    {
        DrawShape = 0;
    }
}
}

如果有人能帮我编辑我的代码来解决这个问题,使它更容易拖动和绘制系统,我将不胜感激

提前谢谢

克里斯来自:

绘制由指定的矩形 坐标对、宽度和宽度 高度

因此,您的代码无法工作:

g.DrawRectangle(Pens.Green,MousePosition.X,MousePosition.Y,e.X,e.Y);
应该是

g.DrawRectangle(Pens.Green, MousePosition.X, MousePosition.Y, Math.Abs(e.X - MousePosition.X), Math.Abs(e.Y - MousePosition.Y));

我看到的最大问题是,您试图在鼠标事件中绘制。这意味着您的绘图将在获得刷新事件时立即被删除


仅在绘制事件中绘制,而不在鼠标事件中绘制。如果您希望应用程序作为鼠标事件的结果绘制,请在鼠标事件中设置点、矩形或任何内容(就像您开始使用IsMouseDown一样),在MouseMoved事件中使要更改的区域无效,然后在Paint事件中绘制矩形或其他任何内容。

同样的问题仍然存在,它保存最后一个位置,并从上次离开picturebox的位置绘制一个正方形。