First things first,the method I am giving does not require a custom control in order to work,it makes use of two of the events provided by PictureBox to run the logic,but if still you need a custom control you should be able to do that by porting these methods to that control with a little coding.
The following are two fields that will hold most of the values in our logic,
private Point horizontalstart, horizontalend;
private Point verticalstart, verticalend;
private bool drawlines;//Specifies a value whether PictureBox should redraw itself.
Let me explain what are these points for;
The first Point statement holds starting and end point of Horizontal line,and
the second Point statement holds starting and end point of Vertical line.
When you'r done with it write this code in MouseMoveEvent
of your PictureBox
;
private void SourcePictureBox_MouseMove(object sender, MouseEventArgs e)
{
if(drawlines==true)//If the lines should move.
{
horizontalstart = new Point(0, e.Y);//Start point of Horizontal line.
horizontalend = new Point(SourcePictureBox.Width, e.Y);//End point of Horizontal line.
verticalstart = new Point(e.X, 0);//Start point of Vertical line
verticalend = new Point(e.X, SourcePictureBox.Height);//End point of Vertical line.
SourcePictureBox.Invalidate();//Force PictureBox to repaint.
}
else if(drawlines==false)//To lock the lines at current coordinates.
{
//Add any code if needed.
}
}
where SourcePictureBox
is the PictureBox that we are operating upon.As soon as the MouseMoveEvent
is raised new coordinates for both lines are calculated and a repaint message is sent to PictureBox,which draws the lines using the coordinates declared and calculated above.
Ok so now write the code below in the Paint
event of your PictureBox;
private void SourcePictureBox_Paint(object sender, PaintEventArgs e)
{
if (SourcePictureBox.Image != null)
{
e.Graphics.DrawLine(SystemPens.WindowText, horizontalstart, horizontalend);//Draw Horizontal line.
e.Graphics.DrawLine(SystemPens.WindowText, verticalstart, verticalend);//Draw Vertical line.
}
}
One more thing that i noticed in your image,the guidelines the image is showing are a little small than the image,if this is the case,then replace both the statements in MouseMoveEvent
event excluding the Invalidate()
one with the following;
horizontalstart = new Point(0+10, e.Y);//Start point of Horizontal line.
horizontalend = new Point(SourcePictureBox.Width-10, e.Y);//End point of Horizontal line.
verticalstart = new Point(e.X, 0+10);//Start point of Vertical line
verticalend = new Point(e.X, SourcePictureBox.Height-10);//End point of Vertical line.
But what's new in these statements,nothing except they increase the start point of both lines by value 10(int) and decrease the end points with value 10,thus giving you some space between the edges of PictureBox and the guiding lines.Please note if you want to increase or decrease the space replace value 10 with a value you would like but keep in mind the value must not be bigger than half of the size of your PictureBox.
The other thing i would like to address is if you want to change the colour of guiding lines
,replace the first argument of both statements in Paint Event
with any of values offered by class SystemPens.If there is nothing in the class that satisfies you,you are always free to create on for yourself.If you know how to do it,then its the best choice,incase if you need some help,take a look here.Creating a new pen gives you the option of defining a width for the guilding lines.
Update
`At anytime,to stop the lines from moving,set drawlines(declared above) to false,to resume their functionality set it to true.
It's not a foolproof method but I Hope it's enough to get your job done,and don't forget to rename the PictureBox and associated events with the name of your PictureBox.