C # Graphic draws circles, triangles, ovans, and images,

Source: Internet
Author: User

C # Graphic draws circles, triangles, ovans, and images,

You can draw graphs, line segments, circles, texts, and graphs on form and panel.
The drawing code must be placed in the OnPaint () function, because this function is called when the form is refreshed, And the painted graph is refreshed again.
The sample code draws a graph on the Panel to briefly describe the line and drawing principle.

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. IO; using System. reflection; namespace TestGraphic {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void panel1_Paint (object sender, PaintEventArgs e) {Graphics gc = e. graphic S; // set the Color of the drawing. Brush greenBrush = new SolidBrush (Color. green); int radius = 30; // draw a circle, (0, 0) is the coordinate of the upper left corner, radius is the diameter gc. fillEllipse (greenBrush, 0, 0, radius, radius); Brush yellowBush = new SolidBrush (Color. yellow); // draw an ellipse. In fact, when the circle is a special one, that is, the two fixed points overlap, (50, 60) is the coordinate of the upper left corner, // 70-bit elliptical width, 100-bit elliptical height gc. fillEllipse (yellowBush, 50, 60, 70,100); // draw a triangle, specifying the red color and line width 5. The three vertices are (150,160) (200,210) (280,180) and draw three links. Pen pen = new Pen (Color. red, 5); gc. drawLine (pen, 150,160,200,210); gc. drawLine (pen, 200,210,280,180); gc. drawLine (pen, 150,160,280,180); // draw a rectangle, (50,300) coordinate in the upper left corner, 110 width, 80 is the height. Gc. drawRectangle (pen, 50,300,110, 80); Brush blueBrush = new SolidBrush (Color. blue); // draw text gc. drawString ("Graphic example", new Font ("", 20, FontStyle. italic), blueBrush, new PointF (300,400); // draw the image TestGraphic. properties. resources. niang is the name of the image in the resource. You can set the image as the background image of the Panel, // obtain the image name, and then clear the background image of the Panel. (300,300) is the coordinate of the upper left corner of the image, and is the width and height of the image to be displayed. // It is not the width and height of the image. Image image = global: TestGraphic. Properties. Resources. niang; gc. DrawImage (image, new Rectangle (400, 20,300,300 ));}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.