WPF's two-dimensional drawing (ii)--Geometry geometry

Source: Internet
Author: User

In the WPF DrawingContext object, the basic drawing ellipse and the rectangle's api:drawellipse and DrawRectangle are provided. However, these are far from sufficient, and we use the Drawgeometry function more often in our everyday applications, which can draw more complex geometries and provide many powerful and easy-to-use functions, in most scenarios, You can even replace the drawellipse and DrawRectangle functions.

In the WPF graphics architecture, the geometry class represents the base class for the geometry, and it is used to instantiate some of its subclasses, specifically:

Basic geometry

    • Segment: LineGeometry
    • Rectangle: RectangleGeometry
    • Ellipse: EllipseGeometry

Geometry collection

The path collection graph PathGeometry can contain a series of geometry collections, which are common:

    • Segment: LineSegment
    • Arc: ArcSegment

Bezier Curve: The Bezier series is also more, the following are several specific:

    1. BezierSegment : Creates a three-cubic Bezier curve between two points.
    2. polybeziersegment : Creates a series of three-cubic Bezier curves.
    3. PolyQuadraticBezierSegment : Creates a series of two Bezier curves.
    4. quadraticbeziersegment : Creates a two-time Bezier curve.

In addition to this combination of methods, the system provides a streamgeometry that is drawn through a series of APIs. It does not support bindings, animations, and is more flexible and efficient.

streamgeometry geometry = new streamgeometry;

Using(StreamgeometrycontextCTX = geometry.Open())
{
CTx.BeginFigure(NewPoint(10, 100),True,True);
CTx. LineTo(new Point(), true , false);
CTx. LineTo(new Point(+), true, false);
}

Composite geometry

You can create a composite geometry object by using GeometryGroup, Combinedgeometry, or Combine by calling the static Geometry method. The main differences are:

    • Combinedgeometry The sprites are superimposed and the sub-shapes with no area are discarded. You can combine only two sub-shapes (but these two sub-shapes can also be composite geometries).
    • GeometryGroup only the combination, not the area overlay. You can add multiple sub-shapes. For an example, see how to: Create a composite shape.

Combinedgeometry are stacked in four ways: Union, Intersect, Exclude, and Xor, with the following effects:

These are very useful in our everyday applications, see the MSDN article: How to: Create a composite shape and how to: Create a combined geometry.

Common methods

The Geometry object itself also contains a series of very useful methods, such as:

    • Getarea -Get the area of the Geometry.
    • FillContains -Determine if other Geometry are included.
    • Strokecontains -Determines whether the specified point is included.
    • Bounds: Getting an external rectangle

These are very common methods, such as Fillcontains,strokecontains for mouse hit testing is very convenient.

Rendering method

Geometry objects cannot be rendered independently as images, which are generally presented in the following ways:

Render in Path:

Can be rendered as a path object as a geometrydrawing.geometry parameter

    <PathStroke= "Black"strokethickness= "1" >        <Path.data>            <LineGeometryStartPoint= "10,20"EndPoint= "100,130" />        </Path.data>    </Path>

It's OK to write some simple geometry in this way, but it's a bit cumbersome for pathgeometry, so XAML uses a simple path- marking syntax to simplify this process .

 <  path  stroke  = "Black"   = "Gray"  >  <  path.data  >  <  pathgeometry  figures  = "M 10,100 C 10,300 300,-200 300,100 " />  </ Span style= "color: #800000;" >path.data  >  </ path  >  

Can even be directly simplified to:

    <Stroke= "Black"  Fill= "Gray"  Data= "M 10,100 C 10,300 300,-200 300,100 "/>

This syntax is very common in some third-party vectors converted to files, and if you can master it, it is very convenient to write some simple geometric figures.

Present in the DrawingContext

Can be used as drawingcontext. Drawgeometry parameter rendering, this way later in the article will be more explanation, here is not more introduction.

Present in the GeometryDrawing

Can be rendered as a drawing object as a geometrydrawing.geometry parameter

    <geometrydrawingBrush= "MediumBlue">        <Geometrydrawing.geometry>            <GeometryGroup>                <EllipseGeometryRadiusX= " the"RadiusY= "$"Center= "50,50" />                <EllipseGeometryRadiusX= "$"RadiusY= " the"Center= "50,50" />            </GeometryGroup>        </Geometrydrawing.geometry>    </geometrydrawing>

of course, drawing objects cannot be rendered independently, and are generally used as DrawingBrush or as drawingcontext.drawdrawing parameters .

Other uses:

Cropping a control as a uielement.clip parameter

    <ImageSource= "Sampleimages\waterlilies.jpg"Width= "$"Height= "Max"HorizontalAlignment= "Left">        <Image.clip>            <EllipseGeometryRadiusX= "+"RadiusY= " the"Center= "100,75"/>        </Image.clip>    </Image>

  

In addition, images are often cropped in drawinggroup.clipgeometry and Drawingcontext.pushclip.

As Doubleanimationusingpath. PathGeometry Property Generation Path Animation

The path of the object can be rotated (rotated) by the geometry path defined by the PathGeometry object.

Summary

Geometry function is very powerful, this article is just a brief introduction to its main functions, the following article, will be more examples to explain its use.

WPF's two-dimensional drawing (ii)--Geometry geometry

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.