绘制用户定义的矩形

时间:2010-10-05 08:45:13

标签: c# graphics drag-and-drop drawrectangle

我当前的代码允许我从用户定义的位置绘制矩形,但不能按照我想要的方式绘制矩形。我需要它就像你在油漆中做的那样,这是我现在的代码:

命名空间SimpleDraw2 {     ///     /// MainForm的描述。     ///     公共部分类MainForm:表单     {         bool IsMouseDown = false;         Point MousePosition;         int DrawShape = 0;         位图StoredImage;

    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;
    }
}

}

如果有人可以帮我编辑我的代码以解决问题,以便轻松拖动和绘制系统,我会非常感激。

先谢谢

克里斯

2 个答案:

答案 0 :(得分:2)

来自msdn

  

绘制由a指定的矩形   坐标对,宽度和a   高度。

所以你的代码不起作用:

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));

答案 1 :(得分:1)

我看到的最大问题是你正在尝试绘制鼠标事件。这意味着您的绘图将在您获得刷新事件的瞬间消失。

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