问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Drawing.Imaging;namespace绘图{publicpartialclassFrm:Form{publicFrm(){InitializeComponent();}stringeditFileName;privateImagetheImage;privateGraphicsig;privatePointstartPoint,oldPoint;boolisDrawing=false;privateColorforeColor=Color.Black;privateColorbackColor=Color.White;privateenumdrawTools{Pen=0,Line,Ellipse,Rectangle,String,Rubber,None};privatedrawToolsdrawTool=drawTools.None;privatevoid新建ToolStripMenuItem_Click(objectsender,EventArgse){Graphicsg=this.CreateGraphics();g.Clear(backColor);toolStrip1.Enabled=true;theImage=newBitmap(this.ClientRectangle.Width,this.ClientRectangle.Height);editFileName="新建文件";this.Text="MyDrawt"+editFileName;ig=Graphics.FromImage(theImage);ig.Clear(backColor);}privatevoid打开ToolStripMenuItem_Click(objectsender,EventArgse){OpenFileDialogopenFileDialog=newOpenFileDialog();openFileDialog.Filter="ImageFiles(*.bmp;*.wmf;*.jgp)|*.bmp;*.wmf;*.jgp";openFileDialog.Multiselect=false;if(openFileDialog.ShowDialog()==DialogResult.OK){this.Text="MyDrawt"+openFileDialog.FileName;editFileName=openFileDialog.FileName;theImage=Image.FromFile(openFileDialog.FileName);Graphicsg=this.CreateGraphics();g.DrawImage(theImage,this.ClientRectangle);ig=Graphics.FromImage(theImage);ig.DrawImage(theImage,this.ClientRectangle);toolStrip1.Enabled=true;}}privatevoid保存ToolStripMenuItem_Click(objectsender,EventArgse){SaveFileDialogsaveFileDialog=newSaveFileDialog();saveFileDialog.Filter="图像(*.bmp)*.bmp";saveFileDialog.FileName=editFileName;if(saveFileDialog.ShowDialog()==DialogResult.OK){theImage.Save(saveFileDialog.FileName,ImageFormat.Bmp);}}privatevoid颜色ToolStripMenuItem_Click(objectsender,EventArgse){ColorDialogcolorDialog=newColorDialog();if(colorDialog.ShowDialog()==DialogResult.OK){foreColor=colorDialog.Color;}}privatevoidFrm_MouseDown(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Left){if(drawTool==drawTools.String){FrminputBox=newFrm();//Frm_inputBox=newFrm_Text();inputBox.StartPosition=FormStartPosition.CenterParent;if(inputBox.ShowDialog()==DialogResult.OK){Graphicsg=this.CreateGraphics();FonttheFont=this.Font;g.DrawString(inputBox.Text,theFont,newSolidBrush(foreColor),e.X,e.Y);ig.DrawString(inputBox.Text,theFont,newSolidBrush(foreColor),e.X,e.Y);}}elseif((isDrawing=!isDrawing)==true){startPoint=newPoint(e.X,e.Y);oldPoint=newPoint(e.X,e.Y);}}}privatevoidFrm_MouseMove(objectsender,MouseEventArgse){Graphicsg;g=this.CreateGraphics();if(isDrawing){switch(drawTool){casedrawTools.None:break;casedrawTools.Pen:g.DrawLine(newPen(foreColor,1),oldPoint,newPoint(e.X,e.Y));ig.DrawLine(newPen(foreColor,1),oldPoint,newPoint(e.X,e.Y));oldPoint.X=e.X;oldPoint.Y=e.Y;break;casedrawTools.Line:this.Frm_Paint(this,newPaintEventArgs(this.CreateGraphics(),this.ClientRectangle));break;casedrawTools.Ellipse:this.Frm_Paint(this,newPaintEventArgs(this.CreateGraphics(),this.ClientRectangle));g.DrawEllipse(newPen(foreColor,1),startPoint.X,startPoint.Y,e.X-startPoint.X,e.Y-startPoint.Y);break;casedrawTools.Rectangle:this.Frm_Paint(this,newPaintEventArgs(this.CreateGraphics(),this.ClientRectangle));g.DrawRectangle(newPen(foreColor,1),startPoint.X,startPoint.Y,e.X-startPoint.X,e.Y-startPoint.Y);break;casedrawTools.String:break;casedrawTools.Rubber:g.DrawLine(newPen(backColor,20),oldPoint,newPoint(e.X,e.Y));;ig.DrawLine(newPen(backColor,20),oldPoint,newPoint(e.X,e.Y));oldPoint.X=e.X;oldPoint.Y=e.Y;break;}}}privatevoidrubber_Click(objectsender,EventArgse){drawTool=drawTools.Rubber;}privatevoidtext_Click(objectsender,EventArgse){drawTool=drawTools.String;}privatevoidrectangle_Click(objectsender,EventArgse){drawTool=drawTools.Rectangle;}privatevoidline_Click(objectsender,EventArgse){drawTool=drawTools.Line;}privatevoidpen_Click(objectsender,EventArgse){drawTool=drawTools.Pen;}privatevoidFrm_MouseUp(objectsender,MouseEventArgse){isDrawing=false;switch(drawTool){casedrawTools.Line:ig.DrawLine(newPen(foreColor,1),startPoint,newPoint(e.X,e.Y));break;casedrawTools.Ellipse:ig.DrawEllipse(newPen(foreColor,1),startPoint.X,startPoint.Y,e.X-startPoint.X,e.Y-startPoint.Y);break;casedrawTools.Rectangle:ig.DrawRectangle(newPen(foreColor,1),startPoint.X,startPoint.Y,e.X-startPoint.X,e.Y-startPoint.Y);break;}}privatevoidFrm_Paint(objectsender,PaintEventArgse){Graphicsg=this.CreateGraphics();if(theImage!=null){g.Clear(Color.White);g.DrawImage(theImage,this.ClientRectangle);}}privatevoidFrm_SizeChanged(objectsender,EventArgse){this.Frm_Paint(this,newPaintEventArgs(this.CreateGraphics(),this.ClientRectangle));}}}
解决方案
解决方案二:
由于鼠标滑动,可能会先执行MouseMove事件;单步执行MouseMove事件吧;看看是不是哪里的问题
解决方案三:
你需要用一个数据结构存储画图的数据,否则Paint上去的东西窗体被覆盖就会擦掉了。