free pen movement

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi! i am trying to make a simple version of paint and i was woundering how i
could make the the free pen so i can draw whatever i like not only lines or
squears. i would really apreciate if i coud somehow use the coordonates of
the maus on maus down event so it fill the pixels as logn as the maus is
pressed and for every coordonate the pen gets when the mause is pressed.
tnx for the help
 
Hi Alexandru,
here is a simple example of how you can use the PictureBox class to draw a
freestyle line:

public partial class Canvas : PictureBox
{
private Bitmap _canvas;
private bool _isMouseDown = false;

public Canvas()
{
InitializeComponent();

Width = 200;
Height = 200;

_canvas = new Bitmap(200, 200);

MouseDown += new MouseEventHandler(Canvas_MouseDown);
MouseUp += new MouseEventHandler(Canvas_MouseUp);
MouseMove += new MouseEventHandler(Canvas_MouseMove);

Image = _canvas;
}

void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (_isMouseDown)
{
using (Graphics g = Graphics.FromImage(_canvas))
using(Brush b = new SolidBrush(Color.Red))
{
g.FillEllipse(b, e.X, e.Y, 5, 5);
}

this.Invalidate();
}
}

void Canvas_MouseUp(object sender, MouseEventArgs e)
{
_isMouseDown = false;
}

void Canvas_MouseDown(object sender, MouseEventArgs e)
{
_isMouseDown = true;
}
}

Hope that helps
Mark Dawson
http://www.markdawson.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top