4
Reply

[SOLVED]Continuous free hand drawing effect in C#

Ask a question
asim mittal

asim mittal

14y
12.1k
1
Hey,

I wrote a small app that allows you to scribble over a transparent panel using C#. The idea is very raw and simple at the moment. if the mouse move event occurs with the left button pressed, then the current cursor position is marked with a dot (actually a small sized rectangle which is filled with color).

However, i realize that when i move my mouse really fast, the dots get discontinuous. So instead of getting a smooth straight line, i end up with a series of dots. I used MS Paint, and they managed to get the drawing of freehand curves and lines really smoothly, no matter how fast the cursor is moved. How can i achieve a similar effect. The relevant part of my code is published below:

 private void paintCurrentPosition(int thickness, Color colorPen)
{
/*
* this method is responsible for creating a single spot on the current position of the cursor. The cursor position as indicated
* by Cursor.Position is not accurately placed at the tip, so an offset is being employed to make sure that the spot appears at the tip of
* the cursor
*/

int x, y; //x,y will be the values offset from the current position of the cursor
int offsetX = 0; //offset for x
int offsetY = 25; //offset for y
int i=0, j=0, totCount;

SolidBrush brush = new SolidBrush(colorPen);
graphObj = this.CreateGraphics();
graphObj.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

x = Cursor.Position.X - offsetX; y = Cursor.Position.Y - offsetY;
graphObj.FillRectangle(brush, x + i, y + i, thickness, thickness);

}

private void frmAnnotate_MouseMove(object sender, MouseEventArgs e)
{
if (Control.MouseButtons == MouseButtons.Left)
{
paintCurrentPosition(penThickness,penColor);
}
}

I have tried making a smaller point by reducing the width of the rectangle. I somehow feel, that when i move the mouse, the mouseMove event is not called for every coordinate the cursor passes over, and that is why the discontinuity occurs. But in that event, how do i rectify this?

Any help is appreciated. Thanks,
AM

Answers (4)