C # GDI + simple Drawing (ii)

Source: Internet
Author: User
In the last piece has been introduced to you how to use GDI + to draw a simple image, this piece continues to introduce some other drawing knowledge.

1. First we look at the pen we used in the last piece.

The properties of a pen are: color, dashcap (dash End shape), DashStyle (dash style), endcap (tail shape), startcap (thread shape), Width (thickness), etc.

We can use a pen to draw dashed lines, straight lines with arrows, etc.

Pen  p = new  pen (Color.Blue, 5);//Set the weight of the pen to the color blue graphics  g = this. CreateGraphics ();//Draw dashed p. DashStyle = dashstyle.dot;//defines the dash style as Point G. DrawLine (P, 10, 10, 200, 10);//Custom dashed p. DashPattern = new  float[] {2, 1};//sets the dash and the empty part of the array G. DrawLine (P, 10, 20, 200, 20);//draw arrows, only useful p for non-closed curves. DashStyle = dashstyle.solid;//Restores the solid line p. Endcap = linecap.arrowanchor;//Defines the style of the end of the line as the arrow G. DrawLine (P, N, (), G.dispose ();p. Dispose ();

The above code runs the result:

2. Next we look at the use of brush

Function: We can fill various graphic shapes with brush, such as rectangle, ellipse, sector, polygon and closed path, there are several different types of brush:

SolidBrush: The simplest form of painting, painted with a solid color

HatchBrush: Similar to SolidBrush, but you can use this class to select a pattern to use from a large number of preset patterns, rather than a solid color

TextureBrush: Drawing with textures (like)

LinearGradientBrush: Drawing with two colors blended along a gradient

PathGradientBrush: Drawing with complex blend color gradients based on a unique path defined by the programmer

Here are just a few examples of how to use them:

Graphics g = this. CreateGraphics (); Rectangle rect = new Rectangle (10, 10, 50, 50);//define Rectangle, parameter is start horizontal ordinate with its length and width/monochrome fill SolidBrush B1 = new SolidBrush (color.blue);//Set Semantic monochrome Brush          g.fillrectangle (B1, rect);//fill this rectangle//String G. DrawString ("string", New Font ("Arial", "Ten"), B1, New PointF (90, 10));//fill with picture TextureBrush b2 = new TextureBrush (Image.FromFile (@ " E:\picture\1.jpg ")); rect. Location = new Point (10, 70);//change the start coordinate of this rectangle rect. width = 200;//Changes the width of the rectangle to rect. Height = 200;//Changes the high g of this rectangle. FillRectangle (B2, rect);//fills the rect with a gradient color. Location = new Point (10, 290); LinearGradientBrush B3 = new  LinearGradientBrush (rect, Color.yellow, Color.Black, Lineargradientmode.horizontal) ; G.fillrectangle (b3, rect);

Run:

3. Axis transformation

The axes in WinForm are different from the plane right angle axes that we normally touch, the axes in the WinForm are exactly the opposite: the upper-left corner of the form is the origin (0,0), the horizontal to the left is X, and the vertical down is Y.

Next, let's do this by rotating the direction of the axis to draw a pattern of different angles, or by changing the position of the coordinate origin to balance the position of the axis.

Graphics g = this. CreateGraphics ();//Monochrome fill//solidbrush B1 = new SolidBrush (color.blue);//define monochrome brush          pen p = new Pen (color.blue,1);// Change the axis angle for (int i = 0; i < i++) {    g.rotatetransform (i);//Once a line is drawn for each rotation    g.drawline (p, 0, 0, 0);    G.resettransform ();//restore axis coordinates}//translation Axis G. TranslateTransform (+), G.drawline (p, 0, 0, 0); G.resettransform ();//pan to the specified coordinates, then the degree of rotation G. TranslateTransform (100,200); for (int i = 0; i < 8; i++) {g.rotatetransform; G.drawline (p, 0, 0, 100, 0);} G.dispose ();

Run:

4. Finally, let's take a look at the graphics, what else can we draw on this artboard?

In fact, we used to draw some simple graphics, straight lines, rectangles, fans, circles and so on, we can also use it to draw pictures, which can be used in its DrawImage method. Here I do not explain in detail, we are interested to go to MSDN to understand the next. We'll use this method later.


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.