I try to write a simple graphical editor. I have next problem, when i'm painting some curve using SolidBrush()
, I get the interrupted one (look image). I need to get uninterrupted curve. I try to use capture of mouse for this, but it doesn't work (result the same). How I can fix it? Look code below for MouseMove event handler:
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
pictureBox1.Capture = true; // I try to capture mouse here
Graphics g = Graphics.FromHwnd(this.pictureBox1.Handle);
g.FillRectangle(new SolidBrush(Color.Black), e.X, e.Y, 1, 1);
}
}
It depends on the speed of your mouse moves, sometimes MouseMove event will fire more often and sometimes not. I think it also depends how much is your machine loaded at that particular moemnt. If you draw lines between two points they won't be curved but straight. Instead you should look at Beziers and Splines. That way you will get curves based on few points.
But you could do something with your code. Whenever the distance between your last mousedown and current mousedown event is greater than a threshold (you can get that empirically), you can add new dot(s) to your curve. below is the example code which is adding one dot:
public bool isMouseDown { get; set; }
Point lastPoint = Point.Empty;
public double treshold { get; set; }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
pictureBox1.Capture = true; // I try to capture mouse here
Graphics g = Graphics.FromHwnd(this.pictureBox1.Handle);
if (Math.Sqrt(Math.Pow(e.X - lastPoint.X, 2) + Math.Pow(e.Y - lastPoint.Y, 2)) > treshold)
{
g.FillRectangle(new SolidBrush(Color.Black), (e.X + lastPoint.X)/2, (e.Y + lastPoint.Y)/2, 1, 1);
}
g.FillRectangle(new SolidBrush(Color.Black), e.X, e.Y, 1, 1);
lastPoint = new Point(e.X, e.Y);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
lastPoint = new Point(e.X, e.Y);
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}