My artboard.
See the implementation diagram first
Implementation process
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.drawing.imaging;using system.linq;using system.text;using system.threading.tasks;using system.windows.forms;//********************** Jin Yan electronic ****************************************namespace huaban{ Public partial class Form1:form {public Form1 () {InitializeComponent (); toolstrip1.enabled = false; ForeColor = Color.Black; BackColor = Color.White; } Color BackColor = new color (); private void new Toolstripmenuitem_click (object sender, EventArgs e) {Graphics g = this. CreateGraphics (); G.clear (BackColor); Toolstrip1.enabled = true; Create a Bitmap theimage = new Bitmap (this. Clientrectangle.width, this. Clientrectangle.height); Editfilename = "New File"; Modify the window caption this. Text = "Mydraw\t" + EditfiLename; IG = Graphics.fromimage (theimage); Ig. Clear (BackColor); } String editfilename = ""; Private Image theimage; Public Graphics IG {get; private set;} private void Open Toolstripmenuitem_click (object sender, EventArgs e) {Openfiledialog1.multiselect = False ; if (openfiledialog1.showdialog () = = DialogResult.OK) {//Modify window caption this. Text = "mydraw\t" + openfiledialog1.filename; Editfilename = Openfiledialog1.filename; Theimage = Image.FromFile (openfiledialog1.filename); Graphics g = this. CreateGraphics (); G.drawimage (Theimage, this. ClientRectangle); IG = Graphics.fromimage (theimage); Ig. DrawImage (Theimage, this. ClientRectangle); Toolbar can be used with toolstrip1.enabled = true; }} private void Save Toolstripmenuitem_click (objectsender, EventArgs e) {savefiledialog1.filter = "image (*.bmp) |*.bmp"; Savefiledialog1.filename = Editfilename; if (savefiledialog1.showdialog () = = DialogResult.OK) {theimage.save (Savefiledialog1.filename, I Mageformat.bmp); This. Text = "mydraw\t" + savefiledialog1.filename; Editfilename = Savefiledialog1.filename; }} private void Form1_paint (object sender, PaintEventArgs e) {//To draw images saved in image Graphics g = this. CreateGraphics (); if (theimage! = null) {g.clear (color.white); G.drawimage (Theimage, this. ClientRectangle); }} Color ForeColor = new color (); private void Form1_mousedown (object sender, MouseEventArgs e) {if (E.button = = MouseButtons.Left) {//If you select text input, open the Strinput form if (drawtool = = (int) drawtools.string) {Frm_text InputBox = new Frm_text (); Inputbox.startposition = formstartposition.centerparent; if (inputbox.showdialog () = = DialogResult.OK) {Graphics g = this. CreateGraphics (); Font Thefont = this. Font; g.DrawString (InputBox.textBox1.Text, Thefont, New SolidBrush (ForeColor), e.x, e.y); Ig. DrawString (InputBox.textBox1.Text, Thefont, New SolidBrush (ForeColor), e.x, e.y); }}//If you start drawing, start recording mouse position else if ((isdrawing =!isdrawing) = = True) {startPoint = new Point (e.x, e.y); Oldpoint = new Point (e.x, e.y); }}} int drawtool = 0; Enum Drawtools {None = 0, Pen = 1, line = 2, Ellipse = 3, Rectangle = 4, String = 5, Rubber =6} point Oldpo int = new Point (); Point startPoint = new Point (); private void Form1_mousemove (object sender, MouseEventArgs e) {Graphics g; g = this. CreateGraphics (); if (isdrawing) {switch (Drawtool) {case (int) Drawtools.none: Break case (int) Drawtools.pen://Draw segment G.drawline from the previous point to the current point (new Pen (ForeColor, 1), Oldpoint, New Point (e.x, e.y)); Ig. DrawLine (New Pen (ForeColor, 1), Oldpoint, New Point (e.x, e.y)); Oldpoint.x = e.x; Oldpoint.y = e.y; Break case (int) Drawtools.line://First restore the image before this operation, and then add line this. Frm_main_paint (This, New PaintEventArgs (this. CreateGraphics (), this. ClientRectangle)); G.drawline (New Pen (ForeColor, 1), StartPoint, New Point (e.x, e.y)); Break case (int) Drawtools.ellipse://First restore the image before this operation, and then add Ellipse this. Frm_main_paint (this, new PaintEventArgs. CreateGraphics (), this. ClientRectangle)); G.drawellipse (New Pen (ForeColor, 1), Startpoint.x, Startpoint.y, E.x-startpoint.x, E.Y-STARTPOINT.Y); Break case (int) Drawtools.rectangle://First restore the image before this operation, and then add Rectangle this. Frm_main_paint (this, new PaintEventArgs. CreateGraphics (), this. ClientRectangle)); G.drawrectangle (New Pen (ForeColor, 1), Startpoint.x, Startpoint.y, E.x-startpoint.x, E.Y-STARTPOINT.Y); Break case (int) drawtools.string: Break case (int) Drawtools.rubber://Draw wide segment G.drawline with background color (new Pen (BackColor, 20), Oldpoint, New Point (e.x, e.y)); Ig. DrawLine (New Pen (BackColor), Oldpoint, New Point (e.x, e.y)); Oldpoint.x = e.x; Oldpoint.y = e.y; Break }}} bool isdrawing = FALSE; private void Form1_mouseup (object sender, MouseEventArgs e) {isdrawing = false; Switch (drawtool) {case (int) DrawTools.Line:ig. DrawLine (New Pen (ForeColor, 1), StartPoint, New Point (e.x, e.y)); Break case (int) DrawTools.Ellipse:ig. DrawEllipse (New Pen (ForeColor, 1), Startpoint.x, Startpoint.y, E.x-startpoint.x, E.Y-STARTPOINT.Y); Break case (int) DrawtoolS.rectangle:ig. DrawRectangle (New Pen (ForeColor, 1), Startpoint.x, Startpoint.y, E.x-startpoint.x, E.Y-STARTPOINT.Y); Break }} private void Frm_main_paint (object sender, PaintEventArgs e) {//To draw images saved in image Graphics g = this. CreateGraphics (); if (theimage! = null) {g.clear (color.white); G.drawimage (Theimage, this. ClientRectangle); }} private void Toolstripbutton1_click (object sender, EventArgs e) {Toolstripbutton6.che cked = false; toolstripbutton2.checked = false; toolstripbutton3.checked = false; toolstripbutton4.checked = false; toolstripbutton5.checked = false; if (Drawtool = = (int) drawtools.pen) {drawtool = (int) drawtools.none; toolstripbutton1.checked = false; } else {drawtool = (int) Drawtools.pen; Toolstripbutton1.checked = true; }} private void Toolstripbutton2_click (object sender, EventArgs e) {toolStripButton1. Checked = false; toolstripbutton6.checked = false; toolstripbutton3.checked = false; toolstripbutton4.checked = false; toolstripbutton5.checked = false; if (Drawtool = = (int) drawtools.line) {drawtool = (int) drawtools.none; toolstripbutton2.checked = false; } else {drawtool = (int) drawtools.line; Toolstripbutton2.checked = true; }} private void Toolstripbutton3_click (object sender, EventArgs e) {Toolstripbutton1.che cked = false; toolstripbutton2.checked = false; toolstripbutton6.checked = false; toolstripbutton4.checked = false; toolstripbutton5.checked = false; if (Drawtool = = (int) drawtools.rectangle) {drawtool = (int) drawtools.none; toolstripbutton3.checked = false; } else {drawtool = (int) drawtools.rectangle; Toolstripbutton3.checked = true; }} private void Toolstripbutton4_click (object sender, EventArgs e) {ToolStrip button1.checked = false; toolstripbutton2.checked = false; toolstripbutton3.checked = false; toolstripbutton6.checked = false; toolstripbutton5.checked = false; if (Drawtool = = (int) drawtools.ellipse) {drawtool = (int) drawtools.none; toolstripbutton4.checked = false; } else {drawtool = (int) drawtools.ellipse; Toolstripbutton4.checked = True; }} private void Toolstripbutton5_click (object sender, EventArgs e) {toolstripbutton1.c hecked = false; toolstripbutton2.checked = false; toolstripbutton3.checked = false; toolstripbutton4.checked = false; toolstripbutton6.checked = false; if (Drawtool = = (int) drawtools.string) {drawtool = (int) drawtools.none; toolstripbutton5.checked = false; } else {drawtool = (int) drawtools.string; Toolstripbutton5.checked = true; }} private void Toolstripbutton6_click (object sender, EventArgs e) {toolstripbutton1.c hecked = false; toolstripbutton2.checked = false; toolstripbutton3.checked = false; toolstripbutton4.checked = false; toolstripbutton5.checked = false; if (Drawtool = = (int) drawtools.Rubber) {drawtool = (int) drawtools.none; toolstripbutton6.checked = false; } else {drawtool = (int) drawtools.rubber; Toolstripbutton6.checked = true; }} private void color Toolstripmenuitem_click (object sender, EventArgs e) {if (ColorDialog 1.ShowDialog () = = DialogResult.OK) {foreColor = colorDialog1.Color; } } }}
Jin Yan Electronics
C # My little artboard