Original: C # Graphic drawing circles, triangles, ellipses, pictures
You can draw graphics, line segments, circles, text, graphics, and so on in form and panel.
The drawing code must be placed inside the OnPaint () function, because the function is called when the form refreshes, and the drawing is refreshed again.
The sample code draws a graphic on the panel to simply describe the plotting line and the drawing principle.
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Reflection;namespacetestgraphic{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidPanel1_paint (Objectsender, PaintEventArgs e) {Graphics GC=E.graphics; ///set the color of a drawingBrush Greenbrush =NewSolidBrush (Color.green); intRadius = -; //Draws a circle, (0, 0) is the coordinate of the upper-left corner, radius is the diameterGc. FillEllipse (Greenbrush,0,0, radius, radius); Brush Yellowbush=NewSolidBrush (Color.yellow); //The ellipse is drawn, in fact, a special kind of ellipse, that is, two fixed-point coincident, (50, 60) is the coordinates of the upper left corner,//width of 70-bit ellipse, height of 100-bit ellipseGc. FillEllipse (Yellowbush, -, -, -, -); //draws a triangle, specifying red and line width 5. Three vertices are (150,160) (200, 210) (280, 180), and three lines are drawn. Pen pen =NewPen (Color.Red,5); Gc. DrawLine (pen, Max, the, $, About); Gc. DrawLine (pen, $, About,280, the); Gc. DrawLine (pen, Max, the,280, the); ///Draws a rectangle, (50,300) the upper-left coordinate, 110-bit width, and 80 height. Gc. DrawRectangle (pen, -, -, the, the); Brush Bluebrush=NewSolidBrush (Color.Blue); ///Draw TextGc. DrawString ("graphic Examples of drawing graphs",NewFont ("Song Body", -, Fontstyle.italic), Bluebrush,NewPointF ( -, -)); ///Draw picture TestGraphic.Properties.Resources.niang as the name of the picture in the resource, you can set the picture to the panel's background map first ,///get the name of the picture, and then empty the panel's background image. (400,20) is the upper-left coordinate of the picture, and 300,300 is the width and height at which the picture will be displayed.///is not the width and height of the picture itself. Image image =Global:: TestGraphic.Properties.Resources.niang; Gc. DrawImage (Image,NewRectangle ( -, -, -, -)); } }}
C # Graphic draw circles, triangles, ellipses, pictures