Drawline Offline Buffer Question

Hi everyone. I have a situation where I have a method that is a draw line tool. You press the mouse down, drag and it draws a line. The Mouse UP captures the AnchorX/Y position and the drag captures the EndX/EndY positions. That works very nicely until I try to move it over into an offline buffered picture.

My issue is when I instantiate a new picture with DrawLineToolBuffer = new picture(Width,Height) the width/height of my drawing is the W/H of my canvas. I know that is because I am telling it to do that with (width/height). I am struggling to find the right variable to use as the W/H to dynamically give me the proper height and width of the actual drawn line. I have tried combinations of add/subtracting the EndX/Y and AnchorX/Y with no success. Any ideas on this one?

  • The DrawLineToolBuffer picture is declared as a property
  • This method is called out of my Canvas’ paint event:

Sub mDrawLineMethod(g as graphics) DrawLineToolBuffer = new picture(Width,Height) DrawLineToolBuffer.Graphics.ForeColor =CampusMapWindow.kBlack DrawLineToolBuffer.Graphics.PenHeight = 1 DrawLineToolBuffer.Graphics.PenWidth = 5 DrawLineToolBuffer.Graphics.DrawLine(LineToolAnchorX-4,LineToolAnchorY+10,LineToolEndX-4,LineToolEndY+10) g.DrawPicture(DrawLineToolBuffer,0,0)

Thanks again!

confused as to what you are attempting to accomplish.

Are you trying to draw a following the mouse line?

  1. MouseDown… set anchor Point
  2. MouseDrag… draw line on CANVAS from anchor to current mouse point, “erasing” the last drawn line
  3. MouseUp… make the line permenant from anchor point to mouseup location

If so…

  1. MouseDown… isdrawing=true, keep X,Y in temp varialbes… store the canvas in a “picture” … better yet… do ALL your work in a picture
  2. MouseDrag… keep X1,Y1 in temp variable. invalidate canvas
  3. MouseUp… isdrawing=false, update picture with line from X,Y to X1,Y1, invalidate canvas

Canvas Paint Event
drawpicture picturebuffer,0,0
if isdrawing the g.drawline x,y,x1,y1

Dave thanks for the reply. Essentially yes I am doing those steps and my lines are drawing nicely except the height and width of picturebuffer are huge since I used “New picture(width , height)” above instead of the actual h/w of the drawn line. I am stuck there if that makes since?

width should be the absolute value of the difference in X, and height the same for Y.

Then you need to offset the lines coordinates so it draws within those bounds. I think just use the min X and Y from either endpoint. Then draw the picture using that same offset.

[code]w = Abs(x1 - x2)
h = Abs(y1 - y2)

offX = Min(x1, x2)
offY = Min(y1, y2)

buffer = new Picture(w, h)

buffer.graphics.drawLine(x1-offX, y1 - offY, x2-offX, y2-offY)

finalPic.graphics.drawPicture(buffer, offX, offY)[/code]

This doesn’t account for line thickness

Thanks Doofus. That is what I am hopefully looking for (my missing link was offset! ) Thanks!