问题描述
//画笔参数Penpen=newPen(Color.Black,1);Colorcolor=Color.Black;floatwidth;//直线参数Pointstartpoint;Pointcurrentpoint;boolmousedown=false;//画图GraphicsPathgPath=newGraphicsPath();Graphicsgs;Bitmapbitmap;privatevoidForm1_Load(objectsender,EventArgse){bitmap=newBitmap(SystemInformation.PrimaryMonitorMaximizedWindowSize.Width,SystemInformation.PrimaryMonitorMaximizedWindowSize.Height);gs=Graphics.FromImage(bitmap);gs.Clear(Color.White);gs.Dispose();}privatevoidtrackBar1_ValueChanged(objectsender,EventArgse){width=trackBar1.Value;}privatevoidcolorPickerButton1_SelectedColorChanged(objectsender,EventArgse){color=colorPickerButton1.SelectedColor;}privatevoidpanel1_MouseDown(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Left){mousedown=true;pen=newPen(color,width);startpoint=newPoint(e.X,e.Y);}}privatevoidpanel1_MouseMove(objectsender,MouseEventArgse){if(mousedown&&e.Button==MouseButtons.Left){currentpoint=newPoint(e.X,e.Y);Graphicsgs=panel1.CreateGraphics();gs.DrawImage(bitmap,0,0);gs.DrawLine(pen,startpoint,currentpoint);gs.Dispose();}}privatevoidpanel1_MouseUp(objectsender,MouseEventArgse){if(mousedown){gPath.AddLine(startpoint,currentpoint);Graphicsgs=Graphics.FromImage(bitmap);gs.DrawPath(pen,gPath);}}privatevoidpanel1_Paint(objectsender,PaintEventArgse){e.Graphics.DrawImage(bitmap,0,0);}画到第三条先,自动连接第一条和第二条直线的;画第四条直线自动连接第二条和第一条直线的!~希望大侠帮帮忙,看看出了什么问题!
解决方案
解决方案二:
Pointlastpoint=Point.Empty;//<<<<<<addprivatevoidpanel1_MouseMove(objectsender,MouseEventArgse){if(mousedown&&e.Button==MouseButtons.Left){currentpoint=newPoint(e.X,e.Y);Graphicsgs=panel1.CreateGraphics();gs.DrawImage(bitmap,0,0);gs.DrawLine(pen,startpoint,currentpoint);if(lastpoint!=Point.Empty)//<<<<<<addgs.DrawLine(pen,startpoint,lastpoint);//<<<<<<addgs.Dispose();}}privatevoidpanel1_MouseUp(objectsender,MouseEventArgse){if(mousedown){lastpoint=currentpoint;//<<<<<<addgPath.AddLine(startpoint,currentpoint);Graphicsgs=Graphics.FromImage(bitmap);gs.DrawPath(pen,gPath);}}