Customize your rendering in WPF (3)

Source: Internet
Author: User
Customize your rendering in WPF (3)
Zhou yinhui

Image Merging
Sometimes we need to merge multiple images into one and draw them, such as merging a circle and a rectangle. there are three methods to customize the rendering of WPF: (1) using the geometrygroup object; (2) using the combinedgeometry object; (3) using the geometry. combin () static method. The first method is to use a set and add any number of elements to the set. However, the latter two methods can only be merged in pairs, but the latter two methods are more flexible, it can be the "intersection", "Union", "difference set", and "exclusive or" of two images ".

1. Use a geometrygroup object to merge Images
Refer to the following Code , We merge an elliptic with a rectangle:   Protected   Override   Void Onrender (drawingcontext DC)
{
Base . Onrender (DC );

// Public sealed class geometrygroup: Geometry
Ellipsegeometry Ellipse =   New Ellipsegeometry ( New Point ( 50 , 50 ), 50 , 20 );
Rectanglegeometry rect =   New Rectanglegeometry ( New Rect ( 50 , 50 , 50 , 20 ), 5 , 5 );

Geometrygroup Group =   New Geometrygroup ();
Group. fillrule = Fillrule. evenodd;
Group. Children. Add (ellipse );
Group. Children. Add (rect );

DC. drawgeometry (brushes. lightblue, New Pen (brushes. Green, 2 ), Group );
}

As follows:

We simply merge the two images together by adding them. Note that a blank colorless area appears in the middle of the merged image, it is determined by the fillrull of the merged image. fillrule = fillrule. evenodd; changed to group. fillrule = fillrule. nonzero; the filling effect will be as follows:

2. Use the combinedgeometry object to merge images.
Refer to the following code to merge an elliptic with a rectangle:   Protected   Override   Void Onrender (drawingcontext DC)
{
Base . Onrender (DC );

// Public sealed class combinedgeometry: Geometry
Ellipsegeometry Ellipse =   New Ellipsegeometry ( New Point ( 50 , 50 ), 50 , 20 );
Rectanglegeometry rect =   New Rectanglegeometry ( New Rect ( 50 , 50 , 50 , 20 ), 5 , 5 );

Combinedgeometry combin =   New Combinedgeometry (geometrycombinemode. XOR, ellipse, rect );

DC. drawgeometry (brushes. lightblue, New Pen (brushes. Green, 2 ), Combin );
}

Note that the rycombinemode enumeration has four enumeration values. If we have a and B representing two figures respectively, then:
Geometrycombinemode. exclude: The merge results are A-B

Geometrycombinemode. intersect: The result is the intersection of A and B.

Geometrycombinemode. Union: the merged result is a + B.

Geometrycombinemode. XOR: The merge result is (A-B) + (B-A)

3. Use geometry. Combin () Static Method to merge Images
The static geometry. Combin () method is similar to using a combinedgeometry object to merge images.   Protected   Override   Void Onrender (drawingcontext DC)
{
Base . Onrender (DC );


// Geometry. Combin ()
Ellipsegeometry Ellipse =   New Ellipsegeometry ( New Point ( 50 , 50 ), 50 , 20 );
Rectanglegeometry rect =   New Rectanglegeometry ( New Rect ( 50 , 50 , 50 , 20 ), 5 , 5 );

Pathgeometry combin = Geometry. Combine (ellipse, rect, geometrycombinemode. XOR, Null );

DC. drawgeometry (brushes. lightblue, New Pen (brushes. Green, 2 ), Combin );

}

The geometry. Combin () static method contains parameters (rotation, scaling, etc.) used for graphic transformation. If no conversion is required, enter null.

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.