在鼠标拖动时绘制时形状不可见C#

时间:2015-09-22 03:09:33

标签: visual-studio-2005 c#-2.0

我是一名新开发者。我正在尝试制作一个简单的程序,我们可以在其中绘制多种形状,如Rectangle,Ellipse,Line等。我现在面临的问题是,当我点击鼠标拖动以在mouseDown和mouseMove事件上绘制我无法看到形状。只有当我离开鼠标时,形状才会出现在pictureBox上。 我希望使用鼠标显示和移动形状,就像用户绘制它一样,就像微软的绘图应用程序一样。 我尝试在绘图时使用Refresh()函数,但它删除了已经在pictureBox中绘制的旧形状。 下面是我正在使用的代码。需要一些建议。

bool draw = false;
    bool isMouseDown = false;
    int x, y, lx, ly = 0;
    Item currItem;
    Pen pen = new Pen(Color.Red,1);

    public enum Item
    { 
        Rectangle, Ellipse, Line, Pencil, Eraser
    }

    private void btnROIDraw_Click(object sender, EventArgs e)
    {
        if (menuStripRoi.Visible == false)
        {
            menuStripRoi.Visible = true;
        }
        else
        {
            menuStripRoi.Visible = false;
            draw = false;
        }
    }

    private void pictureBoxROI_MouseDown(object sender, MouseEventArgs e)
    {
        x = e.X;
        y = e.Y;
        isMouseDown = true;
        //draw = true;
    }
    private void pictureBoxROI_MouseUp(object sender, MouseEventArgs e)
    {
        lx = e.X;
        ly = e.Y;

        if (draw)
        {
            Graphics g = pictureBoxROI.CreateGraphics();
            switch (currItem)
            {
                case Item.Rectangle:
                    g.DrawRectangle(pen, x, y, e.X - x, e.Y - y);
                    break;
                case Item.Ellipse:
                    g.DrawEllipse(pen, x, y, e.X - x, e.Y - y);
                    break;
                case Item.Line:
                    g.DrawLine(pen, x, y, e.X, e.Y);
                    break;
            }
            g.Dispose();
        }
        isMouseDown = false;

    }

    private void pictureBoxROI_MouseMove(object sender, MouseEventArgs e)
    {
        if (isMouseDown)
        {
            if (draw)
            {
                Graphics g = pictureBoxROI.CreateGraphics();
                switch (currItem)
                {
                    case Item.Pencil:
                        g.FillEllipse(new SolidBrush(Color.Red), e.X - x + x, e.Y - y + y, 2, 2);
                        break;
                    case Item.Eraser:
                        g.FillEllipse(new SolidBrush(pictureBoxROI.BackColor), e.X - x + x, e.Y - y + y, 20, 20);
                        break;
                }
                g.Dispose();
            }
        }
    }

    private void pictureBoxROI_Paint(object sender, PaintEventArgs e)
    {

    }
    private void clearToolStripMenuItem_Click(object sender, EventArgs e)
    {
        pictureBoxROI.Refresh();
    }

    private void rectangleToolStripMenuItem_Click(object sender, EventArgs e)
    {
        currItem = Item.Rectangle;
        draw = true;
    }

    private void ellipseToolStripMenuItem_Click(object sender, EventArgs e)
    {
        currItem = Item.Ellipse;
        draw = true;
    }

    private void lineToolStripMenuItem_Click(object sender, EventArgs e)
    {
        currItem = Item.Line;
        draw = true;
    }

    private void pencilToolStripMenuItem_Click(object sender, EventArgs e)
    {
        currItem = Item.Pencil;
        draw = true;
    }

    private void eracerToolStripMenuItem_Click(object sender, EventArgs e)
    {
        currItem = Item.Eraser;
        draw = true;
    }

    private void newToolStripMenuItem_Click(object sender, EventArgs e)
    {
        pictureBoxROI.Refresh();
        pictureBoxROI.Image = null;
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog o = new OpenFileDialog();
        o.Filter = "Png files|*.png|jpeg files|*jpg|bitmaps|*.bmp";
        if (o.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            pictureBoxROI.Image = (Image)Image.FromFile(o.FileName).Clone();
        }
    }

    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(pictureBoxROI.Width, pictureBoxROI.Height);
        Graphics g = Graphics.FromImage(bmp);
        Rectangle rect = pictureBoxROI.RectangleToScreen(pictureBoxROI.ClientRectangle);
        g.CopyFromScreen(rect.Location, Point.Empty, pictureBoxROI.Size);
        g.Dispose();
        SaveFileDialog s = new SaveFileDialog();
        s.Filter = "Png files|*.png|jpeg files|*jpg|bitmaps|*.bmp";
        if (s.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if (File.Exists(s.FileName))
            {
                File.Delete(s.FileName);
            }
            if (s.FileName.Contains(".jpg"))
            {
                bmp.Save(s.FileName, ImageFormat.Jpeg);
            }
            else if (s.FileName.Contains(".png"))
            {
                bmp.Save(s.FileName, ImageFormat.Png);
            }
            else if (s.FileName.Contains(".bmp"))
            {
                bmp.Save(s.FileName, ImageFormat.Bmp);
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

找到我的问题的解决方案。 本文详细介绍了有关使用GDI +绘图的所有内容。值得一看 http://www.codeproject.com/Articles/17893/Extensions-to-DrawTools