Graphics.DrawLine and PictureBox control

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

Guest

I have a PictureBox, between a couple of other controls, in which I use the
DrawLine() function to show links between the controls. There are splitter
bars to either side of the PictureBox to allow it's width to be changed. If
the width is increased, I recalculate the new X-value for the endpoint as I
want the line to be the full width of the PictureBox. Unfortunately, the
X-value I end up with is the same as the width of the PictureBox before it is
resized, rather than after it's resized. Under what event do I need to place
my code to calculate the desired X-value?

Thanks for all your help!

Allen
 
I assume that you are drawing the line in a Paint event handler for the
PictureBox? If the X-value calculation is not computationally intensive then
you could just perform the calculation just before you call DrawLine() in
the Paint handler.
 
BTW, I put the code to recalculate the endpoint of the line in the
PictureBox_Resize event.
 
I tried calculating it both in the Paint and the Resize events. The
calculations for the startpoint and endpoint of the line, which are performed
just prior to calling DrawLine, are as follows:

startPt = new Point(0, sourceStartPt.Y);
endPt = new Point(pictureBox1.Width, destinationEndPt.Y);

As you can see, they're definitely NOT computationally-intensive. This is
driving me absolutely nuts! LOL
 
Can you post the code that you're currently using and I'll take a better
look at it?
 
Tim,

Here's the code; Point panelStart and Point panelEnd are defined external to
the function:

private void pictureBox1_Resize(object sender, EventArgs e)
{
panelStart = new Point(0, sourceStartPoint.Y +
sourceTree1.customTree.Top);
panelEnd = new Point(pictureBox1.Width, destStartPoint.Y +
destTree1.mirroredTree.Top);

Graphics panelGraphics = pictureBox1.CreateGraphics();
panelGraphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1),
panelStart, panelEnd);
}
 
It would probably be best to put your drawing code into a Paint event
handler instead of a Resize handler. What if your application was minimized
and then restored? The line that was drawn in the Resize handler will
disappear. The code below draws a black line through the middle of the
control. You can just replace the Point calculations with your
implementation.

private void pictureBox1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
Rectangle clientRect = this.pictureBox1.ClientRectangle;
Point p1 = new Point(clientRect.X, (clientRect.Height / 2));
Point p2 = new Point(clientRect.Width, (clientRect.Height / 2));
e.Graphics.DrawLine(Pens.Black, p1, p2);
}

Although the code above would work as expected against, for example, a
Button control, you might need to set the SizeMode property of the
PictureBox to "StretchImage" to get it to paint properly. It must be
something internally with the way that the resizing or invalidation is
performed.
this.pictureBox1.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.StretchImage;
 
Tim,

I tried both of your ideas and they didn't work. One thing I did notice is
that if I increased the width of the PictureBox while my app is running and
then both maximized and minimized VS, the line showed up as being drawn the
full width of the PictureBox. I'm at my wit's end as to why this is
happening.

Thanks for your help.

Allen
 
The code that I posted definitely worked at my end. What version of the
framework are you programming against (1.0 or 1.1)? I am assuming that you
have the latest service pack applied for the appropriate framework? What do
you expect to see when the line is drawn? Maybe if you zip your entire
project (if it's not to large), as-is, and attach it so that I can fully see
what's going on.
 
Back
Top