How to handle MouseMove events in C # faster

I am currently trying to draw something on the image that is shown in the pictureBox. I am using event handlers for mouse activity: onMouseUp, onMouseMove and onMouseDown.

private void onMouseDown(object sender, MouseEventArgs e)
    {
        mouseDown = true;
    }

    private void onMouseMove(object sender, MouseEventArgs e)
    {
        if (mouseDown)
        {
            using (Graphics g = pictureBox.CreateGraphics())
            {
                g.FillEllipse(Brushes.Black, e.X - size, e.Y - size, size * 2, size * 2);
            }
        }
    }
private void onMouseUp(object sender, MouseEventArgs e)
    {
        mouseDown = false;

        using (Graphics g = pictureBox.CreateGraphics())
        {
            g.FillEllipse(Brushes.Black, e.X - size, e.Y - size, size * 2, size * 2); //just in case user just clicks instead of move the mouse
        }
    }

      

I am trying to simulate a brush tool that draws circles of a given size (radius) when the mouse moves over a pictureBox. It works great on slow motion, but when the motion is faster, the ImageBox seems to only capture some of the events and the number of circles is skipped and not displayed. Especially when the radius is small.

What can I do to make it faster and smoother?

+3


source to share


1 answer


When the mouse moves, you will not receive an event MouseMove

for every pixel that the mouse pointer moves . You will get them on a fairly consistent time frame, so the faster the mouse moves, the further you get points. This particular detail that you can't do much about.



What you need to do is store the position of the last point obtained and draw an ellipse at each point between the last point and the new one.

+3


source







All Articles