Reputation: 604
I'm currently trying to draw something on image that is shown in pictureBox. I'm 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'm trying to simulate brush tool that draws circles of specified size (radius) when mouse is moving over pictureBox. It is working great when moving slow but when the movement is faster pictureBox seems to catch only some of the events and lot of circles is skipped and not drawn. Especially when the radius is small.
What can I do to make it faster and smoother?
Upvotes: 3
Views: 4461
Reputation: 13874
When the mouse is moved, you won't get a MouseMove
event for every single pixel traveled by the mouse pointer. You'll get them at a fairly consistent time interval, so the faster the mouse moves, the further apart the points you'll get. This particular detail you can't do much about.
What you need to do is store the position of the last point received, and draw an ellipse at every point between the last and the new one.
Upvotes: 4