[GDI + programming] -- Summary + practice

Source: Internet
Author: User

Zookeeper

I wrote a few digress in my previous blog, why did I discuss events and delegation at this time. For events and delegation, yes. NET programmers must master a class. It is a milestone for programmers to step into the design rather than writing. Once they understand the formation of an event, they can use code to write dynamically created events, but after learning to entrust, we can truly step into the design stage.

Now, let's go back to the topic of our article and start our journey of GDI + from the back of the article. Today, we have a lot of basic architecture for GDI +, A cylindrical shape is drawn using several basic classes of GDI +. The focus is to differentiate the relationship between Graphics and GraphicsPath through instances.

The division of common classes of GDI +, through which you still cannot understand the mysteries of GDI +, is a good start ......



Maybe you have to ask why there are two types of Graphics and GraphicsPath. Why? To fundamentally distinguish the two, we must first look at their role. Graphics is in the System. A class in the Drawing namespace is mainly used to display graphical interfaces. It also encapsulates a variety of Drawing methods and also includes Editing graphics.
 
Next, let's look at the GraphicsPath class. The biggest difference between this class and Graphics is that the two belong to different namespaces, And the GraphicsPath class is System. drawing. classes in the Drawing2D namespace provide a series of interconnected straight lines and curves, which provide better plotting functions than Graphics classes and are flexible.



Why are there two types of paint and SolidBrush color classes? Because SolidBrush is a subclass of the Brush class and a Brush is an abstract class, you can only instantiate the SolidBrush class to fill the color in a specific method.
The same Bitmap and image are also a parent-child relationship. Bitmap inherits the abstract parent class image and provides users with images defined by pixel data.
Note: During programming, we often save the image to be drawn in a Bitmap object so that the image remains unchanged.
 


Why do we need to say that the region class does not belong to the graphic drawing class? Yes, it specifies a path area so that we can operate on the area, this class can be used flexibly to obtain the desired region in the source image. It also provides features such as area enlargement and scaling.

 
The class categories used in common GDI + programming are summarized above, but some classes are not involved, such as Rectangle and Point, which can encapsulate the GDI + primitive, the Pen class is used to draw straight lines and curves.


We have summarized several types above. Next we will provide an example to use GDI + to draw a cylindrical shape, so as to gain a deep understanding of several types of usage methods in practice.

Idea of drawing: first, use the GraphicsPath class to draw a closed graph, and then draw the upper and lower sides of the cylindrical ellipse.

Listing 1: Draw a closed area of a cylindrical, use the GraphicsPath class to draw a closed area, use the CloseFigure method to close the area, and finally use the Graphics class to fill the display area.


Private void DrawClosedFigure (PaintEventArgs e) {Graphics g = e. graphics; GraphicsPath gp = new GraphicsPath (); Point p = new Point (20, 20); // coordinate of the starting Point of the square where the rectangle is located Size s = new Size (540,380 ); // float flaHeight = (float) s. width/(float) 6.75; // obtain the height float flaRecHeight = (float) s of the upper elliptic. height/(float) 2.62; // obtain the half of the Height of the rectangle in which the upper elliptic is located. int intHeight = Convert. toInt16 (Math. round (flaHeight, 0); int intRecHeight = Convert. toInt16 (Math. round (flaRecHeight, 0); Rectangle rect = new Rectangle (p, s); // create a rectangular area to draw a cylindrical gp. addArc (rect. x, rect. Y + intHeight, s. width, intRecHeight, 180,180); // draw a cylindrical upper half gp. addArc (rect. x, rect. Y + rect. height, s. width, intRecHeight, 0,180); // draw the cylindrical lower half gp. closeFigure (); // close the g. smoothingMode = SmoothingMode. highQuality; // set the border to smooth g. drawPath (new Pen (Color. fromArgb (213,204,186), 6), gp); // draw the area of the drawing board to draw the desired BOUNDARY g. fillPath (new SolidBrush (Color. fromArgb (245,237,229), gp); // fill the interior of the canvas area}

List 2: Draw a cylindrical bottom-up and bottom-down oval. The bottom-up is not filled, and it is not a closed oval, so it looks more vivid.


Private void DrawBottom (PaintEventArgs e) {Graphics g = e. graphics; Point p = new Point (20, 20); // coordinate of the starting Point of the square where the rectangle is located Size s = new Size (540,380 ); // float flaHeight = (float) s. width/(float) 6.75; // obtain the height float flaRecHeight = (float) s of the upper elliptic. height/(float) 2.62; // obtain the half of the Height of the rectangle in which the upper elliptic is located. int intHeight = Convert. toInt16 (Math. round (flaHeight, 0); int intRecHeight = Convert. toInt16 (Math. round (flaRecHeight, 0); Rectangle rect = new Rectangle (p, s); // create a rectangular area to draw a cylindrical g. fillEllipse (new SolidBrush (Color. fromArgb (213,204,186), rect. x-2, rect. Y + rect. height, s. width + 3, intRecHeight); // draw a cylindrical bottom g. drawArc (new Pen (Color. fromArgb (213,204,186), 3), new Rectangle (rect. x, rect. Y + intHeight, s. width, intRecHeight), 170, 10); // draw the left g of the cylindrical bottom. drawArc (new Pen (Color. fromArgb (213,204,186), 3), new Rectangle (rect. x, rect. Y + intHeight, s. width, intRecHeight), 0, 10); // draw the right of the cylindrical bottom}
The most important part of the above method is not described. It is about the ratio of the height of the cylindrical column to the elliptic ratio of the bottom. This ratio is very important. With this ratio, the ellipse is beautiful ......


Based on the basis of GDI +, This article summarizes several common types in the Process of GDI + programming. By classification, we can clearly distinguish the usage of several types, allows you to use multiple types of images to create images flexibly. In addition, there are many practices to deeply understand the usage between them in practice, so that they can be easily used.

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.