For an article on graphics, it's worth looking at

Source: Internet
Author: User
Graphics Working with brushes in GDI + Drawing GDI + Objects
The following code draws a line, a ellipse, a curve, and a polygon object. As you can the code, I 抳 e used Pen object to fill these objects. More details.

protected override void OnPaint (PaintEventArgs e)
{
Graphics g = e.graphics;
Pen pn = new Pen (color.green, 10);
G.drawline (PN, 100, 10, 30, 10);
G.drawellipse (New Pen (Color.Red, 20), 20, 40, 20, 20);
G.drawbezier (New Pen (Color.Blue, 5), New Point (50,60), new
Point (150,10), new Point (200,230), new Point (100,100));
PointF point1 = new PointF (50.0f, 250.0f);
PointF Point2 = new PointF (100.0f, 25.0f);
PointF Point3 = new PointF (150.0f, 5.0f);
PointF point4 = new PointF (250.0f, 50.0f);
PointF point5 = new PointF (300.0f, 100.0f);

Pointf[] curvepoints = {point1, Point2, Point3, Point4, point5};
G.drawpolygon (New Pen (color.chocolate), curvepoints);
}
Brush and brushes Types
BrushType is an abstract base class.HatchBrush,LinearGradientBrush,PathGradientBrush,SolidBrushandTextureBrushClasses are inherited from Brush class. You are don don't use this class directly.
All brush types are defined inSystem.Drawingand its helper namespaces. Before using brushes, you need to add reference to this namespace.HatchBrushandGradientbrushare defined inSystem.Drawing.Drawing2DNamespace.
Use the brushes to fill GDI + objects with certain kind of brush. Generally callFillMethods ofGraphicsclass to fill various objects such asEllipse,ARC, orPolygon. There are different kinds of brushes. For example, solid brush, hatch brush, texture brush, and gradient brush.Solid Brushes
Solid brushes are normal brushes with no style. You fill GDI + object with a color. SolidBrush type is used to work with solid brushes.

Graphics g = e.graphics;
SolidBrush sdBrush1 = new SolidBrush (color.red);
SolidBrush sdBrush2 = new SolidBrush (color.green);
SolidBrush SdBrush3 = new SolidBrush (Color.Blue);
G.fillellipse (SdBrush2, 20, 40, 60, 70);
Rectangle rect = new Rectangle (0, 0, 200, 100);
G.fillpie (SDBRUSH3, 0, 0, 0.0f, 30.0f);
PointF point1 = new PointF (50.0f, 250.0f);
PointF Point2 = new PointF (100.0f, 25.0f);
PointF Point3 = new PointF (150.0f, 40.0f);
PointF point4 = new PointF (250.0f, 50.0f);
PointF point5 = new PointF (300.0f, 100.0f);
Pointf[] curvepoints = {point1, Point2, Point3, Point4, point5}; G.fillpolygon (SDBRUSH1, curvepoints);
The following code draws an ellipse, a pie, and a polygon.Hatch Brushes
Using System.Drawing.Drawing2D;
The hatch brushes are brushes with a hatch style, a foreground color, and a background color. Hatches are a combination of rectangle lines and the area between the lines. The foreground color defines the color of lines; The background color defines the color of between lines.
Hatchstyle defines the hatch styles.

Member Name
Backwarddiagonal
Cross
Darkdownwarddiagonal
Darkhorizontal
Darkupwarddiagonal
Darkvertical
Dasheddownwarddiagonal
Dashedhorizontal
Dashedupwarddiagonal
Dashedvertical
Diagonalbrick
Diagonalcross
Divot
Dotteddiamond
Dottedgrid
ForwardDiagonal
The following code shows how to draw hatch brushes.


protected override void OnPaint (PaintEventArgs e)
{
Graphics g = e.graphics;
HatchBrush hBrush1 = new HatchBrush (Hatchstyle.diagonalcross, Color.chocolate, color.red);
HatchBrush HBrush2 = new HatchBrush (hatchstyle.dashedhorizontal, Color.green, Color.Black);
HatchBrush HBrush3 = new HatchBrush (Hatchstyle.weave, Color.blueviolet, Color.Blue);
G.fillellipse (HBRUSH1, 20, 80, 60, 20);
Rectangle rect = new Rectangle (0, 0, 200, 100);
G.fillpie (HBRUSH3, 0, 0, 0.0f, 30.0f);
PointF point1 = new PointF (50.0f, 250.0f);
PointF Point2 = new PointF (100.0f, 25.0f);
PointF Point3 = new PointF (150.0f, 40.0f);
PointF point4 = new PointF (250.0f, 50.0f);
PointF point5 = new PointF (300.0f, 100.0f);

Pointf[] curvepoints = {point1, Point2, Point3, Point4, point5};
G.fillpolygon (HBRUSH2, curvepoints);
}

And the result looks like following-
Texture Brushes
The texture brushes provides you and the "to" use a image as brush and fill GDI + objects with the brush. The following code use 搈 yfile.bmp?as a brush. You are need to define a Image object and create brush with this brush into Fill method of GDI + objects.

Private Brush Txbrush;

protected override void OnPaint (PaintEventArgs e)
{
Graphics g = e.graphics;
G.fillrectangle (Txbrush, ClientRectangle);
}
private void Form1_Load (object sender, System.EventArgs e)
{
Image img = new Bitmap (@ "C:\myfile.bmp");
Txbrush = new TextureBrush (IMG);
}
The result looks like following:
gradient Brushes
Gradient brushes are provides more color to your GDI + objects. By using the LinearGradientBrush type, you can blend two colors together. The following code blends red and green colors.

protected override void OnPaint (PaintEventArgs e)
{
Graphics g = e.graphics;
Rectangle rect = new Rectangle (50, 30, 200, 200);
LinearGradientBrush Lbrush = new LinearGradientBrush (rect, color.red, Color.green, lineargradientmode.backwarddiagonal);
G.fillrectangle (Lbrush, rect);
}
And the result looks like the following:
This is like combines blue and green colors-

LinearGradientBrush Lbrush = new LinearGradientBrush (rect, Color.Blue, Color.green, lineargradientmode.vertical);
And the result looks like following:


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.