Detailed steps for c # GDI + simple plotting (2)

Source: Internet
Author: User

In the previous article, we have introduced how to use GDI + to draw simple images. This article continues to introduce some other drawing knowledge.
1. First, let's take a look at the Pen we used in the previous article.
Pen attributes include: Color, DashCap, DashStyle, EndCap, and StartCap ), width (Width.
We can use Pen to draw dotted lines, straight lines with arrows, etc.

Copy codeThe Code is as follows: Pen p = new Pen (Color. Blue, 5); // set the Pen width to Blue
Graphics g = this. CreateGraphics ();
// Draw the dotted line
P. DashStyle = DashStyle. Dot; // defines the dotted line style as a Dot.
G. DrawLine (p, 10, 10,200, 10 );
// Custom dotted line
P. DashPattern = new float [] {2, 1}; // you can specify an array of dashes and spaces.
G. DrawLine (p, 10, 20,200, 20 );
// Draw arrows, which are only useful for non-Closed Curves
P. DashStyle = DashStyle. Solid; // restore the Solid line
P. EndCap = LineCap. ArrowAnchor; // defines the line tail style as an arrow
G. DrawLine (p, 10, 30,200, 30 );
G. Dispose ();
P. Dispose ();

The code execution result is as follows:

2. Next let's take a look at the usage of the Brush.
Purpose: We can fill various image shapes with paint brushes, such as rectangles, ovans, slices, polygon, and closed paths. There are several different types of paint brushes:
• SolidBrush: the simplest form of paint brush. It is drawn with solid color.
• HatchBrush: similar to a SolidBrush, but this class can be used to select the pattern to be used for painting from a large number of Preset patterns, rather than solid colors
• TextureBrush: draws with textures (images)
• LinearGradientBrush: draws with two colors mixed along the gradient
• PathGradientBrush: A unique path defined by the programmer. It is drawn using a complex mixing color gradient.
Here we will briefly introduce several of them:Copy codeThe Code is as follows: Graphics g = this. CreateGraphics ();
Rectangle rect = new Rectangle (10, 10, 50, 50); // defines a Rectangle. The parameter is the abscissa coordinate of the start point and its length and width.
// Monochrome Filling
SolidBrush b1 = new SolidBrush (Color. Blue); // defines a monochrome paint brush.
G. FillRectangle (b1, rect); // fill the rectangle
// String
G. DrawString ("string", new Font ("", 10), b1, new PointF (90, 10 ));
// Fill the image
TextureBrush b2 = new TextureBrush (Image. FromFile (@ "e: \ picture \ 1.jpg "));
Rect. Location = new Point (10, 70); // you can change the start coordinate of the rectangle.
Rect. Width = 200; // change the Width of the rectangle.
Rect. Height = 200; // change the Height of the rectangle
G. FillRectangle (b2, rect );
// Fill with Gradient
Rect. Location = new Point (10,290 );
LinearGradientBrush b3 = new LinearGradientBrush (rect, Color. Yellow, Color. Black, LinearGradientMode. Horizontal );
G. FillRectangle (b3, rect );

Run:

3. Coordinate Axis Transformation
The coordinate axes in winform are different from those in normal contact plane. The coordinate axes in winform are in the opposite direction: the upper left corner of the form is the origin (0, 0), and X is increased horizontally to the left, vertical downward direction increases by Y

Next, we will use the orientation of the coordinate axis to draw a pattern of different angles, or change the coordinate origin to balance the coordinate axis position.

Copy codeThe Code is as follows: Graphics g = this. CreateGraphics ();
// Monochrome Filling
// SolidBrush b1 = new SolidBrush (Color. Blue); // defines a monochrome paint brush
Pen p = new Pen (Color. Blue, 1 );
// Change the coordinate axis angle
For (int I = 0; I <90; I ++)
{
G. RotateTransform (I); // draw a line for each rotation
G. DrawLine (p, 0, 0,100, 0 );
G. ResetTransform (); // restore coordinate axes
}
// Pan the coordinate axis
G. TranslateTransform (100,100 );
G. DrawLine (p, 0, 0,100, 0 );
G. ResetTransform ();
// First move horizontally to the specified coordinate, and then perform the degree rotation
G. TranslateTransform (100,200 );
For (int I = 0; I <8; I ++)
{
G. RotateTransform (45 );
G. DrawLine (p, 0, 0,100, 0 );
}
G. Dispose ();

Run:

4. Finally, let's take a look at what we can draw on Graphics.
In fact, we used to draw some simple images, straight lines, rectangles, slices, and circles. We can also use it to draw images, which can be used in the DrawImage method. I will not explain it in detail here. If you are interested, go to MSDN to learn more. this method will be used later.

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.