Customize your drawing in WPF (iii)

Source: Internet
Author: User

Graphics merge

Sometimes we need to combine multiple graphs into one and then draw them, such as merging a circle with a rectangle. In the custom drawing of WPF, there are three ways to do it, respectively (1) using the GeometryGroup object, (2) using the Combinedgeometry object, and (3) using the Geometry.combin () static method. The first is to use set consolidation to add as many elements as you can to the collection, and the following two methods can only be merged in 22, but the latter are more flexible to merge, and can be the intersection of two graphics and set "difference Sets" and "XOR."

1, using the GeometryGroup object for graphic merging

Referring to the following code, we combine an ellipse 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);
    }

The effect chart is as follows:

We simply merged the two graphs together, noting that a hollow, colorless area appears in the middle part of the merged graph, which is determined by the fillrull of the merged graph, if we put group. FillRule = fillrule.evenodd, change to group. FillRule = Fillrule.nonzero; the fill effect will be as follows:

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.